LuaDrawing.wiki
author kyberneticist@gmail.com
Fri, 19 Oct 2012 20:58:54 +0000
changeset 254 a21261c77793
child 255 18e1a94e1591
permissions -rw-r--r--
Created wiki page through web user interface.

#summary Drawing maps with Lua.

= Drawing Maps With Lua =

Starting in 0.9.18 it is possible to reliably use drawn map mode to draw maps with scripts.
A simple example is given below

= Details =

First, a couple of convenience functions for drawing to the map.
<code lang="lua">
PointsBuffer = ''  -- A string to accumulate points in
function AddPoint(x, y, new, size, erase)
    PointsBuffer = PointsBuffer .. string.char(band(x,0xff00) / 256 , band(x,0xff) , band(y,0xff00) / 256 , band(y,0xff))
    if new then
		size = bor(size,0x80)
        if erase then
			size = bor(size,0x40)
		end
		PointsBuffer = PointsBuffer .. string.char(size)
	else
		PointsBuffer = PointsBuffer .. string.char(0)
    end
    if #PointsBuffer > 245 then
		ParseCommand('draw '..PointsBuffer)
        PointsBuffer = ''
	end
end
function FlushPoints()
    if #PointsBuffer > 0 then
		ParseCommand('draw '..PointsBuffer)
        PointsBuffer = ''
    end
end
</code>
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)
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.

A simple example below.

<code lang="lua">
function onGameInit()
	MapGen = 2
	TemplateFilter = 0

	AddPoint(100,100,true,10)
	AddPoint(2000,2000)
	AddPoint(2000,100,true,10)
	AddPoint(100,2000)
	AddPoint(1000,1000,true,63,true)

    for i = 63,2,-4 do
	AddPoint(2000,1000,true,i)
	AddPoint(2000,1000,true,i-2,true)
    end

    for i = 1,2000,50 do
	AddPoint(i+2000,2000,true,1)
	AddPoint(4000,2000-i)
    end

    AddPoint(2000,2000,true,1)
	AddPoint(4000,2000)
    AddPoint(4000,0,true,1)
	AddPoint(4000,2000)

	FlushPoints()
end
</code>
The first set of AddPoints draws a large X and erases the centre.
The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
The 2nd loop draws a web of lines and frames it using some final AddPoints.