LuaDrawing.wiki
changeset 1389 b57ad8939f32
parent 1388 23cc8581c0da
child 1392 b2b22eeaf5c1
equal deleted inserted replaced
1388:23cc8581c0da 1389:b57ad8939f32
     1 #summary Drawing maps with Lua.
     1 #summary Some examples for drawing maps with Lua
     2 
     2 
     3 = Drawing maps with Lua (OUTDATED) =
     3 = Drawing maps with Lua =
     4 
     4 
     5 == Overview ==
     5 == Overview ==
     6 Starting in 0.9.18 it is possible to reliably use drawn map mode to draw maps with scripts.
     6 It is possible to reliably use drawn map mode to draw maps with Lua scripts.
     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.
     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.
     8 
     8 
     9 == Details ==
     9 == Preconditions ==
    10 First, a couple of convenience functions for drawing to the map.
    10 First of all, you must make sure that the global variable `MapGen` is set to `mgDrawn`. This is done in `onGameInit`.
    11 <code lang="lua">
       
    12 PointsBuffer = ''  -- A string to accumulate points in
       
    13 function AddPoint(x, y, width, erase)
       
    14     PointsBuffer = PointsBuffer .. string.char(band(x,0xff00) / 256 , band(x,0xff) , band(y,0xff00) / 256 , band(y,0xff))
       
    15     if width then
       
    16         width = bor(width,0x80)
       
    17         if erase then
       
    18             width = bor(width,0x40)
       
    19         end
       
    20         PointsBuffer = PointsBuffer .. string.char(width)
       
    21     else
       
    22         PointsBuffer = PointsBuffer .. string.char(0)
       
    23     end
       
    24     if #PointsBuffer > 245 then
       
    25         ParseCommand('draw '..PointsBuffer)
       
    26         PointsBuffer = ''
       
    27     end
       
    28 end
       
    29 function FlushPoints()
       
    30     if #PointsBuffer > 0 then
       
    31         ParseCommand('draw '..PointsBuffer)
       
    32         PointsBuffer = ''
       
    33     end
       
    34 end
       
    35 </code>
       
    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].
       
    37 
    11 
    38 `FlushPoints` writes out any values from `PointsBuffer` that have not already been sent to the engine.
    12 To draw maps, you only need 2 functions: `AddPoint` and `FlushPoints`. See LuaAPI for the explanation.
    39 It would be called at the end of a drawing session.
       
    40 
    13 
    41 A simple example below.
    14 You also should not forget to make use of `onPreviewInit` so the map preview is displayed correctly.
    42 
    15 
       
    16 == Cave ==
       
    17 This generates a simple cave map:
    43 <code lang="lua">
    18 <code lang="lua">
    44 function onGameInit()
    19 function onGameInit()
    45     MapGen = 2
    20     MapGen = mgDrawn
    46     TemplateFilter = 0
    21     TemplateFilter = 0
    47 
    22 
    48     AddPoint(100,100,10)
       
    49     AddPoint(2000,2000)
       
    50     AddPoint(2000,100,10)
       
    51     AddPoint(100,2000)
       
    52     AddPoint(1000,1000,63,true)
       
    53     AddPoint(1000,1000,20)
       
    54 
       
    55     for i = 63,2,-4 do
       
    56         AddPoint(2000,1000,i)
       
    57         AddPoint(2000,1000,i-2,true)
       
    58     end
       
    59 
       
    60     for i = 1,2000,50 do
       
    61         AddPoint(i+2000,2000,1)
       
    62         AddPoint(4000,2000-i)
       
    63     end
       
    64 
       
    65     AddPoint(2000,2000,1)
       
    66     AddPoint(4000,2000)
       
    67     AddPoint(4000,0,1)
       
    68     AddPoint(4000,2000)
       
    69 
       
    70     FlushPoints()
       
    71 end
       
    72 </code>
       
    73 The first set of `AddPoint`s draws a large X and erases the centre.
       
    74 The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
       
    75 The 2nd loop draws a web of lines and frames it using some final `AddPoint`s.
       
    76 
       
    77 <a href="http://m8y.org/hw/draw1.jpeg">screenshot here</a>
       
    78 
       
    79 Another brief example:
       
    80 <code lang="lua">
       
    81     for i = 200,2000,600 do
    23     for i = 200,2000,600 do
    82         AddPoint(1,i,63)
    24         AddPoint(1,i,63)
    83         AddPoint(4000,i)
    25         AddPoint(4000,i)
    84     end
    26     end
    85     for i = 500,3500,1000 do
    27     for i = 500,3500,1000 do
    86         AddPoint(i,1000,63,true)
    28         AddPoint(i,1000,63,true)
    87     end
    29     end
    88     FlushPoints()
    30     FlushPoints()
       
    31 end
       
    32 
       
    33 onPreviewInit = onGameInit
    89 </code>
    34 </code>
    90 This one fills the map with solid land, and draws 4 circular erased points in it.
    35 This one fills the map with solid land, and draws 4 circular erased points in it.
    91 
    36 
    92 <a href="http://m8y.org/hw/draw2.jpeg">screenshot here</a>
    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.
       
    38 
       
    39 <a href="http://m8y.org/hw/draw2.jpeg">Screenshot here!</a>
       
    40 
       
    41 == Concentric circles and more ==
       
    42 <code lang="lua">
       
    43 function onGameInit()
       
    44     MapGen = mgDrawn
       
    45     TemplateFilter = 0
       
    46 
       
    47     -- Cross
       
    48     AddPoint(100,100,10)
       
    49     AddPoint(2000,2000)
       
    50     AddPoint(2000,100,10)
       
    51     AddPoint(100,2000)
       
    52     AddPoint(1000,1000,63,true)
       
    53     AddPoint(1000,1000,20)
       
    54 
       
    55     -- Concentric circles
       
    56     for i = 63,2,-4 do
       
    57         AddPoint(2000,1000,i)
       
    58         AddPoint(2000,1000,i-2,true)
       
    59     end
       
    60 
       
    61     -- Web of lines
       
    62     for i = 1,2000,50 do
       
    63         AddPoint(i+2000,2000,1)
       
    64         AddPoint(4000,2000-i)
       
    65     end
       
    66     -- Web of lines, framing
       
    67     AddPoint(2000,2000,1)
       
    68     AddPoint(4000,2000)
       
    69     AddPoint(4000,0,1)
       
    70     AddPoint(4000,2000)
       
    71 
       
    72     FlushPoints()
       
    73 end
       
    74 
       
    75 onPreviewInit = onGameInit
       
    76 </code>
       
    77 The first set of `AddPoint` functions draws a large cross and erases the centre.
       
    78 The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
       
    79 The 2nd loop draws a web of lines and frames it using some final `AddPoint` calls.
       
    80 
       
    81 <a href="http://m8y.org/hw/draw1.jpeg">Screenshot here!</a>
       
    82 
       
    83 == More examples ==
       
    84 Check out the code for these built-in styles:
       
    85 
       
    86  * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/DiagonalMaze.lua DiagonalMaze]
       
    87  * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/ShoppaMap.lua ShoppaMap]
       
    88  * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua Tunnels]