Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print (" =========================================== ")
- base=wl.Editor()
- x_size=base.map.width
- y_size=base.map.height
- inputfile='/home/tibor/devel/terrain-generator/import_coordinates_2.txt'
- -- see if the file exists
- function file_exists(file)
- local f = io.open(file, "rb")
- if f then
- f:close()
- return true
- end
- return false
- end
- function seeder_immovable(target, thing,whitelist,blacklist,minheight,maxheight)
- --final count, immovable type, surface whitelist, surface blacklist
- i=0
- count=0
- while i<target*100 do
- x=math.random(x_size)-1
- y=math.random(y_size)-1
- f=base.map:get_field(x,y)
- 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
- if pcall(function() base.map:place_immovable(thing,f) end) then
- --print (" placing "..thing.." iteration: "..i)
- count=count+1
- else
- --print (" placing "..thing.." failed, iteration: "..i)
- end
- end
- if count>target then
- i=i+100000
- end
- i=i+1
- end
- print (" Placed "..count.." (target: "..target..") of "..thing.." @ seeder_immovable")
- end
- function table.contains(table, element)
- for _, value in pairs(table) do
- if value == element then
- return true
- end
- end
- return false
- end
- function cluster_immovable(target,thing,searchingfor,blacklist,minheight,maxheight,radius)
- --final count, immovable types,neighbours to search, blacklist of survaces, minheight, maxheight)
- i=0
- count=0
- --for i, v in ipairs(searchingfor) do print("searching for "..i..": "..v) end
- while i<target*200 do
- x=math.random(x_size)-1
- y=math.random(y_size)-1
- f=base.map:get_field(x,y)
- 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
- --print ("good place to build on")
- nearspots=f:region(radius)
- nearspotscount=table.getn(nearspots)
- j=1
- adding=false
- while j<=nearspotscount do
- nearspot=nearspots[j]
- if nearspot.immovable~=nill then
- --print (" "..nearspot.immovable.name)
- if table.contains(searchingfor,nearspot.immovable.name) then
- adding=true
- --print(" immovable found on near position")
- end
- end
- j=j+1
- end
- if adding==true then
- if pcall(function() base.map:place_immovable(thing,f) end) then
- --print (" placing immovable cluster_immovable()")
- count=count+1
- --else
- --print (" placing immovable failed cluster_immovable()")
- end
- else
- --print(" Failed to find the particular immoveble in the neighbourd")
- end
- end
- if count>target then
- i=i+100000
- end
- i=i+1
- end
- --for i, v in ipairs(searchingfor) do print("searching for "..i..": "..v) end
- print (" Placed "..count.." (target: "..target..") of "..thing.." @ cluster_immovable")
- end
- function print_height_stat()
- local x=0
- local y
- local low=0
- local high=0
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- if f.height<20 then
- low=low+1
- end
- if f.height>=50 then
- high=high+1
- end
- y=y+1
- end
- x=x+1
- end
- print (" Height stat: below 20: "..low..", above/equal 50: "..high)
- end
- function big_stat()
- local x=0
- local y
- local count=0
- local medium_count=0
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- if f:has_caps("big") then
- count=count+1
- end
- if f:has_caps("medium") then
- medium_count=medium_count+1
- end
- y=y+1
- end
- x=x+1
- end
- print (" Fields with big/medium buildings capabilities: "..count.."/"..tostring(medium_count))
- end
- function mines_stat()
- local x=0
- local y
- local count=0
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- if f:has_caps("mine") then
- count=count+1
- end
- y=y+1
- end
- x=x+1
- end
- print (" Fields with mine capabilities: "..count)
- end
- function set_surface_if_not(f,newsurface,nochangesurface)
- local changed=false
- if f.terr~=newsurface and f.terr~=nochangesurface then
- f.terr=newsurface
- changed=true
- end
- if f.terd~=newsurface and f.terd~=nochangesurface then
- f.terd=newsurface
- changed=true
- end
- if f.ln.terr~=newsurface and f.ln.terr~=nochangesurface then
- f.ln.terr=newsurface
- changed=true
- end
- if f.tln.terr~=newsurface and f.tln.terr~=nochangesurface then
- f.tln.terr=newsurface
- changed=true
- end
- if f.tln.terd~=newsurface and f.tln.terd~=nochangesurface then
- f.tln.terd=newsurface
- changed=true
- end
- if f.trn.terd~=newsurface and f.trn.terd~=nochangesurface then
- f.trn.terd=newsurface
- changed=true
- end
- return changed
- end
- function read_file(file)
- lines = {}
- for line in io.lines(file) do
- lines[#lines + 1] = line
- end
- return lines
- end
- function is_water_around(field)
- if f.terr=='wasser' then
- return true
- end
- if f.terd=='wasser' then
- return true
- end
- if f.ln.terr=='wasser' then
- return true
- end
- if f.tln.terr=='wasser' then
- return true
- end
- if f.tln.terd=='wasser' then
- return true
- end
- if f.trn.terd=='wasser' then
- return true
- end
- return false
- end
- function split(s,re)
- local i1 = 1
- local ls = {}
- local append = table.insert
- if not re then re = '%s+' end
- if re == '' then return {s} end
- while true do
- local i2,i3 = s:find(re,i1)
- if not i2 then
- local last = s:sub(i1)
- if last ~= '' then append(ls,last) end
- if #ls == 1 and ls[1] == '' then
- return {}
- else
- return ls
- end
- end
- append(ls,s:sub(i1,i2-1))
- i1 = i3+1
- end
- end
- function smooth_surface(terraintype)
- local counter=0
- local x,y
- x=0
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- if f.terr~=terraintype then
- if f.terd==terraintype and f.trn.terd==terraintype then
- f.terr=terraintype
- counter=counter+1
- --print ( " Addding ".. terraintype.." to triangle 1")
- end
- end
- if f.terr~=terraintype then
- if f.terd==terraintype and f.rn.terd==terraintype then
- f.terr=terraintype
- counter=counter+1
- --print ( " Addding ".. terraintype.." to triangle 1")
- end
- end
- if f.terr~=terraintype then
- if f.trn.terd==terraintype and f.rn.terd==terraintype then
- f.terr=terraintype
- counter=counter+1
- --print ( " Addding ".. terraintype.." to triangle 1")
- end
- end
- --f=base.map:get_field(x,y)
- if f.terd~=terraintype then
- if f.bln.terr==terraintype and f.terr==terraintype then
- f.terd=terraintype
- counter=counter+1
- --print ( " Addding ".. terraintype.." to triangle 2")
- end
- end
- if f.terd~=terraintype then
- if f.bln.terr==terraintype and f.ln.terr==terraintype then
- f.terd=terraintype
- counter=counter+1
- --print ( " Addding ".. terraintype.." to triangle 2")
- end
- end
- if f.terd~=terraintype then
- if f.ln.terr==terraintype and f.terr==terraintype then
- f.terd=terraintype
- counter=counter+1
- --print ( " Addding ".. terraintype.." to triangle 2")
- end
- end
- y=y+1
- end
- x=x+1
- end
- return counter
- end
- if not (file_exists(inputfile)) then
- print (" Input file missing")
- exit()
- end
- print (" Zerroing terrain...")
- x=0
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- f.height=10
- f.terr='steppe'
- f.terd='steppe'
- if f.immovable then
- f.immovable:remove()
- end
- y=y+1
- end
- x=x+1
- end
- content=read_file(inputfile)
- print (" importing&applying height data")
- for k,v in pairs(lines) do
- --print(v)
- values=split(v)
- --print ("a")
- x=values[1]
- y=values[2]
- height=values[3]
- --print (values[1])
- f=base.map:get_field(x,y)
- if f.height~=height then
- f.height=height
- end
- end
- print (" setting terrain types")
- --setting terrain types
- x=0
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- if f.height>51 then
- f.terr='schnee'
- f.terd='schnee'
- --print ("snow")
- elseif f.height>43 then
- f.terr='berg4'
- f.terd='berg4'
- elseif f.height>34 then
- f.terr='bergwiese'
- f.terd='bergwiese'
- elseif f.height>=21 then
- f.terr='steppe'
- f.terd='steppe'
- ----print ("snow")
- elseif f.height>0 then
- f.terr='wiese2'
- f.terd='wiese2'
- else
- f.terr='wasser'
- f.terd='wasser'
- end
- y=y+1
- end
- x=x+1
- end
- print (" Wasser smoothered out: "..tostring(smooth_surface('wasser')))
- --adding beaches
- x=0
- count=0
- local result
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- if is_water_around(f) then
- result=set_surface_if_not(f,'strand','wasser')
- --if result==false then
- -- print ("failed to change: "..tostring(f))
- --else
- --print ("changed: "..tostring(f))
- --end
- count=count+1
- end
- --if f.terd~='wasser' and is_water_around(f) then
- --set_surface_if_not(f,'strand','wasser')
- --end
- y=y+1
- end
- x=x+1
- end
- print (" Beach added :"..tostring(count))
- print (" Strand smoothered out: "..tostring(smooth_surface('strand')))
- print (" Wasser smoothered out: "..tostring(smooth_surface('wasser')))
- print (" Steepe smoothered out: "..tostring(smooth_surface('steppe')))
- print (" Snow smoothered out: "..tostring(smooth_surface('schnee')))
- print (" Snow smoothered out: "..tostring(smooth_surface('bergwiese')))
- print (" Strand smoothered out: "..tostring(smooth_surface('strand')))
- print (" Wasser smoothered out: "..tostring(smooth_surface('wasser')))
- print (" Steepe smoothered out: "..tostring(smooth_surface('steppe')))
- print (" Snow smoothered out: "..tostring(smooth_surface('schnee')))
- print (" Snow smoothered out: "..tostring(smooth_surface('bergwiese')))
- print (" Strand smoothered out: "..tostring(smooth_surface('strand')))
- print (" Wasser smoothered out: "..tostring(smooth_surface('wasser')))
- print (" Steepe smoothered out: "..tostring(smooth_surface('steppe')))
- print (" Snow smoothered out: "..tostring(smooth_surface('schnee')))
- --resseting height of water
- print (" Resetting water altitude...")
- x=0
- count=0
- while x<x_size do
- y=0
- while y<y_size do
- f=base.map:get_field(x,y)
- if is_water_around(f) and f.height>00 then
- while f.height>0 do
- f.height=0
- end
- count=count+1
- end
- y=y+1
- end
- x=x+1
- end
- print (" water altitude changed: "..tostring(count))
- target=x_size*y_size*0.025
- seeder_immovable(20, "stones4",{},{'wasser'},11,40)
- cluster_immovable(target*0.2,"stones4",{'stones4'},{'wasser'},11,40,2)
- cluster_immovable(target*0.4,"stones3",{'stones4'},{'wasser'},11,40,1)
- cluster_immovable(target*0.4,"stones2",{'stones4','stones3'},{'wasser'},11,40,1)
- target=x_size*y_size*0.0003
- seeder_immovable(target, "tree6_s",{},{'wasser'},40,47)
- target=x_size*y_size*0.01
- cluster_immovable(target*0.5,"tree3",{'tree3','tree6_s'},{'wasser'},40,50,2)
- cluster_immovable(target*0.5,"tree6_s",{'tree3','tree6_s'},{'wasser'},40,50,1)
- target=x_size*y_size*0.0003
- seeder_immovable(target, "tree8_m",{},{'wasser'},25,48)
- target=x_size*y_size*0.03
- cluster_immovable(target,"tree8_m",{'tree8_m'},{'wasser'},25,51,1)
- target=x_size*y_size*0.0003
- seeder_immovable(target, "tree4",{},{'wasser'},0,26)
- target=x_size*y_size*0.05
- cluster_immovable(target/2,"tree4_m",{'tree4_m','tree4'},{'wasser'},0,26,2)
- cluster_immovable(target/2,"tree4",{'tree4_m','tree4'},{'wasser'},0,26,1)
- print_height_stat()
- mines_stat()
- big_stat()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement