Advertisement
Itchyboy

New Random Hill Map

Oct 2nd, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. -- Noise functions
  2. seed=false
  3. Noise = {
  4.     SEED = nil,
  5.     PERIOD = 40,
  6.     NUMBER_OF_VERTICES = 4,
  7.     MAX_HEIGHT = 400,
  8.     INTERPOLATION_TYPE = 1,
  9.     generatedNoise = {},
  10.     genNoise =  function(maxVertices)
  11.                     math.random()
  12.                     for i = 0, maxVertices do
  13.                         table.insert(Noise.generatedNoise, math.random())
  14.                     end
  15.                 end,
  16.     getInterpolatedNoiseAt =    function(x)
  17.                                     if (x < 1.0 or x >= Noise.PERIOD * Noise.NUMBER_OF_VERTICES) then return 0 end
  18.                                    
  19.                                     local x1 = math.floor(x / Noise.PERIOD) + 1
  20.                                     local x2 = math.ceil(x / Noise.PERIOD) + 1
  21.                                     local y1 = Noise.generatedNoise[x1] * Noise.MAX_HEIGHT
  22.                                     local y2 = Noise.generatedNoise[x2] * Noise.MAX_HEIGHT
  23.                                     x1 = x1 * Noise.PERIOD * 10
  24.                                     x2 = x2 * Noise.PERIOD * 10
  25.                                     x = x * 10
  26.                                     local value = nil
  27.                                    
  28.                                     if (Noise.INTERPOLATION_TYPE == 0) then
  29.                                         value = ((y2 - y1) / (x2 - x1)) * (x - x1) + y2 -- linear interpolation
  30.                                     elseif (Noise.INTERPOLATION_TYPE == 1) then
  31.                                         value = -(0.5 * (y2 - y1)) * math.sin(math.pi/(x2 - x1)*(x - x1) + 4.72)+(y1 + 0.5 * (y2 - y1)) -- sine interpolation
  32.                                        
  33.                                     -- add extra elseif's here for different types of interpolation values you may want
  34.                                     else
  35.                                         value = 0/0 -- no interpolation
  36.                                     end
  37.                                    
  38.                                     if (value ~= value) then return y1 else return value end
  39.                                 end,
  40.     init =  function()
  41.                 if (Noise.SEED == nil) then Noise.SEED = os.time() end
  42.                 math.randomseed(Noise.SEED)
  43.  
  44.                 Noise.genNoise(Noise.NUMBER_OF_VERTICES)
  45.             end
  46. }
  47. -- Map manipulation
  48. function newMap()
  49.     Noise.init()
  50.    
  51.     local mapXML = '<C><P L="1600" /><Z><S>'
  52.    
  53.     for i = 1, (Noise.NUMBER_OF_VERTICES * Noise.PERIOD) do
  54.         local height = Noise.getInterpolatedNoiseAt(i)
  55.         mapXML = mapXML .. '<S L="10" H="' .. height .. '" X="' .. i * 10 .. '" Y="' .. 400 - (height / 2) .. '" T="6" P="0,0,0.3,0.2,0,0,0,0" />'
  56.     end
  57.    
  58.     mapXML = mapXML .. '</S><D><DS Y="50" X="50" /></D><O /></Z></C>'
  59.    
  60.     tfm.exec.newGame(mapXML)
  61. end
  62.  
  63. -- chat and such
  64. system.disableChatCommandDisplay('seed')
  65. system.disableChatCommandDisplay('newmap')
  66. function eventChatCommand(name, cmd)
  67. local args = {}
  68.         for i in string.gmatch(cmd, "%S+") do table.insert(args, i) end
  69.         if (table.getn(args) > 1) then
  70.     if ifargs[1]=='seed' then
  71.         print('Seed for this terrain : ' .. Noise.SEED)
  72.    elseif args[1]=='setseed' and args[2] then if args[2]~='random' then Noise.SEED=args[2] elseif args[2]=='random' then Noise.SEED=nil end
  73.    elseif args[1]=='newmap' then if not seed then Noise.SEED = nil end
  74.     Noise.PERIOD = 40
  75.     Noise.NUMBER_OF_VERTICES = 4
  76.     Noise.MAX_HEIGHT = 400
  77.     Noise.INTERPOLATION_TYPE = 1
  78.     Noise.generatedNoise = {}
  79.     Noise.genNoise =    function(maxVertices)
  80.                     math.random()
  81.                     for i = 0, maxVertices do
  82.                         table.insert(Noise.generatedNoise, math.random())
  83.                     end
  84.                 end
  85.     Noise.getInterpolatedNoiseAt =  function(x)
  86.                                     if (x < 1.0 or x >= Noise.PERIOD * Noise.NUMBER_OF_VERTICES) then return 0 end
  87.                                    
  88.                                     local x1 = math.floor(x / Noise.PERIOD) + 1
  89.                                     local x2 = math.ceil(x / Noise.PERIOD) + 1
  90.                                     local y1 = Noise.generatedNoise[x1] * Noise.MAX_HEIGHT
  91.                                     local y2 = Noise.generatedNoise[x2] * Noise.MAX_HEIGHT
  92.                                     x1 = x1 * Noise.PERIOD * 10
  93.                                     x2 = x2 * Noise.PERIOD * 10
  94.                                     x = x * 10
  95.                                     local value = nil
  96.                                    
  97.                                     if (Noise.INTERPOLATION_TYPE == 0) then
  98.                                         value = ((y2 - y1) / (x2 - x1)) * (x - x1) + y2 -- linear interpolation
  99.                                     elseif (Noise.INTERPOLATION_TYPE == 1) then
  100.                                         value = -(0.5 * (y2 - y1)) * math.sin(math.pi/(x2 - x1)*(x - x1) + 4.72)+(y1 + 0.5 * (y2 - y1)) -- sine interpolation
  101.                                        
  102.                                     -- add extra elseif's here for different types of interpolation values you may want
  103.                                     else
  104.                                         value = 0/0 -- no interpolation
  105.                                     end
  106.                                    
  107.                                     if (value ~= value) then return y1 else return value end
  108.                                 end
  109.     Noise.init =    function()
  110.                 if (Noise.SEED == nil) then Noise.SEED = os.time() end
  111.                 math.randomseed(Noise.SEED)
  112.  
  113.                 Noise.genNoise(Noise.NUMBER_OF_VERTICES)
  114.             end
  115.  newMap()
  116.     end
  117. end
  118.  
  119. newMap()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement