File: procedural_transform.lua

package info (click to toggle)
crawl 2%3A0.33.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,264 kB
  • sloc: cpp: 358,145; ansic: 27,203; javascript: 9,491; python: 8,359; perl: 3,327; java: 2,667; xml: 2,191; makefile: 1,830; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (80 lines) | stat: -rw-r--r-- 2,530 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
------------------------------------------------------------------------------
-- procedural_transform.lua: Complex transforms
--
------------------------------------------------------------------------------

crawl_require("dlua/layout/procedural.lua")

procedural.transform = {}

function procedural.transform.wrapped_cylinder(source,period,radius,zscale,zoff)

  if zscale == nil then zscale = 1 end
  if zoff == nil then zoff = 0 end

  return function(x,y)

    -- Convert x to rads
    local a = x * 2 * math.pi / period
    -- Extrude a circle along the y axis of the original function
    return source(math.sin(a) * radius, y, math.cos(a) * radius * zscale + x * zoff)

  end

end

-- Transforms a cartesian domain into a polar one. In other words, it takes
-- a rectangular shape and transforms it into a circular one, where y becomes
-- the radius, and x becomes the angle about the origin.
function procedural.transform.polar(source,radius,sx,sy)
  local frad = procedural.radial{}
  if sx == nil then sx = 1 end
  if sy == nil then sy = 1 end

  return function(x,y)

    local a = frad(x,y)
    local r = math.sqrt(x^2+y^2)/radius
    return source(a*sx,r*sy)

  end

end

-- Inverse of the polar transform
function procedural.transform.polar_inverse(source,radius,sx,sy)
  local frad = procedural.radial{}
  if sx == nil then sx = 1 end
  if sy == nil then sy = 1 end

  return function(x,y)

    local a = x / sx
    local r = radius * y / sy

    -- TODO: Check that this is properly the inverse of polar; it might have
    -- been flipped or rotated; although this doesn't matter slightly for current layouts
    return source(math.sin(a*2*math.pi)*r, math.cos(a*2*math.pi)*r)
  end

end

-- Distorts a function by an amount depending on another function - in this case its
-- proximity to the map border
function procedural.transform.damped_distortion(source,options)

    -- Apply distortion
    local fdsx = procedural.simplex3d { scale = util.random_range_real(0.1,1), unit = false }
    local fdsy = procedural.simplex3d { scale = util.random_range_real(0.1,1), unit = false }
    -- Distort more at the edge of the map
    -- TODO: Could look even better with a less linear easing function
    local fbox = procedural.box{}
    local fedge = function(x,y)
      return math.max(0,1-fbox(x,y))
    end
    local fdx = procedural.mul(fdsx,fedge)
    local fdy = procedural.mul(fdsy,fedge)

    return procedural.distort { source = source, scale = crawl.random_range(4,8), offsetx = fdx, offsety = fdy }

end