1
+ worldedit = worldedit or {}
2
+
3
+ local get_pointed = function (pos , nearest , distance )
4
+ if distance > 100 then
5
+ return false
6
+ end
7
+
8
+ -- check for collision with node
9
+ local nodename = env :get_node (pos ).name
10
+ if nodename ~= " air"
11
+ and nodename ~= " default:water_source"
12
+ and nodename ~= " default:water_flowing" then
13
+ if nodename ~= " ignore" then
14
+ return nearest
15
+ end
16
+ return false
17
+ end
18
+ end
19
+
20
+ local use = function (itemstack , user , pointed_thing )
21
+ if pointed_thing .type == " nothing" then -- pointing at nothing
22
+ local placepos = worldedit .raytrace (user :getpos (), user :get_look_dir (), get_pointed )
23
+ if placepos then -- extended reach
24
+ pointed_thing .type = " node"
25
+ pointed_thing .under = nil -- wip
26
+ pointed_thing .above = nil -- wip
27
+ end
28
+ end
29
+ return minetest .item_place_node (itemstack , user , pointed_thing )
30
+ end
31
+ --
32
+
33
+ worldedit .raytrace = function (pos , dir , callback )
34
+ local base = {x = math.floor (pos .x ), y = math.floor (pos .y ), z = math.floor (pos .z )}
35
+ local stepx , stepy , stepz = 0 , 0 , 0
36
+ local componentx , componenty , componentz = 0 , 0 , 0
37
+ local intersectx , intersecty , intersectz = 0 , 0 , 0
38
+
39
+ if dir .x == 0 then
40
+ intersectx = math.huge
41
+ elseif dir .x > 0 then
42
+ stepx = 1
43
+ componentx = 1 / dir .x
44
+ intersectx = ((base .x - pos .x ) + 1 ) * componentx
45
+ else
46
+ stepx = - 1
47
+ componentx = 1 / - dir .x
48
+ intersectx = (pos .x - base .x ) * componentx
49
+ end
50
+ if dir .y == 0 then
51
+ intersecty = math.huge
52
+ elseif dir .y > 0 then
53
+ stepy = 1
54
+ componenty = 1 / dir .y
55
+ intersecty = ((base .y - pos .y ) + 1 ) * componenty
56
+ else
57
+ stepy = - 1
58
+ componenty = 1 / - dir .y
59
+ intersecty = (pos .y - base .y ) * componenty
60
+ end
61
+ if dir .z == 0 then
62
+ intersectz = math.huge
63
+ elseif dir .z > 0 then
64
+ stepz = 1
65
+ componentz = 1 / dir .z
66
+ intersectz = ((base .z - pos .z ) + 1 ) * componentz
67
+ else
68
+ stepz = - 1
69
+ componentz = 1 / - dir .z
70
+ intersectz = (pos .z - base .z ) * componentz
71
+ end
72
+
73
+ local distance = 0
74
+ local nearest = {x = base .x , y = base .y , z = base .z }
75
+ while true do
76
+ local values = {callback (base , nearest , distance )}
77
+ if # values > 0 then
78
+ return unpack (values )
79
+ end
80
+
81
+ nearest .x , nearest .y , nearest .z = base .x , base .y , base .z
82
+ if intersectx < intersecty then
83
+ if intersectx < intersectz then
84
+ base .x = base .x + stepx
85
+ distance = intersectx
86
+ intersectx = intersectx + componentx
87
+ else
88
+ base .z = base .z + stepz
89
+ distance = intersectz
90
+ intersectz = intersectz + componentz
91
+ end
92
+ elseif intersecty < intersectz then
93
+ base .y = base .y + stepy
94
+ distance = intersecty
95
+ intersecty = intersecty + componenty
96
+ else
97
+ base .z = base .z + stepz
98
+ distance = intersectz
99
+ intersectz = intersectz + componentz
100
+ end
101
+ end
102
+ end
2 commit comments
Jordach commentedon Aug 6, 2013
WTF is this?
Uberi commentedon Aug 6, 2013
The code is pretty self explanatory. Documentation will be written sometime in the future.