LuaDrawing.wiki
author Wuzzy
Sat, 12 Oct 2019 16:39:38 +0100
changeset 2036 dedce712a325
parent 1903 22f677d419ed
permissions -rw-r--r--
Sounds: Add a couple of new sounds
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
     1
#summary Some examples for drawing maps with Lua
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
     2
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
     3
= Drawing maps with Lua =
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
     4
743
d22bb25c55ac LuaDrawing: Update headers
Wuzzy
parents: 597
diff changeset
     5
== Overview ==
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
     6
It is possible to reliably use drawn map mode to draw maps with Lua scripts.
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
     7
Simple examples are given below. Note that drawn maps use an area of 4096×2048. The examples below are static, but obviously this could be used for a random map variation—for example, simulating the Cave map by doing the fill below, then drawing random tunnels using circles that shift their course smoothly.
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
     8
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
     9
== Preconditions ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    10
First of all, you must make sure that the global variable `MapGen` is set to `mgDrawn`. This is done in `onGameInit`.
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    11
1396
d0f79e828581 LuaDrawing: call functions
Wuzzy
parents: 1395
diff changeset
    12
To draw maps, you only need to call 2 functions: `AddPoint` and `FlushPoints`. See [LuaAPI] for the explanation.
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    13
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    14
You also should not forget to make use of `onPreviewInit` so the map preview is displayed correctly.
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    15
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    16
== Cave ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    17
This generates a simple cave map:
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    18
<code lang="lua">
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    19
function onGameInit()
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    20
    MapGen = mgDrawn
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    21
    TemplateFilter = 0
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    22
1393
0b22af910c31 LuaDrawing: comments
Wuzzy
parents: 1392
diff changeset
    23
    -- Fill map with solid land
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    24
    for i = 200,2000,600 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    25
        AddPoint(1,i,63)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    26
        AddPoint(4000,i)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    27
    end
1393
0b22af910c31 LuaDrawing: comments
Wuzzy
parents: 1392
diff changeset
    28
0b22af910c31 LuaDrawing: comments
Wuzzy
parents: 1392
diff changeset
    29
    -- Punch holes
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    30
    for i = 500,3500,1000 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    31
        AddPoint(i,1000,63,true)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    32
    end
1393
0b22af910c31 LuaDrawing: comments
Wuzzy
parents: 1392
diff changeset
    33
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    34
    FlushPoints()
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    35
end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    36
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    37
onPreviewInit = onGameInit
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    38
</code>
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    39
This one fills the map with solid land, and draws 4 circular erased points in it.
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    40
1392
b2b22eeaf5c1 LuaDrawing: Edited via web interface
Wuzzy
parents: 1389
diff changeset
    41
The final line `onPreviewInit = onGameInit` makes sure the map preview works properly. This is the simplest way to create the preview and is a perfectly reasonable approach in this case, because the code is very simple here.
b2b22eeaf5c1 LuaDrawing: Edited via web interface
Wuzzy
parents: 1389
diff changeset
    42
b2b22eeaf5c1 LuaDrawing: Edited via web interface
Wuzzy
parents: 1389
diff changeset
    43
Note: The reason why `onPreviewInit` is provided in the official Lua API is because it allows you to avoid unneccessary overhead in more complex Lua scripts.
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    44
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    45
== Concentric circles and more ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    46
<code lang="lua">
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    47
function onGameInit()
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    48
    MapGen = mgDrawn
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    49
    TemplateFilter = 0
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    50
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    51
    -- Cross
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    52
    AddPoint(100,100,10)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    53
    AddPoint(2000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    54
    AddPoint(2000,100,10)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    55
    AddPoint(100,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    56
    AddPoint(1000,1000,63,true)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    57
    AddPoint(1000,1000,20)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    58
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    59
    -- Concentric circles
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    60
    for i = 63,2,-4 do
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    61
        AddPoint(2000,1000,i)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    62
        AddPoint(2000,1000,i-2,true)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    63
    end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    64
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    65
    -- Web of lines
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    66
    for i = 1,2000,50 do
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    67
        AddPoint(i+2000,2000,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    68
        AddPoint(4000,2000-i)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    69
    end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    70
    -- Web of lines, framing
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    71
    AddPoint(2000,2000,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    72
    AddPoint(4000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    73
    AddPoint(4000,0,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    74
    AddPoint(4000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    75
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    76
    FlushPoints()
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    77
end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    78
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    79
onPreviewInit = onGameInit
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    80
</code>
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    81
The first set of `AddPoint` functions draws a large cross and erases the centre.
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    82
The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    83
The 2nd loop draws a web of lines and frames it using some final `AddPoint` calls.
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    84
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    85
== More examples ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    86
Check out the code for these built-in styles:
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    87
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    88
 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/DiagonalMaze.lua DiagonalMaze]
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    89
 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/ShoppaMap.lua ShoppaMap]
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    90
 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua Tunnels]