Advertisement
Guest User

Untitled

a guest
Mar 1st, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.45 KB | None | 0 0
  1.  
  2. print ("  =========================================== ")
  3.  
  4. base=wl.Editor()
  5. x_size=base.map.width
  6. y_size=base.map.height
  7.  
  8.  
  9.  
  10. inputfile='/home/tibor/devel/terrain-generator/import_coordinates_2.txt'
  11.  
  12.  
  13. -- see if the file exists
  14. function file_exists(file)
  15.     local f = io.open(file, "rb")
  16.     if f then
  17.         f:close()
  18.         return true
  19.     end
  20.     return false
  21.     end
  22.  
  23. function seeder_immovable(target, thing,whitelist,blacklist,minheight,maxheight)   
  24. --final count, immovable type, surface whitelist, surface blacklist
  25.     i=0
  26.     count=0
  27.     while i<target*100 do
  28.         x=math.random(x_size)-1
  29.         y=math.random(y_size)-1
  30.         f=base.map:get_field(x,y)
  31.         if f.immovable==nil and f.height > minheight and f.height<maxheight and table.contains(blacklist,f.terr)==false and table.contains(blacklist,f.tln.terd)==false then
  32.             if pcall(function() base.map:place_immovable(thing,f) end) then
  33.                 --print ("  placing "..thing.." iteration: "..i)
  34.                 count=count+1
  35.             else
  36.                 --print ("  placing "..thing.." failed, iteration: "..i)                   
  37.             end
  38.         end
  39.         if count>target then
  40.             i=i+100000
  41.         end
  42.         i=i+1
  43.     end
  44.     print (" Placed "..count.." (target: "..target..")  of "..thing.." @ seeder_immovable")
  45. end
  46.  
  47. function table.contains(table, element)
  48.   for _, value in pairs(table) do
  49.     if value == element then
  50.       return true
  51.     end
  52.   end
  53.   return false
  54. end
  55.  
  56. function cluster_immovable(target,thing,searchingfor,blacklist,minheight,maxheight,radius)
  57. --final count, immovable types,neighbours to search, blacklist of survaces, minheight, maxheight)
  58.     i=0
  59.     count=0
  60.     --for i, v in ipairs(searchingfor) do print("searching for "..i..": "..v) end
  61.  
  62.     while i<target*200 do
  63.         x=math.random(x_size)-1
  64.         y=math.random(y_size)-1
  65.         f=base.map:get_field(x,y)
  66.         if f.immovable==nil and f.height > minheight and f.height<maxheight and table.contains(blacklist,f.terr)==false and table.contains(blacklist,f.tln.terd)==false then
  67.             --print ("good place to build on")
  68.             nearspots=f:region(radius)
  69.             nearspotscount=table.getn(nearspots)
  70.             j=1
  71.             adding=false
  72.             while j<=nearspotscount do
  73.                 nearspot=nearspots[j]
  74.                
  75.                 if nearspot.immovable~=nill then
  76.                     --print ("  "..nearspot.immovable.name)
  77.                     if  table.contains(searchingfor,nearspot.immovable.name) then
  78.                         adding=true
  79.                         --print("  immovable found on near position")
  80.                     end
  81.                 end
  82.                 j=j+1
  83.             end
  84.             if adding==true then   
  85.                 if pcall(function() base.map:place_immovable(thing,f) end) then
  86.                     --print ("  placing immovable  cluster_immovable()")
  87.                     count=count+1
  88.                 --else
  89.                     --print ("  placing immovable failed  cluster_immovable()")                
  90.                 end
  91.             else
  92.                 --print(" Failed to find the particular immoveble in the neighbourd")
  93.             end
  94.         end
  95.         if count>target then
  96.             i=i+100000
  97.         end
  98.         i=i+1
  99.     end
  100.     --for i, v in ipairs(searchingfor) do print("searching for "..i..": "..v) end
  101.     print (" Placed "..count.." (target: "..target..") of "..thing.." @ cluster_immovable")
  102. end    
  103.  
  104.  
  105. function print_height_stat()
  106.     local x=0
  107.     local y
  108.     local low=0
  109.     local high=0
  110.     while x<x_size do
  111.         y=0
  112.         while y<y_size do  
  113.             f=base.map:get_field(x,y)
  114.             if f.height<20 then
  115.                 low=low+1
  116.             end
  117.             if f.height>=50 then
  118.                 high=high+1
  119.             end
  120.             y=y+1
  121.         end
  122.         x=x+1
  123.     end
  124.     print (" Height stat: below 20: "..low..", above/equal 50: "..high)
  125. end
  126.  
  127. function big_stat()
  128.     local x=0
  129.     local y
  130.     local count=0
  131.     local medium_count=0
  132.     while x<x_size do
  133.         y=0
  134.         while y<y_size do  
  135.             f=base.map:get_field(x,y)
  136.             if f:has_caps("big") then
  137.                 count=count+1
  138.             end
  139.             if f:has_caps("medium") then
  140.                 medium_count=medium_count+1
  141.             end
  142.             y=y+1
  143.         end
  144.         x=x+1
  145.     end
  146.     print (" Fields with big/medium buildings capabilities: "..count.."/"..tostring(medium_count))
  147. end
  148.  
  149. function mines_stat()
  150.     local x=0
  151.     local y
  152.     local count=0
  153.     while x<x_size do
  154.         y=0
  155.         while y<y_size do  
  156.             f=base.map:get_field(x,y)
  157.             if f:has_caps("mine") then
  158.                 count=count+1
  159.             end
  160.             y=y+1
  161.         end
  162.         x=x+1
  163.     end
  164.     print (" Fields with mine capabilities: "..count)
  165. end
  166.  
  167. function set_surface_if_not(f,newsurface,nochangesurface)
  168.     local changed=false
  169.     if f.terr~=newsurface and f.terr~=nochangesurface then
  170.         f.terr=newsurface
  171.         changed=true
  172.     end
  173.     if f.terd~=newsurface and f.terd~=nochangesurface then
  174.         f.terd=newsurface
  175.         changed=true
  176.     end
  177.     if f.ln.terr~=newsurface and f.ln.terr~=nochangesurface then
  178.         f.ln.terr=newsurface
  179.         changed=true
  180.     end
  181.     if f.tln.terr~=newsurface and f.tln.terr~=nochangesurface then
  182.         f.tln.terr=newsurface
  183.         changed=true
  184.     end
  185.     if f.tln.terd~=newsurface and f.tln.terd~=nochangesurface then
  186.         f.tln.terd=newsurface
  187.         changed=true
  188.     end
  189.     if f.trn.terd~=newsurface and f.trn.terd~=nochangesurface then
  190.         f.trn.terd=newsurface
  191.         changed=true
  192.     end
  193.     return changed
  194. end
  195.    
  196. function read_file(file)
  197.     lines = {}
  198.     for line in io.lines(file) do
  199.         lines[#lines + 1] = line
  200.     end
  201.     return lines
  202.     end
  203.  
  204. function is_water_around(field)
  205.     if f.terr=='wasser' then
  206.         return true
  207.     end
  208.     if f.terd=='wasser' then
  209.         return true
  210.     end
  211.     if f.ln.terr=='wasser' then
  212.         return true
  213.     end
  214.     if f.tln.terr=='wasser' then
  215.         return true
  216.     end
  217.     if f.tln.terd=='wasser' then
  218.         return true
  219.     end
  220.     if f.trn.terd=='wasser' then
  221.         return true
  222.     end
  223.     return false
  224. end
  225.  
  226. function split(s,re)
  227.     local i1 = 1
  228.     local ls = {}
  229.     local append = table.insert
  230.     if not re then re = '%s+' end
  231.         if re == '' then return {s} end
  232.             while true do
  233.             local i2,i3 = s:find(re,i1)
  234.             if not i2 then
  235.             local last = s:sub(i1)
  236.             if last ~= '' then append(ls,last) end
  237.             if #ls == 1 and ls[1] == '' then
  238.             return {}
  239.             else
  240.             return ls
  241.         end
  242.     end
  243.     append(ls,s:sub(i1,i2-1))
  244.     i1 = i3+1
  245.     end
  246. end
  247.  
  248. function smooth_surface(terraintype)
  249.  
  250.     local counter=0
  251.     local x,y
  252.     x=0
  253.     while x<x_size do
  254.         y=0
  255.        
  256.         while y<y_size do
  257.             f=base.map:get_field(x,y)
  258.             if f.terr~=terraintype then
  259.                 if f.terd==terraintype and f.trn.terd==terraintype then
  260.                     f.terr=terraintype
  261.                     counter=counter+1
  262.                     --print ( "  Addding ".. terraintype.." to triangle 1")
  263.                 end
  264.             end
  265.  
  266.             if f.terr~=terraintype then
  267.                 if f.terd==terraintype and f.rn.terd==terraintype then
  268.                     f.terr=terraintype
  269.                     counter=counter+1
  270.                     --print ( "  Addding ".. terraintype.." to triangle 1")
  271.                 end
  272.             end
  273.  
  274.             if f.terr~=terraintype then
  275.                 if f.trn.terd==terraintype and f.rn.terd==terraintype then
  276.                     f.terr=terraintype
  277.                     counter=counter+1
  278.                     --print ( "  Addding ".. terraintype.." to triangle 1")
  279.                 end
  280.             end
  281.  
  282.  
  283.  
  284.            
  285.             --f=base.map:get_field(x,y)
  286.             if f.terd~=terraintype then
  287.                 if f.bln.terr==terraintype and f.terr==terraintype then
  288.                     f.terd=terraintype
  289.                     counter=counter+1
  290.                     --print ( "  Addding ".. terraintype.." to triangle 2")
  291.                 end
  292.             end
  293.             if f.terd~=terraintype then
  294.                 if f.bln.terr==terraintype and f.ln.terr==terraintype then
  295.                     f.terd=terraintype
  296.                     counter=counter+1
  297.                     --print ( "  Addding ".. terraintype.." to triangle 2")
  298.                 end
  299.             end
  300.             if f.terd~=terraintype then
  301.                 if f.ln.terr==terraintype and f.terr==terraintype then
  302.                     f.terd=terraintype
  303.                     counter=counter+1
  304.                     --print ( "  Addding ".. terraintype.." to triangle 2")
  305.                 end
  306.             end
  307.             y=y+1
  308.         end
  309.        
  310.                
  311.        
  312.         x=x+1
  313.     end
  314.     return counter
  315. end
  316.  
  317. if not (file_exists(inputfile)) then
  318.     print (" Input file missing")
  319.     exit()
  320.     end
  321.  
  322.  
  323. print (" Zerroing terrain...")
  324. x=0
  325. while x<x_size do
  326.     y=0
  327.     while y<y_size do
  328.         f=base.map:get_field(x,y)
  329.         f.height=10
  330.         f.terr='steppe'
  331.         f.terd='steppe'
  332.         if f.immovable then
  333.             f.immovable:remove()
  334.         end
  335.         y=y+1
  336.     end
  337.     x=x+1
  338. end
  339.    
  340.    
  341. content=read_file(inputfile)
  342.  
  343.  
  344. print (" importing&applying height data")
  345. for k,v in pairs(lines) do
  346.     --print(v)
  347.     values=split(v)
  348.     --print ("a")
  349.     x=values[1]
  350.     y=values[2]
  351.     height=values[3]
  352.     --print (values[1])
  353.    
  354.     f=base.map:get_field(x,y)
  355.     if f.height~=height then
  356.         f.height=height
  357.     end
  358.     end
  359.  
  360. print (" setting terrain types")
  361. --setting terrain types
  362. x=0
  363. while x<x_size do
  364.     y=0
  365.     while y<y_size do  
  366.         f=base.map:get_field(x,y)
  367.         if f.height>51 then
  368.             f.terr='schnee'
  369.             f.terd='schnee'
  370.             --print ("snow")
  371.         elseif f.height>43 then
  372.             f.terr='berg4'
  373.             f.terd='berg4'
  374.         elseif f.height>34 then
  375.             f.terr='bergwiese'
  376.             f.terd='bergwiese'
  377.         elseif f.height>=21 then
  378.             f.terr='steppe'
  379.             f.terd='steppe'
  380.             ----print ("snow")
  381.         elseif f.height>0 then
  382.             f.terr='wiese2'
  383.             f.terd='wiese2'
  384.         else
  385.             f.terr='wasser'
  386.             f.terd='wasser'    
  387.         end
  388.         y=y+1
  389.     end
  390.     x=x+1
  391. end
  392.  
  393. print ("  Wasser smoothered out: "..tostring(smooth_surface('wasser')))
  394.  
  395. --adding beaches
  396. x=0
  397. count=0
  398. local result
  399. while x<x_size do
  400.     y=0
  401.     while y<y_size do
  402.         f=base.map:get_field(x,y)
  403.         if is_water_around(f) then
  404.             result=set_surface_if_not(f,'strand','wasser')
  405.             --if result==false then
  406.             --  print ("failed to change: "..tostring(f))
  407.             --else
  408.                 --print ("changed: "..tostring(f))         
  409.             --end
  410.             count=count+1
  411.            
  412.         end
  413.         --if f.terd~='wasser' and is_water_around(f) then
  414.             --set_surface_if_not(f,'strand','wasser')
  415.         --end              
  416.         y=y+1
  417.     end
  418.     x=x+1
  419. end
  420. print ("  Beach added :"..tostring(count))
  421.  
  422.  
  423. print ("  Strand smoothered out: "..tostring(smooth_surface('strand')))
  424. print ("  Wasser smoothered out: "..tostring(smooth_surface('wasser')))
  425. print ("  Steepe smoothered out: "..tostring(smooth_surface('steppe')))
  426. print ("  Snow smoothered out: "..tostring(smooth_surface('schnee')))
  427. print ("  Snow smoothered out: "..tostring(smooth_surface('bergwiese')))
  428. print ("  Strand smoothered out: "..tostring(smooth_surface('strand')))
  429. print ("  Wasser smoothered out: "..tostring(smooth_surface('wasser')))
  430. print ("  Steepe smoothered out: "..tostring(smooth_surface('steppe')))
  431. print ("  Snow smoothered out: "..tostring(smooth_surface('schnee')))
  432. print ("  Snow smoothered out: "..tostring(smooth_surface('bergwiese')))
  433. print ("  Strand smoothered out: "..tostring(smooth_surface('strand')))
  434. print ("  Wasser smoothered out: "..tostring(smooth_surface('wasser')))
  435. print ("  Steepe smoothered out: "..tostring(smooth_surface('steppe')))
  436. print ("  Snow smoothered out: "..tostring(smooth_surface('schnee')))
  437.  
  438. --resseting height of water
  439. print (" Resetting water altitude...")
  440. x=0
  441. count=0
  442. while x<x_size do
  443.     y=0
  444.     while y<y_size do
  445.         f=base.map:get_field(x,y)
  446.         if is_water_around(f) and f.height>00 then
  447.             while f.height>0 do
  448.                 f.height=0
  449.             end
  450.             count=count+1
  451.         end
  452.         y=y+1
  453.     end
  454.     x=x+1
  455. end
  456. print ("  water altitude changed: "..tostring(count))
  457.  
  458. target=x_size*y_size*0.025
  459. seeder_immovable(20, "stones4",{},{'wasser'},11,40)    
  460. cluster_immovable(target*0.2,"stones4",{'stones4'},{'wasser'},11,40,2)
  461. cluster_immovable(target*0.4,"stones3",{'stones4'},{'wasser'},11,40,1)
  462. cluster_immovable(target*0.4,"stones2",{'stones4','stones3'},{'wasser'},11,40,1)
  463.  
  464.  
  465. target=x_size*y_size*0.0003
  466. seeder_immovable(target, "tree6_s",{},{'wasser'},40,47)
  467. target=x_size*y_size*0.01
  468. cluster_immovable(target*0.5,"tree3",{'tree3','tree6_s'},{'wasser'},40,50,2)
  469. cluster_immovable(target*0.5,"tree6_s",{'tree3','tree6_s'},{'wasser'},40,50,1)
  470.  
  471.  
  472. target=x_size*y_size*0.0003
  473. seeder_immovable(target, "tree8_m",{},{'wasser'},25,48)
  474. target=x_size*y_size*0.03
  475. cluster_immovable(target,"tree8_m",{'tree8_m'},{'wasser'},25,51,1)
  476.  
  477.  
  478. target=x_size*y_size*0.0003
  479. seeder_immovable(target, "tree4",{},{'wasser'},0,26)   
  480. target=x_size*y_size*0.05
  481. cluster_immovable(target/2,"tree4_m",{'tree4_m','tree4'},{'wasser'},0,26,2)
  482. cluster_immovable(target/2,"tree4",{'tree4_m','tree4'},{'wasser'},0,26,1)
  483.  
  484.  
  485. print_height_stat()
  486. mines_stat()
  487. big_stat()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement