Advertisement
Guest User

Untitled

a guest
Aug 1st, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.88 KB | None | 0 0
  1. roads = {}
  2. roads.pos1 = {}
  3. roads.pos2 = {}
  4. roads.ok = false
  5. roads.path = {}
  6.  
  7. minetest.register_tool("roads:tool", {
  8.     description = "roads edit tool",
  9.     inventory_image = "default_stick.png",
  10.     on_use = function(itemstack, user, pointed_thing)
  11.        local pos = pointed_thing.above
  12.        local name = user:get_player_name()
  13.        if roads.ok then
  14.           minetest.chat_send_player(name,"made road")
  15.          
  16.           roads.path = minetest.find_path(roads.pos1,roads.pos2,
  17.                  5,
  18.                  1,
  19.                  1,
  20.                  "A*")
  21.          
  22.          --roads.path = line3d(roads.pos1, roads.pos2)
  23.           print(dump(roads.path))
  24.           local l = path2line(roads.path)
  25.           print(dump(l))
  26.           for k, v in pairs(l) do
  27.          minetest.set_node(v, { name = "wool:red" } )
  28.           end
  29.           roads.pos1 = {}
  30.           roads.pos2 = {}
  31.           roads.path = {}
  32.           roads.ok = false
  33.        elseif roads.pos1.x then
  34.           roads.pos2 = pos
  35.           minetest.chat_send_player(name,"pos2 set")
  36.           roads.ok = true
  37.        else
  38.           roads.pos1 = pos
  39.           minetest.chat_send_player(name,"pos1 set")
  40.        end
  41.     end,
  42. })
  43.  
  44. path2line = function(path)
  45.    local line = {}
  46.    local pos1, pos2
  47.    pos1 = path[1]
  48.    for k, v in pairs(path) do
  49.       pos2 = v
  50.       local l = line3d(pos1, pos2)
  51.       for k, v in pairs(l) do
  52.      table.insert(line, v)
  53.       end
  54.       pos1 = v
  55.    end
  56.    return line
  57. end
  58.  
  59. --line2street = function(line)
  60.    --for k, v in pairs(line) do
  61.       --turn turtle forward (starts facing up)
  62.       --find angle perpendicular to direction
  63.       --set angle, spawn axiom, something like "TTTTFTTTT"
  64.       --with appropriate materials in treedef
  65.       --spawn "tree"
  66.    --end
  67. --end
  68.  
  69. line3d = function(from_position, to_position)
  70. --from https://github.com/ryanramage/bresenham3d/blob/master/index.js
  71. --lua port by jin_xi
  72.    local line = {}
  73.    local temp
  74.  
  75.    local x0 = math.floor(from_position.x)
  76.    local y0 = math.floor(from_position.y)
  77.    local z0 = math.floor(from_position.z)
  78.    local x1 = math.floor(to_position.x)
  79.    local y1 = math.floor(to_position.y)
  80.    local z1 = math.floor(to_position.z)
  81.    
  82.    --'steep' xy Line, make longest delta x plane
  83.    local swap_xy = math.abs(y1 - y0) > math.abs(x1 - x0)
  84.    if swap_xy then
  85.       temp = x0; x0 = y0; y0 = temp --swap(x0, y0);
  86.       temp = x1; x1 = y1; y1 = temp --swap(x1, y1);
  87.    end
  88.    
  89.    local swap_xz = math.abs(z1 - z0) > math.abs(x1 - x0)
  90.    if swap_xz then
  91.       temp = x0; x0 = z0; z0 = temp --swap(x0, z0);
  92.       temp = x1; x1 = z1; z1 = temp --swap(x1, z1);
  93.    end
  94.    
  95.    --delta is Length in each plane
  96.    local delta_x = math.abs(x1 - x0)
  97.    local delta_y = math.abs(y1 - y0)
  98.    local delta_z = math.abs(z1 - z0)
  99.    
  100.    --drift controls when to step in 'shallow' planes
  101.    --starting value keeps Line centred
  102.    local drift_xy = (delta_x / 2)
  103.    local drift_xz = (delta_x / 2)
  104.    
  105.    --direction of line
  106.    local step_x = 1
  107.    if x0 > x1 then step_x = -1 end
  108.    local step_y = 1
  109.    if y0 > y1 then step_y = -1 end
  110.    local step_z = 1
  111.    if z0 > z1 then step_z = -1 end
  112.    
  113.    --starting point
  114.    local y = y0
  115.    local z = z0
  116.    
  117.    local cx, cy, cz
  118.    
  119.    --step through longest delta (which we have swapped to x)
  120.    for x = x0, x1, step_x do
  121.      
  122.       --copy position
  123.       cx = x; cy = y; cz = z
  124.      
  125.       --unswap (in reverse)
  126.       if swap_xz then
  127.      temp = cx; cx = cz; cz = temp --swap(cx, cz)
  128.       end
  129.       if swap_xy then
  130.      temp = cx; cx = cy; cy = temp --swap(cx, cy)
  131.       end
  132.      
  133.       table.insert(line, { x = cx, y = cy, z = cz } )
  134.  
  135.       --update progress in other planes
  136.       drift_xy = drift_xy - delta_y
  137.       drift_xz = drift_xz - delta_z
  138.      
  139.       --step in y plane
  140.       if drift_xy < 0 then
  141.      y = y + step_y
  142.      drift_xy = drift_xy + delta_x
  143.       end
  144.      
  145.       --same in z
  146.       if (drift_xz < 0) then
  147.      z = z + step_z
  148.      drift_xz = drift_xz + delta_x
  149.       end
  150.    end
  151.    return line
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement