LuaDrawing.wiki
author almikes@aol.com
Sun, 25 Jan 2015 20:37:53 +0000
changeset 552 27add396065d
parent 378 4793d4b4e6be
child 595 d5647149658e
permissions -rw-r--r--
Update SetVisualGearValues
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.
269
3282541a4491 swap width/height
kyberneticist@gmail.com
parents: 262
diff changeset
     6
A simple example is given below.  Note that Drawn maps use an area of 4096x2048.  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.
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
258
9c815950568d Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 257
diff changeset
    13
function AddPoint(x, y, width, 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))
259
903c90df0e5d Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 258
diff changeset
    15
    if width then
378
4793d4b4e6be fix variable "width" being called "size" in 2 places of the code (I guess those were missed when changing variable name)
sheepyluva@gmail.com
parents: 269
diff changeset
    16
        width = bor(width,0x80)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    17
        if erase then
378
4793d4b4e6be fix variable "width" being called "size" in 2 places of the code (I guess those were missed when changing variable name)
sheepyluva@gmail.com
parents: 269
diff changeset
    18
            width = bor(width,0x40)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    19
        end
259
903c90df0e5d Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 258
diff changeset
    20
        PointsBuffer = PointsBuffer .. string.char(width)
255
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>
258
9c815950568d Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 257
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].
257
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    37
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    38
FlushPoints writes out any values from PointsBuffer that have not already been sent to the engine.
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    39
It would be called at the end of a drawing session.
254
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
A simple example below.
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    42
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    43
<code lang="lua">
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    44
function onGameInit()
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    45
    MapGen = 2
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    46
    TemplateFilter = 0
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    47
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    48
    AddPoint(100,100,10)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    49
    AddPoint(2000,2000)
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    50
    AddPoint(2000,100,10)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    51
    AddPoint(100,2000)
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    52
    AddPoint(1000,1000,63,true)
259
903c90df0e5d Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 258
diff changeset
    53
    AddPoint(1000,1000,20)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    54
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    55
    for i = 63,2,-4 do
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    56
        AddPoint(2000,1000,i)
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    57
        AddPoint(2000,1000,i-2,true)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    58
    end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    59
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    60
    for i = 1,2000,50 do
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    61
        AddPoint(i+2000,2000,1)
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    62
        AddPoint(4000,2000-i)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    63
    end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    64
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    65
    AddPoint(2000,2000,1)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    66
    AddPoint(4000,2000)
256
0b93b3ec3ebf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 255
diff changeset
    67
    AddPoint(4000,0,1)
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    68
    AddPoint(4000,2000)
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    69
255
18e1a94e1591 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 254
diff changeset
    70
    FlushPoints()
254
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    71
end
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    72
</code>
a21261c77793 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    73
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
    74
The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
257
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    75
The 2nd loop draws a web of lines and frames it using some final AddPoints.
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    76
261
4c759fad66cf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 260
diff changeset
    77
<a href="http://m8y.org/hw/draw1.jpeg">screenshot here</a>
4c759fad66cf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 260
diff changeset
    78
257
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    79
Another brief example.
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    80
<code lang="lua">
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    81
    for i = 200,2000,600 do
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    82
        AddPoint(1,i,63)
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    83
        AddPoint(4000,i)
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    84
    end
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    85
    for i = 500,3500,1000 do
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    86
        AddPoint(i,1000,63,true)
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    87
    end
258
9c815950568d Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 257
diff changeset
    88
    FlushPoints()
257
3d03892b7545 Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 256
diff changeset
    89
</code>
261
4c759fad66cf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 260
diff changeset
    90
This one fills the map with solid land, and draws 4 circular erased points in it.
4c759fad66cf Edited wiki page LuaDrawing through web user interface.
kyberneticist@gmail.com
parents: 260
diff changeset
    91
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>