GE Lua Documentation

Press F to search!

saveSVG

Definition


-- @/lua/ge/map.lua:2729

local function saveSVG(filename, includeLinks, includeNodes)
  local svg = require('libs/EzSVG/EzSVG')

  includeLinks = includeLinks ~= false -- Default to true if not provided
  includeNodes = includeNodes or false -- Default to false if not provided

  local terrain = scenetree.findObject(scenetree.findClassObjects('TerrainBlock')[1])
  local terrainPosition = vec3(terrain:getPosition())

  local m = map
  if not m or not next(m.nodes) then return end

  -- Calculate bounding box for all nodes
  local minX, minY, maxX, maxY = math.huge, math.huge, -math.huge, -math.huge
  for nid, n in pairs(m.nodes) do
    local pos = n.pos - terrainPosition
    minX = math.min(minX, pos.x)
    minY = math.min(minY, pos.y)
    maxX = math.max(maxX, pos.x)
    maxY = math.max(maxY, pos.y)
  end

  -- Add a margin for link widths
  local margin = 20
  minX = minX - margin
  minY = minY - margin
  maxX = maxX + margin
  maxY = maxY + margin

  -- Calculate SVG size
  local width = maxX - minX
  local height = maxY - minY

  local svgDoc = svg.Document(width, height, svg.gray(255))
  local lines = svg.Group()

  -- Draw links
  if includeLinks then
    for nid, n in pairs(m.nodes) do
      for lid, dif in pairs(n.links) do
        local p1 = n.pos - terrainPosition
        local p2 = m.nodes[lid].pos - terrainPosition

        -- TODO: add proper fading between some colors
        local typeColor = 'black'
        local d = dif
        if type(dif) == "table" then
          d = dif.drivability or 0
        end
        if d < 0.9 and d >= 0 then
          typeColor = svg.rgb(170, 68, 0) -- dirt road = brown
        end

        lines:add(svg.Polyline({p1.x - minX, maxY - p1.y, p2.x - minX, maxY - p2.y}, {
          fill = 'none',
          stroke = typeColor,
          stroke_width = n.radius * 2,
          stroke_opacity = 0.4,
        }))
      end
    end
    svgDoc:add(lines)
  end

  -- Draw nodes
  if includeNodes then
    local nodes = svg.Group()
    for nid, n in pairs(m.nodes) do
      local p = n.pos - terrainPosition
      nodes:add(svg.Circle(p.x - minX, maxY - p.y, n.radius, {
        fill = 'black',
        fill_opacity = 0.4,
        stroke = 'none',
      }))
    end
    svgDoc:add(nodes)
  end

  svgDoc:writeTo(filename or 'map.svg')
end

Callers