LuaDrawing.wiki
author Wuzzy
Thu, 25 May 2023 13:04:05 +0000
changeset 2231 f00c578955f7
parent 1903 22f677d419ed
permissions -rw-r--r--
LuaEvents: remove version numbers

#summary Some examples for drawing maps with Lua

= Drawing maps with Lua =

== Overview ==
It is possible to reliably use drawn map mode to draw maps with Lua scripts.
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.

== Preconditions ==
First of all, you must make sure that the global variable `MapGen` is set to `mgDrawn`. This is done in `onGameInit`.

To draw maps, you only need to call 2 functions: `AddPoint` and `FlushPoints`. See [LuaAPI] for the explanation.

You also should not forget to make use of `onPreviewInit` so the map preview is displayed correctly.

== Cave ==
This generates a simple cave map:
<code lang="lua">
function onGameInit()
    MapGen = mgDrawn
    TemplateFilter = 0

    -- Fill map with solid land
    for i = 200,2000,600 do
        AddPoint(1,i,63)
        AddPoint(4000,i)
    end

    -- Punch holes
    for i = 500,3500,1000 do
        AddPoint(i,1000,63,true)
    end

    FlushPoints()
end

onPreviewInit = onGameInit
</code>
This one fills the map with solid land, and draws 4 circular erased points in it.

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.

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.

== Concentric circles and more ==
<code lang="lua">
function onGameInit()
    MapGen = mgDrawn
    TemplateFilter = 0

    -- Cross
    AddPoint(100,100,10)
    AddPoint(2000,2000)
    AddPoint(2000,100,10)
    AddPoint(100,2000)
    AddPoint(1000,1000,63,true)
    AddPoint(1000,1000,20)

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

    -- Web of lines
    for i = 1,2000,50 do
        AddPoint(i+2000,2000,1)
        AddPoint(4000,2000-i)
    end
    -- Web of lines, framing
    AddPoint(2000,2000,1)
    AddPoint(4000,2000)
    AddPoint(4000,0,1)
    AddPoint(4000,2000)

    FlushPoints()
end

onPreviewInit = onGameInit
</code>
The first set of `AddPoint` functions draws a large cross 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 `AddPoint` calls.

== More examples ==
Check out the code for these built-in styles:

 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/DiagonalMaze.lua DiagonalMaze]
 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/ShoppaMap.lua ShoppaMap]
 * [https://hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua Tunnels]