LuaDrawing.wiki
author Wuzzy
Thu, 03 May 2018 00:44:13 +0100
changeset 1388 23cc8581c0da
parent 743 d22bb25c55ac
child 1389 b57ad8939f32
permissions -rw-r--r--
LuaDrawing: formatting fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
     1
#summary Drawing maps with Lua.
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
     2
743
d22bb25c55ac LuaDrawing: Update headers
Wuzzy
parents: 597
diff changeset
     3
= Drawing maps with Lua (OUTDATED) =
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
     4
743
d22bb25c55ac LuaDrawing: Update headers
Wuzzy
parents: 597
diff changeset
     5
== Overview ==
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
     6
Starting in 0.9.18 it is possible to reliably use drawn map mode to draw maps with scripts.
1388
23cc8581c0da LuaDrawing: formatting fixes
Wuzzy
parents: 743
diff changeset
     7
A simple example is 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.
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
     8
743
d22bb25c55ac LuaDrawing: Update headers
Wuzzy
parents: 597
diff changeset
     9
== Details ==
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    10
First, a couple of convenience functions for drawing to the map.
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    11
<code lang="lua">
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    12
PointsBuffer = ''  -- A string to accumulate points in
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    13
function AddPoint(x, y, width, erase)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    14
    PointsBuffer = PointsBuffer .. string.char(band(x,0xff00) / 256 , band(x,0xff) , band(y,0xff00) / 256 , band(y,0xff))
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    15
    if width then
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    16
        width = bor(width,0x80)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    17
        if erase then
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    18
            width = bor(width,0x40)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    19
        end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    20
        PointsBuffer = PointsBuffer .. string.char(width)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    21
    else
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    22
        PointsBuffer = PointsBuffer .. string.char(0)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    23
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    24
    if #PointsBuffer > 245 then
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    25
        ParseCommand('draw '..PointsBuffer)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    26
        PointsBuffer = ''
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    27
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    28
end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    29
function FlushPoints()
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    30
    if #PointsBuffer > 0 then
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    31
        ParseCommand('draw '..PointsBuffer)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    32
        PointsBuffer = ''
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    33
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    34
end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    35
</code>
1388
23cc8581c0da LuaDrawing: formatting fixes
Wuzzy
parents: 743
diff changeset
    36
`AddPoint` takes an x and y location for the point, a width (indicates the start of a new line) and `erase` (whether the line is erasing from the map). The width calculation is described in [DrawnMapFormat].
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    37
1388
23cc8581c0da LuaDrawing: formatting fixes
Wuzzy
parents: 743
diff changeset
    38
`FlushPoints` writes out any values from `PointsBuffer` that have not already been sent to the engine.
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    39
It would be called at the end of a drawing session.
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    40
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    41
A simple example below.
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    42
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    43
<code lang="lua">
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    44
function onGameInit()
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    45
    MapGen = 2
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    46
    TemplateFilter = 0
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    47
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    48
    AddPoint(100,100,10)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    49
    AddPoint(2000,2000)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    50
    AddPoint(2000,100,10)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    51
    AddPoint(100,2000)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    52
    AddPoint(1000,1000,63,true)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    53
    AddPoint(1000,1000,20)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    54
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    55
    for i = 63,2,-4 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    56
        AddPoint(2000,1000,i)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    57
        AddPoint(2000,1000,i-2,true)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    58
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    59
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    60
    for i = 1,2000,50 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    61
        AddPoint(i+2000,2000,1)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    62
        AddPoint(4000,2000-i)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    63
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    64
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    65
    AddPoint(2000,2000,1)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    66
    AddPoint(4000,2000)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    67
    AddPoint(4000,0,1)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    68
    AddPoint(4000,2000)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    69
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    70
    FlushPoints()
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    71
end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    72
</code>
1388
23cc8581c0da LuaDrawing: formatting fixes
Wuzzy
parents: 743
diff changeset
    73
The first set of `AddPoint`s draws a large X and erases the centre.
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    74
The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
1388
23cc8581c0da LuaDrawing: formatting fixes
Wuzzy
parents: 743
diff changeset
    75
The 2nd loop draws a web of lines and frames it using some final `AddPoint`s.
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    76
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    77
<a href="http://m8y.org/hw/draw1.jpeg">screenshot here</a>
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    78
1388
23cc8581c0da LuaDrawing: formatting fixes
Wuzzy
parents: 743
diff changeset
    79
Another brief example:
597
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    80
<code lang="lua">
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    81
    for i = 200,2000,600 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    82
        AddPoint(1,i,63)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    83
        AddPoint(4000,i)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    84
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    85
    for i = 500,3500,1000 do
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    86
        AddPoint(i,1000,63,true)
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    87
    end
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    88
    FlushPoints()
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    89
</code>
654a9b9b6aa5 Edited via web interface
sheepluva
parents: 596
diff changeset
    90
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
    91
261
4c759fad66cf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 260
diff changeset
    92
<a href="http://m8y.org/hw/draw2.jpeg">screenshot here</a>