Advertisement
Guest User

Untitled

a guest
Jun 28th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1.  
  2. function generateGround()
  3.     -- some constants here
  4.     local minX = 0
  5.     local maxX = 100
  6.     local minY = 0
  7.     local maxY = 100
  8.     local defaultGroundTexture = "textures/tiles/ground"
  9.     local defaultAirTexture = "textures/tiles/stone"
  10.  
  11.     -- function start
  12.     local defaultElevationValue = (maxY + minY) / 2
  13.     local getNearestHigherPo2 = function(n) local val = 2; while val < n do val = val * 2; end; return val; end
  14.     local adaptedGroundWidth = getNearestHigherPo2(1 + maxX - minX)
  15.  
  16.     -- this table will contain, for each integral x coord, the height of the floor
  17.     local requestedFloorLevels = {}
  18.     for x = 0, adaptedGroundWidth, 1 do requestedFloorLevels[x] = defaultElevationValue; end        -- filling the table with default elevation
  19.  
  20.     -- loop that fills requestedFloorLevels
  21.     local step = adaptedGroundWidth / 2
  22.     while step >= 1 do
  23.         local stepLog = math.log(step)
  24.         for x = step, adaptedGroundWidth - 1, step * 2 do
  25.             local averageValue = (requestedFloorLevels[x - step] + requestedFloorLevels[x + step]) / 2
  26.             local elevationDelta = (math.random() - 0.5) * stepLog * 2
  27.             requestedFloorLevels[x] = averageValue + elevationDelta
  28.         end
  29.         step = step / 2
  30.     end
  31.  
  32.     -- loop that will create all entities
  33.     for x = 0, maxX - minX, 1 do
  34.         local realX = x + minX
  35.         local groundElevation = math.floor(requestedFloorLevels[realX] + 0.5)
  36.  
  37.         local leftGroundElevation = defaultElevationValue
  38.         if x ~= 0 then leftGroundElevation = math.floor(requestedFloorLevels[realX - 1] + 0.5); end
  39.         local rightGroundElevation = defaultElevationValue
  40.         if realX ~= maxX then rightGroundElevation = math.floor(requestedFloorLevels[realX + 1] + 0.5); end
  41.  
  42.         for y = minY, maxY, 1 do
  43.             local entity = Entities:new()
  44.             entity:add("position")
  45.             entity.position.x, entity.position.y = realX, y
  46.  
  47.             entity:add("tileDisplay")
  48.  
  49.             if y < groundElevation then
  50.                 entity.tileDisplay.textureBottom = defaultGroundTexture
  51.                 entity.tileDisplay.textureUp = entity.tileDisplay.textureBottom
  52.  
  53.             elseif y == groundElevation then
  54.  
  55.                 entity.tileDisplay.textureBottom = defaultGroundTexture
  56.                 if leftGroundElevation > groundElevation then
  57.                     entity.tileDisplay.textureUp = defaultAirTexture
  58.                     entity.tileDisplay.leftGoesUp = true
  59.                 elseif rightGroundElevation > groundElevation then
  60.                     entity.tileDisplay.textureUp = defaultAirTexture
  61.                     entity.tileDisplay.leftGoesUp = false
  62.                 else
  63.                     entity.tileDisplay.textureUp = entity.tileDisplay.textureBottom
  64.                 end
  65.  
  66.             else
  67.                 entity.tileDisplay.textureBottom = defaultAirTexture
  68.                 entity.tileDisplay.textureUp = defaultAirTexture
  69.             end
  70.         end
  71.     end
  72. end
  73.  
  74. generateGround()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement