LuaDrawing.wiki
author kyberneticist@gmail.com
Fri, 19 Oct 2012 21:10:21 +0000
changeset 256 0b93b3ec3ebf
parent 255 18e1a94e1591
child 257 3d03892b7545
permissions -rw-r--r--
Edited wiki page LuaDrawing through web user interface.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     1
#summary Drawing maps with Lua.
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     2
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     3
= Drawing Maps With Lua =
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     4
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     5
Starting in 0.9.18 it is possible to reliably use drawn map mode to draw maps with scripts.
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
     6
A simple example is given below.  Note that Drawn maps use an area of 4096x2048
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     7
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     8
= Details =
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     9
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    10
First, a couple of convenience functions for drawing to the map.
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    11
<code lang="lua">
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    12
PointsBuffer = ''  -- A string to accumulate points in
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    13
function AddPoint(x, y, size, erase)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    14
    PointsBuffer = PointsBuffer .. string.char(band(x,0xff00) / 256 , band(x,0xff) , band(y,0xff00) / 256 , band(y,0xff))
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    15
    if size then
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    16
        size = bor(size,0x80)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    17
        if erase then
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    18
            size = bor(size,0x40)
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    19
        end
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    20
        PointsBuffer = PointsBuffer .. string.char(size)
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    21
    else
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    22
        PointsBuffer = PointsBuffer .. string.char(0)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    23
    end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    24
    if #PointsBuffer > 245 then
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    25
        ParseCommand('draw '..PointsBuffer)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    26
        PointsBuffer = ''
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    27
    end
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    28
end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    29
function FlushPoints()
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    30
    if #PointsBuffer > 0 then
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    31
        ParseCommand('draw '..PointsBuffer)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    32
        PointsBuffer = ''
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    33
    end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    34
end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    35
</code>
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    36
AddPoint takes an x and y location for the point, a size (required to start a line) and erase (whether the line is erasing from the map)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    37
FlushPoints writes out any values from PointsBuffer that had not already been sent to the engine.  It would be called at the end of a drawing session.
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    38
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    39
A simple example below.
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    40
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    41
<code lang="lua">
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    42
function onGameInit()
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    43
    MapGen = 2
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    44
    TemplateFilter = 0
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    45
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    46
    AddPoint(100,100,10)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    47
    AddPoint(2000,2000)
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    48
    AddPoint(2000,100,10)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    49
    AddPoint(100,2000)
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    50
    AddPoint(1000,1000,63,true)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    51
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    52
    for i = 63,2,-4 do
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    53
        AddPoint(2000,1000,i)
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    54
        AddPoint(2000,1000,i-2,true)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    55
    end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    56
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    57
    for i = 1,2000,50 do
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    58
        AddPoint(i+2000,2000,1)
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    59
        AddPoint(4000,2000-i)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    60
    end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    61
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    62
    AddPoint(2000,2000,1)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    63
    AddPoint(4000,2000)
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    64
    AddPoint(4000,0,1)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    65
    AddPoint(4000,2000)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    66
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    67
    FlushPoints()
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    68
end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    69
</code>
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    70
The first set of AddPoints draws a large X and erases the centre.
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    71
The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    72
The 2nd loop draws a web of lines and frames it using some final AddPoints.