LuaDrawing.wiki
author Wuzzy
Fri, 13 Jul 2018 04:42:32 +0100
changeset 1452 4883b31e9277
parent 1396 d0f79e828581
child 1903 22f677d419ed
permissions -rw-r--r--
WikiSyntax: Remove links to issues and revisions
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
<a href="http://m8y.org/hw/draw2.jpeg">Screenshot here!</a>
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    46
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    47
== Concentric circles and more ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    48
<code lang="lua">
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    49
function onGameInit()
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    50
    MapGen = mgDrawn
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    51
    TemplateFilter = 0
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    52
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    53
    -- Cross
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    54
    AddPoint(100,100,10)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    55
    AddPoint(2000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    56
    AddPoint(2000,100,10)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    57
    AddPoint(100,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    58
    AddPoint(1000,1000,63,true)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    59
    AddPoint(1000,1000,20)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    60
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    61
    -- Concentric circles
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    62
    for i = 63,2,-4 do
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    63
        AddPoint(2000,1000,i)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    64
        AddPoint(2000,1000,i-2,true)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    65
    end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    66
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    67
    -- Web of lines
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    68
    for i = 1,2000,50 do
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    69
        AddPoint(i+2000,2000,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    70
        AddPoint(4000,2000-i)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    71
    end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    72
    -- Web of lines, framing
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    73
    AddPoint(2000,2000,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
    AddPoint(4000,0,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    76
    AddPoint(4000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    77
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    78
    FlushPoints()
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    79
end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    80
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    81
onPreviewInit = onGameInit
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    82
</code>
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    83
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
    84
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
    85
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
    86
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    87
<a href="http://m8y.org/hw/draw1.jpeg">Screenshot here!</a>
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    88
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    89
== More examples ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    90
Check out the code for these built-in styles:
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    91
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    92
 * [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
    93
 * [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
    94
 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua Tunnels]