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 |
|
1396
|
12 |
To draw maps, you only need to call 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 |
|
1393
|
23 |
-- Fill map with solid land
|
597
|
24 |
for i = 200,2000,600 do
|
|
25 |
AddPoint(1,i,63)
|
|
26 |
AddPoint(4000,i)
|
|
27 |
end
|
1393
|
28 |
|
|
29 |
-- Punch holes
|
597
|
30 |
for i = 500,3500,1000 do
|
|
31 |
AddPoint(i,1000,63,true)
|
|
32 |
end
|
1393
|
33 |
|
597
|
34 |
FlushPoints()
|
1389
|
35 |
end
|
|
36 |
|
|
37 |
onPreviewInit = onGameInit
|
597
|
38 |
</code>
|
|
39 |
This one fills the map with solid land, and draws 4 circular erased points in it.
|
|
40 |
|
1392
|
41 |
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.
|
|
42 |
|
|
43 |
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
|
44 |
|
|
45 |
== Concentric circles and more ==
|
|
46 |
<code lang="lua">
|
|
47 |
function onGameInit()
|
|
48 |
MapGen = mgDrawn
|
|
49 |
TemplateFilter = 0
|
|
50 |
|
|
51 |
-- Cross
|
|
52 |
AddPoint(100,100,10)
|
|
53 |
AddPoint(2000,2000)
|
|
54 |
AddPoint(2000,100,10)
|
|
55 |
AddPoint(100,2000)
|
|
56 |
AddPoint(1000,1000,63,true)
|
|
57 |
AddPoint(1000,1000,20)
|
|
58 |
|
|
59 |
-- Concentric circles
|
|
60 |
for i = 63,2,-4 do
|
|
61 |
AddPoint(2000,1000,i)
|
|
62 |
AddPoint(2000,1000,i-2,true)
|
|
63 |
end
|
|
64 |
|
|
65 |
-- Web of lines
|
|
66 |
for i = 1,2000,50 do
|
|
67 |
AddPoint(i+2000,2000,1)
|
|
68 |
AddPoint(4000,2000-i)
|
|
69 |
end
|
|
70 |
-- Web of lines, framing
|
|
71 |
AddPoint(2000,2000,1)
|
|
72 |
AddPoint(4000,2000)
|
|
73 |
AddPoint(4000,0,1)
|
|
74 |
AddPoint(4000,2000)
|
|
75 |
|
|
76 |
FlushPoints()
|
|
77 |
end
|
|
78 |
|
|
79 |
onPreviewInit = onGameInit
|
|
80 |
</code>
|
|
81 |
The first set of `AddPoint` functions draws a large cross and erases the centre.
|
|
82 |
The following loop draws a set of nested points, alternating erasure and fill, which results in a set of concentric circles.
|
|
83 |
The 2nd loop draws a web of lines and frames it using some final `AddPoint` calls.
|
|
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] |