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