Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Noise functions
- Noise = {
- SEED = nil,
- PERIOD = 40,
- NUMBER_OF_VERTICES = 4,
- MAX_HEIGHT = 400,
- INTERPOLATION_TYPE = 1,
- generatedNoise = {},
- genNoise = function(maxVertices)
- math.random()
- for i = 0, maxVertices do
- table.insert(Noise.generatedNoise, math.random())
- end
- end,
- getInterpolatedNoiseAt = function(x)
- if (x < 1.0 or x >= Noise.PERIOD * Noise.NUMBER_OF_VERTICES) then return 0 end
- local x1 = math.floor(x / Noise.PERIOD) + 1
- local x2 = math.ceil(x / Noise.PERIOD) + 1
- local y1 = Noise.generatedNoise[x1] * Noise.MAX_HEIGHT
- local y2 = Noise.generatedNoise[x2] * Noise.MAX_HEIGHT
- x1 = x1 * Noise.PERIOD * 10
- x2 = x2 * Noise.PERIOD * 10
- x = x * 10
- local value = nil
- if (Noise.INTERPOLATION_TYPE == 0) then
- value = ((y2 - y1) / (x2 - x1)) * (x - x1) + y2 -- linear interpolation
- elseif (Noise.INTERPOLATION_TYPE == 1) then
- value = -(0.5 * (y2 - y1)) * math.sin(math.pi/(x2 - x1)*(x - x1) + 4.72)+(y1 + 0.5 * (y2 - y1)) -- sine interpolation
- -- add extra elseif's here for different types of interpolation values you may want
- else
- value = 0/0 -- no interpolation
- end
- if (value ~= value) then return y1 else return value end
- end,
- init = function()
- if (Noise.SEED == nil) then Noise.SEED = os.time() end
- math.randomseed(Noise.SEED)
- Noise.genNoise(Noise.NUMBER_OF_VERTICES)
- end
- }
- -- Map manipulation
- function newMap()
- Noise.init()
- local mapXML = '<C><P L="1600" /><Z><S>'
- for i = 1, (Noise.NUMBER_OF_VERTICES * Noise.PERIOD) do
- local height = Noise.getInterpolatedNoiseAt(i)
- 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" />'
- end
- mapXML = mapXML .. '</S><D><DS Y="50" X="50" /></D><O /></Z></C>'
- tfm.exec.newGame(mapXML)
- end
- -- chat and such
- system.disableChatCommandDisplay('seed')
- function eventChatCommand(name, cmd)
- if (cmd == 'seed') then
- print('Seed for this terrain : ' .. Noise.SEED)
- end
- end
- newMap()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement