LuaDrawing.wiki
author Wuzzy
Thu, 03 May 2018 01:13:29 +0100
changeset 1390 815d9da643af
parent 1389 b57ad8939f32
child 1392 b2b22eeaf5c1
permissions -rw-r--r--
TableOfContents: Drawing maps is no longer outdated
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
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    12
To draw maps, you only need 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
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    23
    for i = 200,2000,600 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    24
        AddPoint(1,i,63)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    25
        AddPoint(4000,i)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    26
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    27
    for i = 500,3500,1000 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    28
        AddPoint(i,1000,63,true)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    29
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    30
    FlushPoints()
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    31
end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    32
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    33
onPreviewInit = onGameInit
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    34
</code>
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    35
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
    36
1389
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    37
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. The reason why `onPreviewInit` is provided is because it allows you to avoid unneccessary overhead in more complex Lua scripts.
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    38
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    39
<a href="http://m8y.org/hw/draw2.jpeg">Screenshot here!</a>
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    40
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    41
== Concentric circles and more ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    42
<code lang="lua">
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    43
function onGameInit()
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    44
    MapGen = mgDrawn
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    45
    TemplateFilter = 0
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
    -- Cross
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    48
    AddPoint(100,100,10)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    49
    AddPoint(2000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    50
    AddPoint(2000,100,10)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    51
    AddPoint(100,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    52
    AddPoint(1000,1000,63,true)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    53
    AddPoint(1000,1000,20)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    54
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    55
    -- Concentric circles
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    56
    for i = 63,2,-4 do
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    57
        AddPoint(2000,1000,i)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    58
        AddPoint(2000,1000,i-2,true)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    59
    end
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
    -- Web of lines
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    62
    for i = 1,2000,50 do
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    63
        AddPoint(i+2000,2000,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    64
        AddPoint(4000,2000-i)
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
    -- Web of lines, framing
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    67
    AddPoint(2000,2000,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    68
    AddPoint(4000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    69
    AddPoint(4000,0,1)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    70
    AddPoint(4000,2000)
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    71
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    72
    FlushPoints()
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    73
end
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    74
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    75
onPreviewInit = onGameInit
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    76
</code>
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    77
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
    78
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
    79
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
    80
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    81
<a href="http://m8y.org/hw/draw1.jpeg">Screenshot here!</a>
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    82
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    83
== More examples ==
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    84
Check out the code for these built-in styles:
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    85
b57ad8939f32 LuaDrawing: complete update for modern HW
Wuzzy
parents: 1388
diff changeset
    86
 * [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
    87
 * [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
    88
 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua Tunnels]