Created
November 27, 2013 13:17
-
-
Save rubenwardy/7675464 to your computer and use it in GitHub Desktop.
Minetest OS Mapgen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- OS MAP READER | |
-- by rubenwardy | |
-- incomplete | |
-- CC BY SA | |
-- ================= | |
osmapgen = { | |
_api = "http://maps.googleapis.com/maps/api/elevation/json?", | |
url = {}, | |
data = {} | |
} | |
function osmapgen.get_height(x,z) | |
-- Check to see if it has been requested before | |
for i=1, #osmapgen.data do | |
local val = osmapgen.data[i] | |
if (val.x == x and val.z == z) then | |
return val.h | |
end | |
end | |
-- Request it | |
osmapgen.url.request({ | |
sensor=false, | |
locations=x..","..z | |
}) | |
if not osmapgen.result then | |
return 0 | |
else | |
local id = string.match(osmapgen.result.result, '"elevation" : ([-0123456789]+)') | |
print (id) | |
end | |
end | |
function osmapgen.url.request(params) | |
local res = "" | |
for key,val in pairs(params) do | |
if (res~="") then | |
res = res.."&" | |
end | |
if val then | |
res = res.. key .. "=" .. val | |
else | |
print("Value is empty!") | |
end | |
end | |
print (osmapgen._api..res) | |
if minetest.download_http then | |
osmapgen.result = nil | |
minetest.download_http(osmapgen._api..res, 10, osmapgen.url._result) | |
local start = os.clock() | |
while (not osmapgen.result) do | |
if (os.clock()>start+10) then | |
print("DOWNLOAD TIMEOUT") | |
return | |
end | |
end | |
elseif io.popen then | |
local dl,err = io.popen("curl --url "..osmapgen._api..res, "r") | |
if not dl the | |
--throw("OSMAPGEN: There is no way to download data."..err) | |
osmapgen.result = {result='"elevation" : 10'} | |
return | |
end | |
osmapgen.result = {dl=dl} | |
end | |
end | |
function osmapgen.url._result(result,code) | |
osmapgen.result = {result = result, code = code} | |
end | |
-- Initialise | |
function osmapgen.init() | |
if (not minetest.download_http) then | |
if (not io.popen) then | |
throw("OSMAPGEN: There is no way to download data.") | |
else | |
print("") | |
print("[WARNING!] You do not have a lua curl build.") | |
print("Delays will occur as the data is being downloaded.") | |
print("") | |
end | |
end | |
end | |
-- Set up mapgeneration call back | |
minetest.register_on_mapgen_init(function(mgparams) | |
minetest.set_mapgen_params({mgname="singlenode", flags="nolight", flagmask="nolight"}) | |
end) | |
minetest.register_on_generated(function(minp, maxp, seed) | |
-- Start timer | |
local t1 = os.clock() | |
-- Get voxel manipulator | |
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip") | |
local a = VoxelArea:new{ | |
MinEdge={x=emin.x, y=emin.y, z=emin.z}, | |
MaxEdge={x=emax.x, y=emax.y, z=emax.z}, | |
} | |
local data = vm:get_data() | |
-- Content Id | |
local c_dirt = minetest.get_content_id("default:dirt") | |
local c_grass = minetest.get_content_id("default:dirt_with_grass") | |
local c_water = minetest.get_content_id("default:water_source") | |
local sidelen = maxp.x - minp.x + 1 | |
for z = minp.z, maxp.z do | |
for x = minp.x, maxp.x do | |
local h = 0 --osmapgen.get_height(x,z) | |
for y = minp.y, maxp.y do | |
-- Do mapgeneration | |
if y<h-5 then | |
local vi = a:index(x, y, z) | |
data[vi] = c_stone | |
elseif y<h then | |
local vi = a:index(x, y, z) | |
data[vi] = c_dirt | |
elseif y=h then | |
local vi = a:index(x, y, z) | |
data[vi] = c_grass | |
end | |
end | |
end | |
end | |
vm:set_data(data) | |
vm:calc_lighting( | |
{x=minp.x-16, y=minp.y, z=minp.z-16}, | |
{x=maxp.x+16, y=maxp.y, z=maxp.z+16} | |
) | |
vm:write_to_map(data) | |
print(string.format("elapsed time: %.2fms", (os.clock() - t1) * 1000)) | |
end) | |
-- Init | |
osmapgen.init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are missing this code: