author | Wuzzy |
Wed, 02 May 2018 22:51:10 +0100 | |
changeset 1339 | 0b7f8de0843c |
parent 743 | d22bb25c55ac |
child 1388 | 23cc8581c0da |
permissions | -rw-r--r-- |
597 | 1 |
#summary Drawing maps with Lua. |
2 |
||
743 | 3 |
= Drawing maps with Lua (OUTDATED) = |
597 | 4 |
|
743 | 5 |
== Overview == |
597 | 6 |
Starting in 0.9.18 it is possible to reliably use drawn map mode to draw maps with scripts. |
7 |
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. |
|
8 |
||
743 | 9 |
== Details == |
597 | 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, 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 |
||
38 |
FlushPoints writes out any values from PointsBuffer that have not already been sent to the engine. |
|
39 |
It would be called at the end of a drawing session. |
|
40 |
||
41 |
A simple example below. |
|
42 |
||
43 |
<code lang="lua"> |
|
44 |
function onGameInit() |
|
45 |
MapGen = 2 |
|
46 |
TemplateFilter = 0 |
|
47 |
||
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 AddPoints 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 AddPoints. |
|
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 |
|
82 |
AddPoint(1,i,63) |
|
83 |
AddPoint(4000,i) |
|
84 |
end |
|
85 |
for i = 500,3500,1000 do |
|
86 |
AddPoint(i,1000,63,true) |
|
87 |
end |
|
88 |
FlushPoints() |
|
89 |
</code> |
|
90 |
This one fills the map with solid land, and draws 4 circular erased points in it. |
|
91 |
||
261
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> |