1389
|
1 |
#summary Some examples for drawing maps with Lua
|
597
|
2 |
|
1389
|
3 |
= Drawing maps with Lua =
|
597
|
4 |
|
743
|
5 |
== Overview ==
|
1389
|
6 |
It is possible to reliably use drawn map mode to draw maps with Lua scripts.
|
|
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 |
|
|
9 |
== Preconditions ==
|
|
10 |
First of all, you must make sure that the global variable `MapGen` is set to `mgDrawn`. This is done in `onGameInit`.
|
597
|
11 |
|
1389
|
12 |
To draw maps, you only need 2 functions: `AddPoint` and `FlushPoints`. See LuaAPI for the explanation.
|
597
|
13 |
|
1389
|
14 |
You also should not forget to make use of `onPreviewInit` so the map preview is displayed correctly.
|
597
|
15 |
|
1389
|
16 |
== Cave ==
|
|
17 |
This generates a simple cave map:
|
597
|
18 |
<code lang="lua">
|
|
19 |
function onGameInit()
|
1389
|
20 |
MapGen = mgDrawn
|
597
|
21 |
TemplateFilter = 0
|
|
22 |
|
|
23 |
for i = 200,2000,600 do
|
|
24 |
AddPoint(1,i,63)
|
|
25 |
AddPoint(4000,i)
|
|
26 |
end
|
|
27 |
for i = 500,3500,1000 do
|
|
28 |
AddPoint(i,1000,63,true)
|
|
29 |
end
|
|
30 |
FlushPoints()
|
1389
|
31 |
end
|
|
32 |
|
|
33 |
onPreviewInit = onGameInit
|
597
|
34 |
</code>
|
|
35 |
This one fills the map with solid land, and draws 4 circular erased points in it.
|
|
36 |
|
1392
|
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.
|
|
38 |
|
|
39 |
Note: The reason why `onPreviewInit` is provided in the official Lua API is because it allows you to avoid unneccessary overhead in more complex Lua scripts.
|
1389
|
40 |
|
|
41 |
<a href="http://m8y.org/hw/draw2.jpeg">Screenshot here!</a>
|
|
42 |
|
|
43 |
== Concentric circles and more ==
|
|
44 |
<code lang="lua">
|
|
45 |
function onGameInit()
|
|
46 |
MapGen = mgDrawn
|
|
47 |
TemplateFilter = 0
|
|
48 |
|
|
49 |
-- Cross
|
|
50 |
AddPoint(100,100,10)
|
|
51 |
AddPoint(2000,2000)
|
|
52 |
AddPoint(2000,100,10)
|
|
53 |
AddPoint(100,2000)
|
|
54 |
AddPoint(1000,1000,63,true)
|
|
55 |
AddPoint(1000,1000,20)
|
|
56 |
|
|
57 |
-- Concentric circles
|
|
58 |
for i = 63,2,-4 do
|
|
59 |
AddPoint(2000,1000,i)
|
|
60 |
AddPoint(2000,1000,i-2,true)
|
|
61 |
end
|
|
62 |
|
|
63 |
-- Web of lines
|
|
64 |
for i = 1,2000,50 do
|
|
65 |
AddPoint(i+2000,2000,1)
|
|
66 |
AddPoint(4000,2000-i)
|
|
67 |
end
|
|
68 |
-- Web of lines, framing
|
|
69 |
AddPoint(2000,2000,1)
|
|
70 |
AddPoint(4000,2000)
|
|
71 |
AddPoint(4000,0,1)
|
|
72 |
AddPoint(4000,2000)
|
|
73 |
|
|
74 |
FlushPoints()
|
|
75 |
end
|
|
76 |
|
|
77 |
onPreviewInit = onGameInit
|
|
78 |
</code>
|
|
79 |
The first set of `AddPoint` functions draws a large cross and erases the centre.
|
|
80 |
The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
|
|
81 |
The 2nd loop draws a web of lines and frames it using some final `AddPoint` calls.
|
|
82 |
|
|
83 |
<a href="http://m8y.org/hw/draw1.jpeg">Screenshot here!</a>
|
|
84 |
|
|
85 |
== More examples ==
|
|
86 |
Check out the code for these built-in styles:
|
|
87 |
|
|
88 |
* [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/DiagonalMaze.lua DiagonalMaze]
|
|
89 |
* [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/ShoppaMap.lua ShoppaMap]
|
|
90 |
* [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua Tunnels] |