Guest
Public paste!

Michael Craft

By: a guest | Nov 10th, 2009 | Syntax: Lua | Size: 2.06 KB | Hits: 122 | Expires: Never
Copy text to clipboard
  1. -- Include Gmod dummy functions for testing
  2. require('gmod')
  3.  
  4. -- Create the logic_case entities
  5. for y=1, 20 do
  6.         for x=1, 30 do
  7.                 --print('(' .. x .. ',' .. y .. ')')
  8.                 local ent = ents.create('logic_case')
  9.                 local kvtable = {
  10.                         ['Case01'] = x,
  11.                         ['Case02'] = y,
  12.                 }
  13.                 ent:SetKeyValues(kvtable)
  14.         end
  15. end
  16.  
  17. -- Real Code Starts Here
  18.  
  19. function getNodeTable()
  20.         -- Count up total X and Y
  21.         local totalX = 0
  22.         local totalY = 0
  23.         local entities = ents.FindByClass('logic_case')
  24.         for k,v in ipairs(entities) do
  25.                 local values = v:GetKeyValues()
  26.                 for key, value in pairs(values) do
  27.                         if key == 'Case01' then
  28.                                 if value > totalX then
  29.                                         totalX = value
  30.                                 end
  31.                         elseif key == 'Case02' then
  32.                                 if value > totalY then
  33.                                         totalY = value
  34.                                 end
  35.                         end
  36.                 end
  37.         end
  38.        
  39.         -- Init the node table
  40.         local nodeTable = {}
  41.         for i=1, totalX do
  42.                 nodeTable[i] = {} -- create a new row
  43.                 for j=1, totalY do
  44.                         nodeTable[i][j] = 0
  45.                 end
  46.         end
  47.        
  48.         -- Populate the node table
  49.         for k,v in ipairs(entities) do
  50.                 local values = v:GetKeyValues()
  51.                 local x = 0
  52.                 local y = 0
  53.                 for key, value in pairs(values) do
  54.                         if key == 'Case01' then
  55.                                 x = value
  56.                         elseif key == 'Case02' then
  57.                                 y = value
  58.                         end
  59.                 end
  60.                
  61.                 nodeTable[x][y] = v
  62.         end
  63.        
  64.         return nodeTable
  65. end
  66.  
  67. function generateTerrain(nodeTable)
  68.         math.randomseed(os.time())
  69.         local heights = { (math.random(1, #nodeTable[#nodeTable]))-5 }
  70.         for i=2, #nodeTable do
  71.                 local prev = heights[i-1]
  72.                 local height = math.random(prev-3, prev+3)
  73.                 if height > #nodeTable[#nodeTable] then
  74.                         height = #nodeTable[#nodeTable]
  75.                 elseif height < 1 then
  76.                         height = 1
  77.                 end
  78.                 table.insert(heights, height)
  79.         end
  80.        
  81.         for k,v in ipairs(heights) do -- iterate over X values (columns)
  82.                 for i=1, v do -- iterate over Y values (rows)
  83.                         nodeTable[k][i]:Spawn()
  84.                 end
  85.         end
  86. end
  87.  
  88. nodeTable = getNodeTable()
  89. generateTerrain(nodeTable)
  90.  
  91. -- Visiualisaton code for testing
  92. for y=20, 1, -1 do
  93.         for x=1, 30 do
  94.                 if nodeTable[x][y].spawned then
  95.                         io.write('X')
  96.                 else
  97.                         io.write('-')
  98.                 end
  99.         end
  100.         io.write('\n')
  101. end