-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaScripting.html
More file actions
1271 lines (1197 loc) · 51.6 KB
/
Copy pathLuaScripting.html
File metadata and controls
1271 lines (1197 loc) · 51.6 KB
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lua scripting in TileZed</title>
<style>
:root {
color-scheme: light dark;
--page: #f5f6f8;
--panel: #ffffff;
--text: #20242a;
--muted: #59616c;
--border: #cdd2d9;
--accent: #b31c2c;
--code: #f0f2f5;
--note: #eef5ff;
--warn: #fff4dd;
}
@media (prefers-color-scheme: dark) {
:root {
--page: #1b1d20;
--panel: #24272b;
--text: #eeeeee;
--muted: #b8bec7;
--border: #4b5058;
--accent: #ff6675;
--code: #181a1d;
--note: #1f3045;
--warn: #4b3a18;
}
}
body {
max-width: 1120px;
margin: 0 auto;
padding: 24px;
color: var(--text);
background: var(--page);
font: 15px/1.55 "Segoe UI", Arial, sans-serif;
}
h1, h2, h3 { line-height: 1.2; }
h1 { margin-top: 0; }
h2 {
margin-top: 2em;
padding-bottom: .25em;
border-bottom: 1px solid var(--border);
}
h3 { margin-top: 1.5em; }
a { color: var(--accent); }
code, pre { font-family: Consolas, "Courier New", monospace; }
code {
padding: .08em .3em;
border-radius: 3px;
background: var(--code);
}
pre {
overflow: auto;
padding: 14px 16px;
border: 1px solid var(--border);
border-radius: 5px;
background: var(--code);
tab-size: 4;
}
table {
width: 100%;
border-collapse: collapse;
background: var(--panel);
}
th, td {
padding: 8px 10px;
border: 1px solid var(--border);
text-align: left;
vertical-align: top;
}
th { white-space: nowrap; }
.note, .warning {
margin: 1em 0;
padding: 10px 14px;
border-left: 4px solid #4488cc;
background: var(--note);
}
.warning {
border-left-color: #d59100;
background: var(--warn);
}
.api {
margin: .35em 0;
padding: .6em .8em;
border-left: 3px solid var(--border);
background: var(--panel);
}
.api code { font-weight: 600; }
.muted { color: var(--muted); }
.toc {
columns: 2 280px;
padding: 14px 30px;
border: 1px solid var(--border);
background: var(--panel);
}
</style>
</head>
<body>
<h1>Lua scripting in TileZed</h1>
<p>TileZed embeds Lua 5.2.1 for map editing and mapping automation. This is
an editor API: it does <strong>not</strong> execute Project Zomboid gameplay
scripts and it is not the API exposed by the game.</p>
<p>BuildingEd has a separate transactional API and global
<code>building</code> object. See the
<a href="../BuildingEd/LuaScripting.html">BuildingEd Lua scripting
reference</a>.</p>
<div class="note">
The authoritative binding is <code>src/tiled/luatiled.pkg</code>. The
ready-to-run examples installed in the <code>lua</code> directory use only
functions exposed by that binding.
</div>
<h2 id="contents">Contents</h2>
<ul class="toc">
<li><a href="#execution">Execution modes</a></li>
<li><a href="#first-script">Your first script</a></li>
<li><a href="#rules">Important rules</a></li>
<li><a href="#examples">Examples</a></li>
<li><a href="#interactive-tools">Interactive tools</a></li>
<li><a href="#api-map">Map</a></li>
<li><a href="#api-layer">Layer and TileLayer</a></li>
<li><a href="#api-app">Application actions</a></li>
<li><a href="#api-objects">ObjectGroup and MapObject</a></li>
<li><a href="#api-bmp">MapBmp and rules</a></li>
<li><a href="#api-basic">Tile, Color, Region and Rect</a></li>
<li><a href="#api-tool">TileTool</a></li>
<li><a href="#api-perlin">Perlin</a></li>
<li><a href="#behavior">Detailed function behavior</a></li>
<li><a href="#errors">Errors and cancellation</a></li>
</ul>
<h2 id="execution">Execution modes</h2>
<table>
<thead>
<tr><th>Mode</th><th>How to run it</th><th>Globals</th><th>Saving</th></tr>
</thead>
<tbody>
<tr>
<td>Current map</td>
<td><strong>Tools > Lua Console > Run Script</strong></td>
<td><code>map</code>, <code>app</code>, <code>scriptDirectory</code></td>
<td>Changes are added to the current TileZed undo stack.</td>
</tr>
<tr>
<td>TMX directory</td>
<td><strong>Lua Console > Run In Directory</strong></td>
<td><code>map</code>, <code>app</code>, <code>scriptDirectory</code></td>
<td>Selected TMX files are rewritten only when changed. Enable backups.</td>
</tr>
<tr>
<td>WorldEd world</td>
<td><strong>Lua Console > Run On World</strong></td>
<td><code>map</code>, <code>app</code>, <code>scriptDirectory</code></td>
<td>Existing TMX cells in the selected PZW are processed. Enable backups.</td>
</tr>
<tr>
<td>Interactive tool</td>
<td>Declare a tool in <code>LuaTools.txt</code>, then select it in TileZed.</td>
<td><code>map</code>, <code>self</code>, <code>scriptDirectory</code></td>
<td>The tool calls <code>self:applyChanges("Undo label")</code>.</td>
</tr>
</tbody>
</table>
<p><code>scriptDirectory</code> is the absolute directory containing the
running script. Use it to include a companion file:</p>
<pre><code>dofile(scriptDirectory .. "/shared-functions.lua")</code></pre>
<p>When a TMX belongs to a loaded WorldEd project,
<code>map:cellX()</code> and <code>map:cellY()</code> return its absolute
world-cell coordinates. They return <code>-1</code> when that context is not
available.</p>
<h2 id="first-script">Your first script</h2>
<p>Save this as UTF-8, open a map, and run it from the Lua Console:</p>
<pre><code>print(("Map: %d x %d tiles, levels %d to %d")
:format(map:width(), map:height(), map:minLevel(), map:maxLevel()))
print(("World cell: %d,%d"):format(map:cellX(), map:cellY()))
for index = 0, map:layerCount() - 1 do
local layer = map:layerAt(index)
print(("[%d] %s (%s)")
:format(index, layer:nameWithPrefix(), layer:type()))
end</code></pre>
<p>The installed file <code>lua/example-inspect-map.lua</code> contains a
longer read-only version.</p>
<h2 id="rules">Important rules</h2>
<ul>
<li>Tile coordinates and API collection indexes are zero-based. Lua tables
returned as lists are still iterated normally with <code>ipairs()</code>,
starting at one.</li>
<li><code>layerAt(0)</code>, <code>tilesetAt(0)</code>, and
<code>objectAt(0)</code> access the first element.</li>
<li>Always test lookup results. A missing layer, tileset, tile or rule
returns <code>nil</code>.</li>
<li>Layer lookups use the level-prefixed name, for example
<code>0_Floor</code>, <code>8_RoomDefs</code>, or
<code>-1_Floor</code>. <code>layer:name()</code> returns the unprefixed
name; <code>layer:nameWithPrefix()</code> returns the lookup name.</li>
<li>The complete available tileset catalog is loaded before scripts can
run. Tile lookup, placement, deletion and replacement are synchronous
and require no callbacks.</li>
<li>Batch changes are collected and then applied through TileZed. Do not
call <code>map:write()</code> to overwrite the current map; that function
is intended for writing a separate map or a map created in Lua.</li>
<li>Run scripts that modify many TMX files on a copy first and keep the
batch backup option enabled.</li>
<li>Use UTF-8 for scripts, paths, object names and custom properties.</li>
</ul>
<h2 id="examples">Batch examples</h2>
<h3>Inspect tilesets and used tiles</h3>
<pre><code>for index = 0, map:tilesetCount() - 1 do
local tileset = map:tilesetAt(index)
print(("tileset[%d] = %s"):format(index, tileset:name()))
end
local floor = map:tileLayer("0_Floor")
if floor then
local tile = floor:tileAt(10, 20)
if tile then
print(("10,20 = %s_%d")
:format(tile:tileset():name(), tile:id()))
end
end</code></pre>
<h3>Fill the current selection</h3>
<pre><code>local layer = assert(map:tileLayer("0_Floor"), "Missing layer 0_Floor")
local tile = assert(
map:tile("floors_interior_tilesandwood_01_0"),
"Edit TILE_NAME in the script: this tile is not loaded")
local selection = map:tileSelection()
if selection:isEmpty() then
error("Select one or more tiles before running this script")
end
layer:fill(selection, tile)
print(("Filled %d selection rectangle(s)"):format(selection:rectCount()))</code></pre>
<p>A configurable version is installed as
<code>lua/example-fill-selection.lua</code>.</p>
<h3>Create a RoomDefs layer and object</h3>
<pre><code>local layerName = "8_RoomDefs"
local roomDefs = map:objectLayer(layerName)
if not roomDefs then
roomDefs = map:newObjectLayer(layerName)
map:addLayer(roomDefs)
print("Created " .. layerName)
end
local room = MapObject:new("example_room", "RoomDef", 20, 20, 12, 8)
room:setProperty("usage", "documentation example")
roomDefs:addObject(room)
print(("RoomDefs now contains %d object(s)"):format(roomDefs:objectCount()))</code></pre>
<p>See <code>lua/example-roomdefs.lua</code> for a version that first checks
whether the example object already exists.</p>
<h3>List and update object properties</h3>
<pre><code>local zones = map:objectLayer("0_Zones")
if zones then
for _, object in ipairs(zones:objects()) do
print(object:name(), object:type(), object:shape())
for _, propertyName in ipairs(object:propertyNames()) do
print(" " .. propertyName .. " = " .. object:property(propertyName))
end
if object:type() == "TownZone" then
object:setProperty("reviewed", "true")
end
end
end</code></pre>
<h3>Paint an exact BMP color</h3>
<pre><code>local mainBmp = map:bmp(0) -- 0 = Map.png, 1 = Map_veg.png
local oldColor = rgb(128, 64, 32)
local newColor = rgb(130, 66, 34)
for y = 0, map:height() - 1 do
for x = 0, map:width() - 1 do
if mainBmp:pixel(x, y) == oldColor.pixel then
mainBmp:setPixel(x, y, newColor)
end
end
end</code></pre>
<h2 id="interactive-tools">Interactive tools</h2>
<p>An interactive tool is loaded from an entry in the application or user
<code>LuaTools.txt</code>:</p>
<pre><code>tool
{
label = Example Painter
icon = tool-example.png
script = tool-example.lua
dialog-title = Example Painter
}</code></pre>
<p>The script may define these callbacks:</p>
<div class="api"><code>activate()</code> — tool selected.</div>
<div class="api"><code>deactivate()</code> — tool deselected.</div>
<div class="api"><code>mouseMoved(buttons, x, y, modifiers)</code></div>
<div class="api"><code>mousePressed(buttons, x, y, modifiers)</code></div>
<div class="api"><code>mouseReleased(buttons, x, y, modifiers)</code></div>
<div class="api"><code>modifiersChanged(modifiers)</code></div>
<div class="api"><code>options()</code> and
<code>setOption(name, value)</code> — optional tool-dialog integration.</div>
<p><code>buttons.left</code>, <code>buttons.right</code>,
<code>modifiers.alt</code>, <code>modifiers.control</code>, and
<code>modifiers.shift</code> are true when active. Mouse coordinates are
floating-point tile coordinates at the current level.</p>
<pre><code>local tileName = "floors_interior_tilesandwood_01_0"
function activate()
self:setCursorType(TileTool.EdgeTool)
end
function mouseMoved(buttons, x, y, modifiers)
self:clearToolTiles()
local tile = map:tile(tileName)
local layer = self:currentLayer()
if tile and layer then
local point = Region:new()
point:unite(math.floor(x), math.floor(y), 1, 1)
self:setToolTile(layer:nameWithPrefix(), point, tile)
end
end
function mouseReleased(buttons, x, y, modifiers)
if buttons.left then
local layer = self:currentLayer()
local tile = map:tile(tileName)
if layer and tile then
layer:setTile(math.floor(x), math.floor(y), tile)
self:applyChanges("Example Painter")
end
end
end</code></pre>
<p>The shipped tools <code>tool-edge.lua</code>,
<code>tool-curb.lua</code>, <code>tool-fence.lua</code>, and
<code>tool-parking-stall.lua</code> are complete examples with preview,
dragging, cancellation and undo.</p>
<h2 id="api-map">API reference: Map</h2>
<p class="muted">In batch scripts the global <code>map</code> wraps the
current TMX. Collections accessed by index are zero-based.</p>
<div class="api"><code>Map:new(orientation, width, height [, tileWidth = 64 [, tileHeight = 32]])</code></div>
<div class="api"><code>orientation()</code> — one of
<code>Map.Unknown</code>, <code>Map.Orthogonal</code>,
<code>Map.Isometric</code>, <code>Map.LevelIsometric</code>,
<code>Map.Staggered</code>.</div>
<div class="api"><code>width()</code>, <code>height()</code>,
<code>minLevel()</code>, <code>maxLevel()</code></div>
<div class="api"><code>cellX()</code>, <code>cellY()</code> — absolute
WorldEd cell coordinates, or <code>-1</code>.</div>
<div class="api"><code>layerCount()</code>,
<code>layerAt(index)</code>, <code>layer(prefixedName)</code></div>
<div class="api"><code>tileLayer(prefixedName)</code>,
<code>tileLayerAt(level, baseName)</code>,
<code>objectLayer(prefixedName)</code></div>
<div class="api"><code>newTileLayer(prefixedName)</code>,
<code>newObjectLayer(prefixedName)</code></div>
<div class="api"><code>addLayer(layer)</code>,
<code>insertLayer(index, layer)</code>,
<code>removeLayer(index)</code></div>
<div class="api"><code>tilesetCount()</code>,
<code>tileset(name)</code>, <code>tilesetAt(index)</code>,
<code>addTileset(tileset)</code></div>
<div class="api"><code>tile("tileset_name_id")</code>,
<code>tile(tilesetName, id)</code>, <code>noneTile()</code></div>
<div class="api"><code>replaceTilesByName("from_name_id;to_name_id;...")</code>
— replaces pairs across tile layers and loads required known tilesets.</div>
<div class="api"><code>clearTilesByName(tileName)</code> clears every
matching occurrence in all tile layers and returns the number changed.</div>
<div class="api"><code>replaceTileByName(oldTileName, newTileName)</code>
replaces every occurrence in all tile layers and returns the number
changed. An empty destination clears matching tiles.</div>
<div class="api"><code>tileSelection()</code>,
<code>setTileSelection(region)</code></div>
<div class="api"><code>bmp(0)</code> returns Map.png data;
<code>bmp(1)</code> returns Map_veg.png data.</div>
<div class="api"><code>alias(name)</code>,
<code>rules()</code>, <code>ruleCount()</code>,
<code>ruleAt(index)</code>, <code>rule(label)</code></div>
<div class="api"><code>blends()</code>, <code>blendLayers()</code>,
<code>isBlendTile(tile)</code></div>
<div class="api"><code>noBlend(layerName)</code>,
<code>reloadRules()</code>, <code>reloadBlends()</code></div>
<div class="api"><code>write(path)</code> — writes a separate TMX and returns
true on success.</div>
<h2 id="api-layer">API reference: Layer and TileLayer</h2>
<h3>Layer</h3>
<div class="api"><code>name()</code>, <code>nameWithPrefix()</code>,
<code>type()</code></div>
<div class="api"><code>asTileLayer()</code>,
<code>asObjectGroup()</code> — return <code>nil</code> for the wrong type.</div>
<h3>TileLayer</h3>
<div class="api"><code>TileLayer:new(name, x, y, width, height)</code></div>
<div class="api"><code>level()</code></div>
<div class="api"><code>tileAt(x, y)</code>,
<code>setTile(x, y, tile)</code>, <code>clearTile(x, y)</code></div>
<div class="api"><code>tileNameAt(x, y)</code> returns the complete
<code>tileset_name_id</code>, or an empty string.</div>
<div class="api"><code>setTileByName(x, y, tileName)</code> places a
named tile, loading its known tileset if required, and returns success.</div>
<div class="api"><code>clearTileByName(x, y, tileName)</code> clears the
coordinate only when the current tile has that exact name.</div>
<div class="api"><code>replaceTileByNameAt(x, y, oldTileName, newTileName)</code>
conditionally replaces one coordinate. An empty destination clears it.</div>
<div class="api"><code>erase(x, y, width, height)</code>,
<code>erase(rect)</code>, <code>erase(region)</code>,
<code>erase()</code></div>
<div class="api"><code>fill(x, y, width, height, tile)</code>,
<code>fill(rect, tile)</code>, <code>fill(region, tile)</code>,
<code>fill(tile)</code></div>
<div class="api"><code>replaceTile(oldTile, newTile)</code> — returns true
when at least one tile was replaced.</div>
<h2 id="api-app">API reference: application actions</h2>
<p>The global <code>app</code> requests existing TileZed Qt actions.
Requests run only after the script succeeds and its map transaction has
entered the Undo stack.</p>
<div class="api"><code>app:availableActions()</code> returns
<code>save</code>, <code>saveAs</code>, <code>export</code>,
<code>exportBinary</code>, <code>convertToLot</code>,
<code>mapProperties</code>, <code>launchBuildingEd</code>, and
<code>launchWorldEd</code>.</div>
<div class="api"><code>app:invoke(actionName)</code> queues one
whitelisted action. Dialog-based actions open their normal Qt dialog.</div>
<pre><code>local floor = assert(map:tileLayerAt(0, "Floor"))
assert(floor:setTileByName(10, 20, "floors_interior_tilesandwood_01_0"))
app:invoke("exportBinary")</code></pre>
<div class="warning">Lua does not expose arbitrary QObject construction,
widget ownership, signals, or slots. Use the explicit action list.
In-game map feature generation is a WorldEd operation, not a TileZed
action. Avoid dialog actions in directory/world batch scripts because the
request is executed once for every processed map.</div>
<h2 id="api-objects">API reference: ObjectGroup and MapObject</h2>
<h3>ObjectGroup</h3>
<div class="api"><code>ObjectGroup:new(name, x, y, width, height)</code></div>
<div class="api"><code>color()</code>, <code>setColor(color)</code></div>
<div class="api"><code>objects()</code>, <code>objectCount()</code>,
<code>objectAt(index)</code></div>
<div class="api"><code>addObject(object)</code>,
<code>removeObject(object)</code></div>
<h3>MapObject</h3>
<div class="api"><code>MapObject:new(name, type, x, y, width, height)</code></div>
<div class="api"><code>name()</code>, <code>setName(name)</code>,
<code>type()</code>, <code>setType(type)</code></div>
<div class="api"><code>bounds()</code>,
<code>setBounds(x, y, width, height)</code></div>
<div class="api"><code>shape()</code> — <code>"rectangle"</code>,
<code>"polygon"</code>, or <code>"polyline"</code>.</div>
<div class="api"><code>property(name)</code>,
<code>setProperty(name, value)</code>,
<code>removeProperty(name)</code>,
<code>propertyNames()</code></div>
<div class="warning">
The current Lua constructor creates rectangular objects. Existing polygon
and polyline objects can be inspected, named, typed, moved, resized and
given properties, but the binding does not expose their vertex arrays.
</div>
<h2 id="api-bmp">API reference: BMP data and rules</h2>
<h3>MapBmp</h3>
<div class="api"><code>pixel(x, y)</code> — packed RGB integer;
compare it with <code>rgb(r,g,b).pixel</code>.</div>
<div class="api"><code>setPixel(x, y, color)</code>,
<code>rand(x, y)</code></div>
<div class="api"><code>erase(x, y, width, height)</code>,
<code>erase(rect)</code>, <code>erase(region)</code>,
<code>erase()</code></div>
<div class="api"><code>fill(x, y, width, height, color)</code>,
<code>fill(rect, color)</code>, <code>fill(region, color)</code>,
<code>fill(color)</code></div>
<div class="api"><code>replace(oldColor, newColor)</code></div>
<h3>BmpAlias</h3>
<div class="api"><code>tiles()</code></div>
<h3>BmpRule</h3>
<div class="api"><code>label()</code>, <code>bmpIndex()</code>,
<code>color()</code>, <code>tiles()</code>, <code>layer()</code>,
<code>condition()</code></div>
<h3>BmpBlend</h3>
<div class="api"><code>layer()</code>, <code>mainTile()</code>,
<code>blendTile()</code>, <code>direction()</code>,
<code>exclude()</code></div>
<p>Directions are <code>BmpBlend.Unknown</code>, <code>N</code>,
<code>S</code>, <code>E</code>, <code>W</code>, <code>NW</code>,
<code>NE</code>, <code>SW</code>, and <code>SE</code>.</p>
<h3>MapNoBlend</h3>
<div class="api"><code>get(x, y)</code>,
<code>set(x, y, boolean)</code>, <code>set(region, boolean)</code></div>
<h2 id="api-basic">API reference: basic types</h2>
<h3>Tile and Tileset</h3>
<div class="api"><code>tile:id()</code>, <code>tile:tileset()</code>,
<code>tileset:name()</code></div>
<h3>Color</h3>
<div class="api"><code>Color:new()</code>,
<code>Color:new(r, g, b)</code>, <code>rgb(r, g, b)</code></div>
<div class="api">Fields: <code>r</code>, <code>g</code>, <code>b</code>,
<code>pixel</code>.</div>
<h3>Region</h3>
<div class="api"><code>Region:new()</code>, <code>boundingRect()</code>,
<code>rects()</code>, <code>rectCount()</code>,
<code>isEmpty()</code></div>
<div class="api"><code>unite(x, y, w, h)</code>,
<code>unite(rect)</code>, <code>unite(region)</code></div>
<div class="api"><code>intersect(x, y, w, h)</code>,
<code>intersect(rect)</code>, <code>intersect(region)</code>,
<code>intersected(rectOrRegion)</code></div>
<div class="api"><code>translated(dx, dy)</code>; region operators
<code>+</code> and <code>-</code> are also bound.</div>
<h3>Rect</h3>
<div class="api"><code>Rect:new(x, y, width, height)</code></div>
<div class="api"><code>x()</code>, <code>y()</code>,
<code>width()</code>, <code>height()</code>,
<code>left()</code>, <code>right()</code>,
<code>top()</code>, <code>bottom()</code></div>
<div class="api"><code>translate(dx, dy)</code>,
<code>translated(dx, dy)</code>, <code>intersects(other)</code></div>
<h2 id="api-tool">API reference: TileTool</h2>
<p>The global <code>self</code> in an interactive tool is a TileTool.</p>
<div class="api"><code>setCursorType(TileTool.None | TileTool.CurbTool | TileTool.EdgeTool)</code></div>
<div class="api"><code>currentLayer()</code>,
<code>applyChanges(undoText)</code>,
<code>dragged()</code></div>
<div class="api"><code>angle(x1, y1, x2, y2)</code></div>
<div class="api"><code>clearToolTiles()</code>,
<code>setToolTile(layerName, x, y, tile)</code>,
<code>setToolTile(layerName, region, tile)</code></div>
<div class="api"><code>clearToolNoBlends()</code>,
<code>setToolNoBlend(layerName, region, boolean)</code></div>
<div class="api"><code>clearDistanceIndicators()</code>,
<code>indicateDistance(x1, y1, x2, y2)</code></div>
<h2 id="api-perlin">API reference: Perlin</h2>
<div class="api"><code>Perlin:new()</code>,
<code>perlin(x, y, z)</code> — returns deterministic 3D Perlin noise.</div>
<p>See <code>lua/perlin_trees.lua</code> for a complete map-editing example.</p>
<h2 id="behavior">Detailed function behavior</h2>
<p>This section complements the signature list with return values, side
effects and practical limitations. “Recorded change” means that a current-map
batch script will place the change on TileZed's Undo stack after the script
completes. Interactive tools must additionally call
<code>self:applyChanges()</code>.</p>
<h3>Map construction, identity and dimensions</h3>
<table>
<thead><tr><th>Function</th><th>Returns / effect</th><th>Usage notes</th></tr></thead>
<tbody>
<tr>
<td><code>Map:new(orientation, width, height, tileWidth, tileHeight)</code></td>
<td>A new in-memory map.</td>
<td>Use this only when generating a separate TMX. The global
<code>map</code> already wraps the current document.</td>
</tr>
<tr>
<td><code>orientation()</code></td>
<td>A <code>Map.*</code> enum.</td>
<td>PZ maps normally use level-isometric geometry. Do not change
coordinate assumptions based only on the visual camera.</td>
</tr>
<tr>
<td><code>width()</code>, <code>height()</code></td>
<td>Map dimensions in tiles.</td>
<td>Valid tile/BMP coordinates range from zero through
<code>width - 1</code> and <code>height - 1</code>.</td>
</tr>
<tr>
<td><code>minLevel()</code>, <code>maxLevel()</code></td>
<td>The lowest and highest level found among layers.</td>
<td>Both return zero for a map containing no layer outside level 0.
Negative values identify basements.</td>
</tr>
<tr>
<td><code>cellX()</code>, <code>cellY()</code></td>
<td>Absolute WorldEd cell coordinates, or <code>-1</code>.</td>
<td>Coordinates are available when TileZed can associate the TMX with a
loaded WorldEd project. A standalone TMX has no world-cell identity.</td>
</tr>
</tbody>
</table>
<h3>Layer lookup and structure</h3>
<table>
<thead><tr><th>Function</th><th>Returns / effect</th><th>Usage notes</th></tr></thead>
<tbody>
<tr>
<td><code>layerCount()</code></td>
<td>Number of map layers.</td>
<td>Use <code>0 .. layerCount()-1</code> with
<code>layerAt()</code>.</td>
</tr>
<tr>
<td><code>layerAt(index)</code></td>
<td>A Layer, or <code>nil</code> outside the valid range.</td>
<td>The index is zero-based and follows the map's layer order.</td>
</tr>
<tr>
<td><code>layer(name)</code></td>
<td>Any layer matching the exact level-prefixed name.</td>
<td>Use names such as <code>0_Floor</code>,
<code>8_RoomDefs</code> and <code>-1_Walls</code>.</td>
</tr>
<tr>
<td><code>tileLayer(name)</code>, <code>objectLayer(name)</code></td>
<td>The requested typed layer, or <code>nil</code>.</td>
<td>These combine an exact lookup with a type check and are preferable
to calling <code>asTileLayer()</code> manually.</td>
</tr>
<tr>
<td><code>newTileLayer(name)</code>,
<code>newObjectLayer(name)</code></td>
<td>A detached new layer sized to the current map.</td>
<td>A numeric prefix is parsed as its level. The returned layer is not
part of the map until passed to <code>addLayer()</code> or
<code>insertLayer()</code>.</td>
</tr>
<tr>
<td><code>addLayer(layer)</code></td>
<td>Appends a detached or previously removed layer.</td>
<td>Adding the same layer object twice is ignored.</td>
</tr>
<tr>
<td><code>insertLayer(index, layer)</code></td>
<td>Inserts a layer and records a structural change.</td>
<td>The index is clamped to the valid insertion range, so a negative
index becomes the first position and a large index becomes the end.</td>
</tr>
<tr>
<td><code>removeLayer(index)</code></td>
<td>Removes the indexed layer and records a structural change.</td>
<td>An invalid index is ignored. Resolve the index immediately before
removal because earlier removals shift later indexes.</td>
</tr>
<tr>
<td><code>layer:name()</code></td>
<td>The base name without its level prefix.</td>
<td>A level-8 RoomDefs layer normally returns
<code>RoomDefs</code>.</td>
</tr>
<tr>
<td><code>layer:nameWithPrefix()</code></td>
<td>The canonical lookup name.</td>
<td>Prefer this value when comparing layers or passing a layer name to
another API function.</td>
</tr>
<tr>
<td><code>layer:type()</code></td>
<td>The layer type name.</td>
<td>Use the typed conversion functions for logic; the type string is
most useful for reports and diagnostics.</td>
</tr>
<tr>
<td><code>asTileLayer()</code>, <code>asObjectGroup()</code></td>
<td>A typed wrapper or <code>nil</code>.</td>
<td>These never convert a layer; they only test its existing type.</td>
</tr>
</tbody>
</table>
<h3>Tilesets and tile identity</h3>
<table>
<thead><tr><th>Function</th><th>Returns / effect</th><th>Usage notes</th></tr></thead>
<tbody>
<tr>
<td><code>tilesetCount()</code>,
<code>tilesetAt(index)</code></td>
<td>The map's referenced tileset count and a zero-based lookup.</td>
<td>This reports tilesets attached to the TMX wrapper, not every entry
in the global <code>Tilesets.txt</code> catalog.</td>
</tr>
<tr>
<td><code>tileset(name)</code></td>
<td>The exact named Tileset or <code>nil</code>.</td>
<td>Names are case-sensitive in the current binding. This is important
for future Linux compatibility.</td>
</tr>
<tr>
<td><code>addTileset(tileset)</code></td>
<td>Adds a tileset reference to the in-memory map.</td>
<td>Passing a tileset already attached to the map is ignored.</td>
</tr>
<tr>
<td><code>tile("tileset_name_id")</code></td>
<td>A Tile or <code>nil</code>.</td>
<td>The final underscore separates the numeric ID, so tileset names may
themselves contain underscores. Leading zeroes in the ID are accepted.
The tileset must be present in the preloaded catalog.</td>
</tr>
<tr>
<td><code>tile(tilesetName, id)</code></td>
<td>A Tile or <code>nil</code>.</td>
<td>Useful when the name and ID are already separate. The tileset must
be known to the map/catalog and available in the preloaded catalog. A
<code>1x</code> image is not required when a usable <code>2x</code> or
custom variant exists.</td>
</tr>
<tr>
<td><code>tile:id()</code></td>
<td>Numeric ID within the tile's tileset.</td>
<td>This is not a globally unique ID; combine it with
<code>tile:tileset():name()</code>.</td>
</tr>
<tr>
<td><code>tile:tileset()</code>,
<code>tileset:name()</code></td>
<td>The owning Tileset and its name.</td>
<td>These are read-only identity helpers.</td>
</tr>
<tr>
<td><code>noneTile()</code></td>
<td>The editor's special empty-tile sentinel.</td>
<td>Passing it to <code>setTile()</code> or a fill operation clears the
cell. <code>clearTile()</code> is clearer for a single coordinate.</td>
</tr>
<tr>
<td><code>replaceTilesByName(pairs)</code></td>
<td>Replaces semicolon-separated from/to pairs across all tile layers.</td>
<td>Example:
<code>"old_set_3;new_set_9;old_set_4;new_set_10"</code>.
The complete string must contain an even number of valid names. The
legacy function does not return an error object, so verify results.</td>
</tr>
</tbody>
</table>
<h3>TileLayer editing</h3>
<table>
<thead><tr><th>Function</th><th>Returns / effect</th><th>Usage notes</th></tr></thead>
<tbody>
<tr>
<td><code>level()</code></td>
<td>The layer's integer level.</td>
<td>Basement layers return negative values.</td>
</tr>
<tr>
<td><code>tileAt(x, y)</code></td>
<td>The Tile at a coordinate, or <code>nil</code>.</td>
<td><code>nil</code> may mean either an empty cell or an out-of-bounds
coordinate; validate coordinates when that distinction matters.</td>
</tr>
<tr>
<td><code>setTile(x, y, tile)</code></td>
<td>Records one changed tile.</td>
<td>Out-of-bounds writes are ignored. The special
<code>noneTile()</code> sentinel is converted to an empty cell.</td>
</tr>
<tr>
<td><code>clearTile(x, y)</code></td>
<td>Records one tile deletion.</td>
<td>Equivalent to setting an empty tile, but more explicit.</td>
</tr>
<tr>
<td><code>erase(...)</code></td>
<td>Clears a rectangle, Region or complete layer.</td>
<td>Input areas are clipped to the layer. The no-argument form can
create a very large Undo change.</td>
</tr>
<tr>
<td><code>fill(..., tile)</code></td>
<td>Fills a rectangle, Region or complete layer.</td>
<td>Region fills operate on every rectangle in the Region. Prefer a
selected Region when only part of a layer should change.</td>
</tr>
<tr>
<td><code>replaceTile(oldTile, newTile)</code></td>
<td>Returns true if at least one cell was replaced.</td>
<td>Only this TileLayer is scanned. Use
<code>noneTile()</code> as the new value to erase matches.</td>
</tr>
</tbody>
</table>
<h3>Selection and integer geometry</h3>
<table>
<thead><tr><th>Function</th><th>Returns / effect</th><th>Usage notes</th></tr></thead>
<tbody>
<tr>
<td><code>tileSelection()</code></td>
<td>A copy of the current TileZed selection as a Region.</td>
<td>It may contain multiple disjoint rectangles.</td>
</tr>
<tr>
<td><code>setTileSelection(region)</code></td>
<td>Records a new editor selection.</td>
<td>This changes selection state, not tile content.</td>
</tr>
<tr>
<td><code>Region:new()</code></td>
<td>An empty integer region.</td>
<td>Add rectangles with <code>unite()</code>.</td>
</tr>
<tr>
<td><code>boundingRect()</code></td>
<td>The smallest Rect covering the Region.</td>
<td>The bounding rectangle may include holes or space between disjoint
rectangles; use <code>rects()</code> for exact coverage.</td>
</tr>
<tr>
<td><code>rects()</code>, <code>rectCount()</code></td>
<td>A one-based Lua list of component Rects and its count.</td>
<td>Iterate the returned list with <code>ipairs()</code>.</td>
</tr>
<tr>
<td><code>isEmpty()</code></td>
<td>True when the Region contains no coordinate.</td>
<td>Check this before a selection-based destructive edit.</td>
</tr>
<tr>
<td><code>unite(...)</code></td>
<td>Mutates the Region by adding an area.</td>
<td>Overlapping rectangles are normalized by Qt's region engine.</td>
</tr>
<tr>
<td><code>intersect(...)</code></td>
<td>Mutates the Region, retaining only common area.</td>
<td>Use this to clip a selection to safe map bounds.</td>
</tr>
<tr>
<td><code>intersected(...)</code></td>
<td>A new Region; the original is unchanged.</td>
<td>Choose this form when the source Region is needed later.</td>
</tr>
<tr>
<td><code>translated(dx, dy)</code></td>
<td>A shifted copy of the Region.</td>
<td>Coordinates remain integers.</td>
</tr>
<tr>
<td><code>Rect:new(x, y, w, h)</code></td>
<td>An integer rectangle.</td>
<td>A non-positive width or height represents no useful tile area.</td>
</tr>
<tr>
<td><code>x/y/width/height</code></td>
<td>Rectangle origin and size.</td>
<td>The origin is the top-left tile coordinate.</td>
</tr>
<tr>
<td><code>left/right/top/bottom</code></td>
<td>Inclusive Qt rectangle edges.</td>
<td>For a width of 10, <code>right()</code> is
<code>left() + 9</code>.</td>
</tr>
<tr>
<td><code>translate(dx, dy)</code></td>
<td>Mutates the Rect.</td>
<td><code>translated()</code> returns a shifted copy instead.</td>
</tr>
<tr>
<td><code>intersects(other)</code></td>
<td>True when two Rects share at least one cell.</td>
<td>Touching just outside an inclusive edge is not an intersection.</td>
</tr>
</tbody>
</table>
<h3>Object Groups, RoomDefs and zone-like objects</h3>
<table>
<thead><tr><th>Function</th><th>Returns / effect</th><th>Usage notes</th></tr></thead>
<tbody>
<tr>
<td><code>ObjectGroup:new(name, x, y, w, h)</code></td>
<td>A detached Object Group.</td>
<td><code>map:newObjectLayer()</code> is usually easier because it uses
the current map dimensions and parses a level prefix.</td>
</tr>
<tr>
<td><code>color()</code>, <code>setColor(color)</code></td>
<td>Reads or records the Object Group display color.</td>
<td>This is editor metadata; it does not change object types or gameplay
semantics.</td>
</tr>
<tr>
<td><code>objects()</code></td>
<td>A one-based Lua list of current MapObjects.</td>
<td>The list includes newly added objects and excludes objects removed
earlier in the same script.</td>
</tr>
<tr>
<td><code>objectCount()</code>,
<code>objectAt(index)</code></td>
<td>Count and zero-based access.</td>
<td><code>objectAt()</code> returns <code>nil</code> outside the valid
range.</td>
</tr>
<tr>
<td><code>addObject(object)</code></td>
<td>Records an object insertion.</td>
<td>The same wrapper cannot be added twice to one group.</td>
</tr>
<tr>
<td><code>removeObject(object)</code></td>
<td>Records an object removal.</td>
<td>Removing an object that is not currently in the group is ignored.</td>
</tr>
<tr>
<td><code>MapObject:new(name, type, x, y, w, h)</code></td>
<td>A detached rectangular MapObject.</td>
<td>Coordinates and size are integer-aligned by the binding.</td>
</tr>
<tr>
<td><code>name()</code>, <code>setName(value)</code></td>
<td>Reads or records the object's logical name.</td>
<td>RoomDefs often use the name to join adjacent rectangles belonging
to one room.</td>
</tr>
<tr>
<td><code>type()</code>, <code>setType(value)</code></td>
<td>Reads or records the object's type string.</td>
<td>Type matching is exact. Use the spelling expected by the PZ tool or
game workflow consuming the object.</td>
</tr>
<tr>
<td><code>bounds()</code></td>
<td>An integer-aligned Rect around the object.</td>
<td>Fractional source coordinates are rounded outward by
<code>toAlignedRect()</code>.</td>
</tr>
<tr>
<td><code>setBounds(x, y, w, h)</code></td>
<td>Records position and size together.</td>
<td>For existing polygon/polyline objects this changes the object's
bounds, but the Lua API does not expose vertex editing.</td>
</tr>
<tr>
<td><code>shape()</code></td>
<td><code>"rectangle"</code>, <code>"polygon"</code> or
<code>"polyline"</code>.</td>
<td>Read-only in this binding.</td>
</tr>
<tr>
<td><code>property(name)</code></td>
<td>The string value, or an empty string when absent.</td>
<td>Use <code>propertyNames()</code> when absence must be distinguished
from an intentionally empty value.</td>
</tr>
<tr>
<td><code>setProperty(name, value)</code></td>
<td>Records a string property.</td>
<td>Both names and values are converted from UTF-8.</td>
</tr>
<tr>
<td><code>removeProperty(name)</code></td>
<td>Records removal of the named property.</td>
<td>Removing an absent property has no visible effect.</td>
</tr>
<tr>
<td><code>propertyNames()</code></td>
<td>A case-insensitively sorted one-based Lua list.</td>
<td>Useful for generic inspectors and migration scripts.</td>
</tr>
</tbody>
</table>
<h3>Map BMP data, aliases, rules and blends</h3>
<table>
<thead><tr><th>Function</th><th>Returns / effect</th><th>Usage notes</th></tr></thead>
<tbody>
<tr>
<td><code>bmp(0)</code>, <code>bmp(1)</code></td>
<td>Main and vegetation MapBmp wrappers.</td>
<td>Use only indexes 0 and 1. The images use map-tile coordinates.</td>