/
automap.ts
839 lines (716 loc) · 23.3 KB
/
automap.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
import { TilesLayer } from './tilesLayer'
import * as Info from './types'
/// This file contains parsing and linting of automapper files, and application to tileslayers.
export type Automapper = Config[]
export interface Config {
name: string
runs: Run[]
}
export interface Run {
layerCopy: boolean
indexRules: IndexRule[]
}
export interface IndexRule {
tile: Info.Tile
rules: Rule[]
}
export type Rule = PosRule | RandomRule
export interface PosRule {
offset: Info.Coord
states: TileState[]
invert: boolean // whether to invert state matches, e.g. state.id = 0 will match all tiles with id != 0. (NOTINDEX)
}
export const defaultPosRule: PosRule = {
offset: { x: 0, y: 0 },
states: [{ id: 0 }],
invert: true,
}
export interface TileState {
id: number
vFlip?: boolean
hFlip?: boolean
rotate?: boolean
}
export interface RandomRule {
coef: number
}
// monadic result type à la rust.
type Result<T, E> =
| {
success: true
content: T
}
| {
success: false
content: E
}
function ok<T>(content: T): Result<T, any> {
return { success: true, content }
}
function err<T>(content: T): Result<any, T> {
return { success: false, content }
}
/// lexer
// range of characters in a line.
// range delimits space between characters:
// - range [0, 0] is an empty range at the start of the line
// - range [0, 1] contains the first character
type Range = [number, number]
enum TokenErrorKind {
MissingToken,
InvalidNumber,
InvalidHeader,
}
interface TokenError {
range: Range
line: number
reason: TokenErrorKind
note?: string
}
function tokError(reason: TokenErrorKind, line: number, range: Range, note?: string): TokenError {
return { range, line, reason, note }
}
// export enum Keyword {
// NewRun,
// Index,
// XFLIP, YFLIP, ROTATE,
// Pos,
// EMPTY, FULL,
// INDEX, NOTINDEX,
// NONE, OR,
// Random,
// NoDefaultRule,
// NoLayerCopy,
// }
type Token = { line: number; range: Range } & (
| { word: string }
| { header: string }
| { float: number }
| { int: number }
)
class FileReader {
state: {
line: number
token: number
}
lines: string[]
constructor(content: string) {
this.state = {
line: -1, // call to nextline() follows
token: 0,
}
this.lines = content.split(/\r?\n/)
this.nextLine()
}
nextLine() {
this.state.line++
this.state.token = 0
// ignore comments, newlines etc.
while (this.state.line < this.lines.length) {
const line = this.lines[this.state.line]
if (line === '' || /^\s*#/.test(line) || /^\s+$/.test(line)) this.state.line++
else break
}
}
token(): Result<Token, TokenError> {
if (this.state.line >= this.lines.length) {
return err(tokError(TokenErrorKind.MissingToken, this.state.line, [0, 0]))
}
const line = this.lines[this.state.line].substring(this.state.token)
if (line === '' || /^\s*#/.test(line)) {
const range: Range = [this.state.token, this.state.token]
return err(tokError(TokenErrorKind.MissingToken, this.state.line, range))
} else if (line[0] === '[') {
const end = line.indexOf(']')
if (end === -1) {
const range: Range = [this.state.token, this.state.token + line.length]
this.state.token = range[1]
return err(
tokError(
TokenErrorKind.InvalidHeader,
this.state.line,
range,
'Missing closing bracket "]"'
)
)
} else {
const range: Range = [this.state.token, this.state.token + end + 1]
this.state.token = range[1]
return ok({ header: line.substring(1, end), line: this.state.line, range })
}
} else if (/^[-+]?[\d\.]+%?/.test(line)) {
let str = line.split(/\s/)[0]
const range: Range = [this.state.token, this.state.token + str.length]
this.state.token = range[1] + 1 // add a single space
if (str[str.length - 1] === '%') {
const num = Number(str.substring(0, str.length - 1))
if (isNaN(num)) return err(tokError(TokenErrorKind.InvalidNumber, this.state.line, range))
else return ok({ float: num / 100.0, line: this.state.line, range })
} else {
const num = Number(str)
if (isNaN(num)) return err(tokError(TokenErrorKind.InvalidNumber, this.state.line, range))
else if (str.indexOf('.') === -1) return ok({ int: num, line: this.state.line, range })
else return ok({ float: num, line: this.state.line, range })
}
} else {
const word = line.split(/\s/)[0]
const range: Range = [this.state.token, this.state.token + word.length]
this.state.token = range[1] + 1 // add a single space
return ok({ range, line: this.state.line, word })
}
}
peek() {
const tok = this.state.token
const res = this.token()
this.state.token = tok
return res
}
lineEmpty() {
const tok = this.peek()
return tok.success === false && tok.content.reason === TokenErrorKind.MissingToken
}
empty() {
return (
this.state.line >= this.lines.length ||
(this.state.line === this.lines.length - 1 &&
this.lines[this.state.line].length <= this.state.token)
)
}
}
/// linting
export enum LintLevel {
Warning,
Error,
}
export interface Lint {
line: number
range: Range
level: LintLevel
reason: string
note?: string
}
function lintWarn(reason: string, line: number, range: Range, note?: string): Lint {
return { level: LintLevel.Warning, line, range, reason, note }
}
function lintErr(reason: string, line: number, range: Range, note?: string): Lint {
return { level: LintLevel.Error, line, range, reason, note }
}
// linter functions
// linter functions expect reader to be at the start of the line and return the reader
// after the last consumed token.
function lintHeader(reader: FileReader): Lint[] {
const errs: Lint[] = []
const tok = reader.token()
if (!tok.success || !('header' in tok.content))
errs.push(
lintErr(
'Expected a configuration name',
reader.state.line,
tok.content.range,
'Configuration name are written in square brackets, e.g. "[my config]"'
)
)
return errs
}
function lintIndex(reader: FileReader): Lint[] {
const errs: Lint[] = []
let tok = reader.token()
if (!tok.success || !('word' in tok.content) || tok.content.word !== 'Index') {
errs.push(lintErr('Expected "Index"', reader.state.line, tok.content.range))
return errs
}
tok = reader.token()
if (!tok.success || !('int' in tok.content)) {
errs.push(lintErr('Expected a tile id', reader.state.line, tok.content.range))
return errs
}
let xflip = false,
yflip = false,
rotate = false
while (!reader.lineEmpty()) {
tok = reader.token()
const validToks = ['XFLIP', 'YFLIP', 'ROTATE']
if (!tok.success || !('word' in tok.content) || !validToks.includes(tok.content.word)) {
errs.push(
lintErr(
'Unexpected token',
reader.state.line,
tok.content.range,
'Expected one of ' + validToks.map(t => '"' + t + '"').join(', ')
)
)
} else if (tok.content.word === 'XFLIP') {
if (xflip) errs.push(lintWarn('Duplicate "XFLIP"', reader.state.line, tok.content.range))
else xflip = true
} else if (tok.content.word === 'YFLIP') {
if (yflip) errs.push(lintWarn('Duplicate "YFLIP"', reader.state.line, tok.content.range))
else yflip = true
} else if (tok.content.word === 'ROTATE') {
if (rotate) errs.push(lintWarn('Duplicate "ROTATE"', reader.state.line, tok.content.range))
else rotate = true
}
}
return errs
}
function lintPos(reader: FileReader): Lint[] {
const errs: Lint[] = []
let tok = reader.token()
if (!tok.success || !('word' in tok.content) || tok.content.word !== 'Pos') {
errs.push(lintErr('Expected "Pos"', reader.state.line, tok.content.range))
return errs
}
// offset
tok = reader.token()
if (!tok.success || !('int' in tok.content))
errs.push(
lintErr(
'Expected position x-offset',
reader.state.line,
tok.content.range,
'"Pos" must be followed by a x-offset, then a y-offset, e.g. "Pos -1 1"'
)
)
tok = reader.token()
if (!tok.success || !('int' in tok.content))
errs.push(
lintErr(
'Expected position y-offset',
reader.state.line,
tok.content.range,
'"Pos" must be followed by a x-offset, then a y-offset, e.g. "Pos -1 1"'
)
)
// rule
tok = reader.token()
const validToks = ['EMPTY', 'FULL', 'INDEX', 'NOTINDEX']
if (!tok.success || !('word' in tok.content) || !validToks.includes(tok.content.word))
errs.push(
lintErr(
'Unexpected token',
reader.state.line,
tok.content.range,
'Expected one of ' + validToks.map(t => '"' + t + '"').join(', ')
)
)
else if (tok.content.word === 'INDEX' || tok.content.word === 'NOTINDEX') {
tok = reader.token()
if (!tok.success || !('int' in tok.content))
errs.push(
lintErr(
'Expected a tile index',
reader.state.line,
tok.content.range,
'"INDEX" or "NOTINDEX" must be followed by a tile index, e.g. "INDEX 10"'
)
)
let none = false,
xflip = false,
yflip = false,
rotate = false
while (!reader.lineEmpty()) {
tok = reader.token()
const validToks = ['XFLIP', 'YFLIP', 'ROTATE', 'OR', 'NONE']
if (!tok.success || !('word' in tok.content) || !validToks.includes(tok.content.word)) {
errs.push(
lintErr(
'Unexpected token',
reader.state.line,
tok.content.range,
'Expected one of ' + validToks.map(t => '"' + t + '"').join(', ')
)
)
} else if (tok.content.word === 'XFLIP') {
if (xflip) errs.push(lintWarn('Duplicate "XFLIP"', reader.state.line, tok.content.range))
else if (none)
errs.push(
lintWarn(
'"ROTATE" after a "NONE"',
reader.state.line,
tok.content.range,
'"NONE" conflicts with other flags'
)
)
xflip = true
} else if (tok.content.word === 'YFLIP') {
if (yflip) errs.push(lintWarn('Duplicate "YFLIP"', reader.state.line, tok.content.range))
else if (none)
errs.push(
lintWarn(
'"ROTATE" after a "NONE"',
reader.state.line,
tok.content.range,
'"NONE" conflicts with other flags'
)
)
yflip = true
} else if (tok.content.word === 'ROTATE') {
if (rotate) errs.push(lintWarn('Duplicate "ROTATE"', reader.state.line, tok.content.range))
else if (none)
errs.push(
lintWarn(
'"ROTATE" after a "NONE"',
reader.state.line,
tok.content.range,
'"NONE" conflicts with other flags'
)
)
rotate = true
} else if (tok.content.word === 'NONE') {
if (none) errs.push(lintWarn('Duplicate "NONE"', reader.state.line, tok.content.range))
else if (xflip || yflip || rotate)
errs.push(
lintWarn(
'"NONE" preceded by a "XFLIP", "YFLIP" or "ROTATE"',
reader.state.line,
tok.content.range,
'"NONE" conflicts with the previous flags'
)
)
none = true
xflip = yflip = rotate = false
} else if (tok.content.word === 'OR') {
tok = reader.token()
if (!tok.success || !('int' in tok.content))
errs.push(
lintErr(
'Expected a tile index',
reader.state.line,
tok.content.range,
'"OR" must be followed by a tile index, e.g. "INDEX 3 OR 4"'
)
)
xflip = yflip = rotate = none = false
}
}
}
return errs
}
function lintRandom(reader: FileReader): Lint[] {
const errs: Lint[] = []
let tok = reader.token()
if (!tok.success || !('word' in tok.content) || tok.content.word !== 'Random') {
errs.push(lintErr('Expected "Random"', reader.state.line, tok.content.range))
return errs
}
tok = reader.token()
if (!tok.success || (!('int' in tok.content) && !('float' in tok.content))) {
errs.push(lintErr('Expected a number', reader.state.line, tok.content.range))
return errs
}
return errs
}
function lintAutomapper(reader: FileReader): Lint[] {
const errs = lintHeader(reader)
if (!reader.lineEmpty()) {
const range: Range = [reader.state.token, reader.lines[reader.state.line].length]
errs.push(lintWarn('Expected end of line', reader.state.line, range))
}
reader.nextLine()
let noLayerCopy = false
let noDefaultRule = false
let indexRule = false
while (!reader.empty()) {
let tok = reader.token()
if (tok.success && 'header' in tok.content) {
// finished config
if (!indexRule)
errs.push(lintWarn('Previous config or run is empty', reader.state.line, tok.content.range))
reader.state.token = 0
return errs
}
const validToks = ['NoLayerCopy', 'Index', 'NewRun']
if (indexRule) validToks.push('Pos', 'Random', 'NoDefaultRule')
if (!tok.success || !('word' in tok.content) || !validToks.includes(tok.content.word))
errs.push(
lintErr(
'Unexpected token',
reader.state.line,
tok.content.range,
'Expected one of ' + validToks.map(t => '"' + t + '"').join(', ')
)
)
else if (tok.content.word === 'NoLayerCopy') {
if (indexRule)
errs.push(lintWarn('Misplaced "NoLayerCopy"', reader.state.line, tok.content.range, 'For readability, "NoLayerCopy" should be placed at the start of the run.'))
if (noLayerCopy)
errs.push(lintWarn('Duplicate "NoLayerCopy"', reader.state.line, tok.content.range))
else noLayerCopy = true
} else if (tok.content.word === 'Index') {
reader.state.token = 0
errs.push(...lintIndex(reader))
indexRule = true
noDefaultRule = false
} else if (tok.content.word === 'NewRun') {
if (!indexRule)
errs.push(lintWarn('Previous run is empty', reader.state.line, tok.content.range))
indexRule = false
noDefaultRule = false
noLayerCopy = false
} else if (tok.content.word === 'Pos') {
reader.state.token = 0
errs.push(...lintPos(reader))
} else if (tok.content.word === 'Random') {
reader.state.token = 0
errs.push(...lintRandom(reader))
} else if (tok.content.word === 'NoDefaultRule') {
if (noDefaultRule)
errs.push(lintWarn('Duplicate "NoDefaultRule"', reader.state.line, tok.content.range))
else noDefaultRule = true
}
if (!reader.lineEmpty()) {
const range: Range = [reader.state.token, reader.lines[reader.state.line].length]
errs.push(lintWarn('Expected end of line', reader.state.line, range))
}
reader.nextLine()
}
return errs
}
export function lint(content: string): Lint[] {
const reader = new FileReader(content)
const errs: Lint[] = []
while (!reader.empty()) {
errs.push(...lintAutomapper(reader))
}
return errs
}
export function lintToString(lint: Lint): string {
const level = lint.level === LintLevel.Error ? 'error' : 'warning'
let str = `[${level}] line ${lint.line + 1}, chars ${lint.range[0]}-${lint.range[1]}: ${lint.reason}.`
if (lint.note)
str += ` Note: ${lint.note}`
return str
}
/// parsing
export function parse(content: string): Config[] | null {
const reader = new FileReader(content)
const automappers: Config[] = []
let automapper: Config | null = null as Config | null
let run: Run | null = null as Run | null
let indexRule: IndexRule | null = null as IndexRule | null
let defaultRule = true
function finishIndexRule() {
if (indexRule && defaultRule) {
indexRule.rules.push(defaultPosRule)
}
indexRule = null
defaultRule = true
}
function newIndexRule(tile: Info.Tile) {
finishIndexRule()
indexRule = { tile, rules: [] }
run!.indexRules.push(indexRule)
}
function newRun() {
finishIndexRule()
run = { layerCopy: true, indexRules: [] }
automapper!.runs.push(run)
}
function newConfig(name: string) {
automapper = { name, runs: [] }
automappers.push(automapper)
newRun()
}
while (!reader.empty()) {
let tok = reader.token()
if ('header' in tok.content) {
const name = tok.content.header
newConfig(name)
}
else if (!('word' in tok.content) || !automapper) {
// invalid line, skip
continue
}
else if (tok.content.word === 'NewRun') {
newRun()
}
else if (tok.content.word === 'Index') {
// id
tok = reader.token()
if (!tok.success || !('int' in tok.content)) return null
const id = tok.content.int
// flags
let flags = 0
do {
tok = reader.token()
if (tok.success && 'word' in tok.content) {
if (tok.content.word === 'XFLIP') flags |= Info.TileFlags.VFLIP
else if (tok.content.word === 'YFLIP') flags |= Info.TileFlags.HFLIP
else if (tok.content.word === 'ROTATE') flags |= Info.TileFlags.ROTATE
}
} while (tok.success)
const tile: Info.Tile = { id, flags }
newIndexRule(tile)
}
else if (indexRule !== null && tok.content.word === 'Pos') {
// offset
tok = reader.token()
if (!tok.success || !('int' in tok.content)) return null
const x = tok.content.int
tok = reader.token()
if (!tok.success || !('int' in tok.content)) return null
const y = tok.content.int
const offset = { x, y }
if (x === 0 && y === 0) {
defaultRule = false
}
// rule
tok = reader.token()
if (!tok.success || !('word' in tok.content)) return null
if (['EMPTY', 'FULL'].includes(tok.content.word)) {
const rule: PosRule = {
offset: { x, y },
states: [{ id: 0 }],
invert: tok.content.word === 'FULL',
}
indexRule.rules.push(rule)
}
else if (['INDEX', 'NOTINDEX'].includes(tok.content.word)) {
const invert = tok.content.word === 'NOTINDEX'
// id
tok = reader.token()
if (!tok.success || !('int' in tok.content)) return null
const id = tok.content.int
let state: TileState = { id }
const states = [state]
// flags
do {
tok = reader.token()
if (tok.success && 'word' in tok.content) {
if (tok.content.word === 'XFLIP') {
state.vFlip = true
if (!('hFlip' in state)) state.hFlip = false
if (!('rotate' in state)) state.rotate = false
} else if (tok.content.word === 'YFLIP') {
if (!('vFlip' in state)) state.vFlip = false
state.hFlip = true
if (!('rotate' in state)) state.rotate = false
} else if (tok.content.word === 'ROTATE') {
if (!('vFlip' in state)) state.vFlip = false
if (!('hFlip' in state)) state.hFlip = false
state.rotate = true
} else if (tok.content.word === 'NONE') {
state.vFlip = false
state.hFlip = false
state.rotate = false
} else if (tok.content.word === 'OR') {
// id
tok = reader.token()
if (!tok.success || !('int' in tok.content)) return null
const id = tok.content.int
state = { id }
states.push(state)
}
}
} while (tok.success)
const rule: PosRule = { offset, states, invert }
indexRule.rules.push(rule)
}
}
else if (indexRule !== null && tok.content.word === 'Random') {
tok = reader.token()
let coef = 1.0
if (tok.success && 'float' in tok.content) coef = tok.content.float
else if (tok.success && 'int' in tok.content) coef = 1.0 / tok.content.int
const rule: RandomRule = { coef }
indexRule.rules.push(rule)
}
else if (indexRule !== null && tok.content.word === 'NoDefaultRule') {
defaultRule = false
}
else if (run !== null && tok.content.word === 'NoLayerCopy') {
run.layerCopy = false
}
reader.nextLine()
}
finishIndexRule()
return automappers
}
/// apply automapper
// implementation taken from ddnet source code and adpted to js.
// Based on triple32inc from https://github.com/skeeto/hash-prospector/tree/79a6074062a84907df6e45b756134b74e2956760
function hashUint32(num: number) {
num++
num ^= num >> 17
num *= 0xed5ad4bb
num ^= num >> 11
num *= 0xac4c1b51
num ^= num >> 15
num *= 0x31848bab
num ^= num >> 14
return num
}
const HASH_MAX = 65536
const RAND_MAX = 2147483647
// implementation taken from ddnet source code and adpted to js.
function hashLocation(seed: number, run: number, rule: number, x: number, y: number) {
const prime = 31
let hash = 1
hash = hash * prime + hashUint32(seed)
hash = hash * prime + hashUint32(run)
hash = hash * prime + hashUint32(rule)
hash = hash * prime + hashUint32(x)
hash = hash * prime + hashUint32(y)
hash = hashUint32(hash * prime) // Just to double-check that values are well-distributed
return hash % HASH_MAX
}
function cloneLayer(layer: TilesLayer) {
const clone = new TilesLayer()
clone.init(layer.width, layer.height, i => TilesLayer.cloneTile(layer.tiles[i]))
return clone
}
function tileMatches(tile: Info.Tile, test: TileState) {
if (tile.id !== test.id) return false
const vFlip = (tile.flags & Info.TileFlags.VFLIP) !== 0
const hFlip = (tile.flags & Info.TileFlags.HFLIP) !== 0
const rotate = (tile.flags & Info.TileFlags.ROTATE) !== 0
if ('vFlip' in test && test.vFlip !== vFlip) return false
else if ('hFlip' in test && test.hFlip !== hFlip) return false
else if ('rotate' in test && test.rotate !== rotate) return false
return true
}
function inBounds(x: number, y: number, w: number, h: number) {
return x >= 0 && y >= 0 && x < w && y < h
}
const outTile: Info.Tile = {
id: -1,
flags: 0,
}
function posRuleMatches(rule: PosRule, layer: TilesLayer, x: number, y: number) {
const xx = x + rule.offset.x
const yy = y + rule.offset.y
const tile = inBounds(xx, yy, layer.width, layer.height)
? layer.getTile(xx, yy)
: outTile
return rule.invert
? rule.states.every(s => !tileMatches(tile, s))
: rule.states.some(s => tileMatches(tile, s))
}
export function automap(layer: TilesLayer, automapper: Config, seed: number) {
if (seed === 0) seed = Math.floor(Math.random() * RAND_MAX)
let r1 = 0
for (const run of automapper.runs) {
let srcLayer = run.layerCopy ? cloneLayer(layer) : layer
for (let y = 0; y < layer.height; y++) {
for (let x = 0; x < layer.height; x++) {
let r2 = 0
for (const irule of run.indexRules) {
let match = true
for (const rule of irule.rules) {
if ('coef' in rule) {
match = hashLocation(seed, r1, r2, x, y) < HASH_MAX * rule.coef
} else {
match = posRuleMatches(rule, srcLayer, x, y)
}
if (!match)
break
}
if (match) {
layer.setTile(x, y, TilesLayer.cloneTile(irule.tile))
}
r2++
}
}
}
r1++
}
}