LuaDrawing.wiki
changeset 254 a21261c77793
child 255 18e1a94e1591
equal deleted inserted replaced
253:d7db074d6efa 254:a21261c77793
       
     1 #summary Drawing maps with Lua.
       
     2 
       
     3 = Drawing Maps With Lua =
       
     4 
       
     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
       
     7 
       
     8 = Details =
       
     9 
       
    10 First, a couple of convenience functions for drawing to the map.
       
    11 <code lang="lua">
       
    12 PointsBuffer = ''  -- A string to accumulate points in
       
    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))
       
    15     if new then
       
    16 		size = bor(size,0x80)
       
    17         if erase then
       
    18 			size = bor(size,0x40)
       
    19 		end
       
    20 		PointsBuffer = PointsBuffer .. string.char(size)
       
    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, 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)
       
    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.
       
    38 
       
    39 A simple example below.
       
    40 
       
    41 <code lang="lua">
       
    42 function onGameInit()
       
    43 	MapGen = 2
       
    44 	TemplateFilter = 0
       
    45 
       
    46 	AddPoint(100,100,true,10)
       
    47 	AddPoint(2000,2000)
       
    48 	AddPoint(2000,100,true,10)
       
    49 	AddPoint(100,2000)
       
    50 	AddPoint(1000,1000,true,63,true)
       
    51 
       
    52     for i = 63,2,-4 do
       
    53 	AddPoint(2000,1000,true,i)
       
    54 	AddPoint(2000,1000,true,i-2,true)
       
    55     end
       
    56 
       
    57     for i = 1,2000,50 do
       
    58 	AddPoint(i+2000,2000,true,1)
       
    59 	AddPoint(4000,2000-i)
       
    60     end
       
    61 
       
    62     AddPoint(2000,2000,true,1)
       
    63 	AddPoint(4000,2000)
       
    64     AddPoint(4000,0,true,1)
       
    65 	AddPoint(4000,2000)
       
    66 
       
    67 	FlushPoints()
       
    68 end
       
    69 </code>
       
    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.
       
    72 The 2nd loop draws a web of lines and frames it using some final AddPoints.