Consider the following code:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
cid_diamond = core.get_content_id("default:diamondblock")
core.register_on_generated(function(minp, maxp, seed)
-- get mapgen objects
local hm = core.get_mapgen_object("heightmap")
local vm, emin, emax = core.get_mapgen_object("voxelmanip")
local vm_area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
local vm_data = vm:get_data()
-- get a random position
local index3d = 0
do
local p1 = {
x = math.random(minp.x,maxp.x),
y = 0,
z = math.random(minp.z,maxp.z)
}
-- returns the height based on height map
local chunksize = maxp.x - minp.x + 1
local index2d = (p1.z - minp.z) * chunksize + (p1.x - minp.x) + 1
--[[
local index2d = 0
for i = minp.x, maxp.x do
local br = false
for j = minp.z, maxp.z do
index2d = index2d + 1
if (i == p1.x) and (j == p1.z) then
br = true
break
end
end
if br == true then break end
end
]]
p1.y = hm[index2d]
index3d = vm_area:indexp(p1)
print(core.pos_to_string(p1))
end
vm_data[index3d] = cid_diamond
vm:set_data(vm_data)
vm:write_to_map()
end)
No matter if using the mathematical method of finding the index or the (commented) count method, more often than not the position is not on the ground. The mathematical method seems to work better, but still, more often than not if I teleport to the position I am many nodes above or below the ground. There has to be something I am missing or something is wrong with my code. Anybody have any ideas?
edit:
Seem to have found a slight error in the math. The line...
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local index2d = (p1.z - minp.z) * chunksize + (p1.x - minp.x) + 1
...should be...
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local index2d = (p1.z - minp.z) * chunksize + (p1.x - minp.x)
..I think. The "+ 1" was adding an extra 80 to the index. It is still not anywhere close to right all the time but it is much closer than before and I think I can force it with a little node checking. Still, it would be nice to have an extra set of eyes on this and tell what is going wrong.