-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffedit_cl.lua
More file actions
129 lines (118 loc) · 3.73 KB
/
offedit_cl.lua
File metadata and controls
129 lines (118 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
local MAX_THICKNESS = 1.2
local RENDER = true
local thickness
local drawLine
local colour = tocolor(255, 255, 255, 120)
local attachedToElements = {}
addEvent("updateGridlines", true)
addEventHandler("updateGridlines", root,
function(tbl)
attachedToElements = tbl
end
)
addEvent("setObjectBreakability", true)
addEventHandler("setObjectBreakability", root,
function(object, bool)
setObjectBreakable(object, bool)
end
)
addCommandHandler('togglelines',
function ()
if RENDER then
RENDER = false
else
RENDER = true
end
end
)
function renderGridlines()
if RENDER then
for k in pairs (attachedToElements) do
if not isElement(k) then return end
if getElementDimension(k) ~= getElementDimension(localPlayer) then return end
local x, y, z = getElementPosition(k)
if not x then return end
local minX, minY, minZ, maxX, maxY, maxZ = getElementBoundingBox(k)
if not minX then
local radius = getElementRadius(k)
if radius then
minX, minY, minZ, maxX, maxY, maxZ = -radius, -radius, -radius, radius, radius, radius
end
end
if not minX or not minY or not minZ or not maxX or not maxY or not maxZ then return end
local camX,camY,camZ = getCameraMatrix()
--Work out our line thickness
thickness = (100/getDistanceBetweenPoints3D(camX,camY,camZ,x,y,z)) * MAX_THICKNESS
--
local elementMatrix = (getElementMatrix(k))
and matrix(getElementMatrix(k))
if not elementMatrix then
--Make them into absolute coords
minX, minY, minZ = minX + x,minY + y,minZ + z
maxX, maxY, maxZ = maxX + x,maxY + y,maxZ + z
end
--
local face1 = matrix{
{minX, maxY, minZ,1},
{minX, maxY, maxZ,1},
{maxX, maxY, maxZ,1},
{maxX, maxY, minZ,1},
}
local face2 = matrix{
{minX, minY, minZ,1},
{minX, minY, maxZ,1},
{maxX, minY, maxZ,1},
{maxX, minY, minZ,1},
}
if elementMatrix then
face1 = face1 * elementMatrix
face2 = face2 * elementMatrix
end
local faces = {face1,face2}
local drawLines, furthestNode, furthestDistance = {},{},0
--Draw rectangular faces
for k,face in ipairs (faces) do
for i,coord3d in ipairs (face) do
if not getScreenFromWorldPosition(coord3d[1], coord3d[2], coord3d[3], 10) then return end
local nextIndex = i + 1
if not face[nextIndex] then nextIndex = 1 end
local targetCoord3d = face[nextIndex]
table.insert(drawLines, {coord3d, targetCoord3d})
local camDistance = getDistanceBetweenPoints3D(camX, camY, camZ, unpack(coord3d))
if camDistance > furthestDistance then
furthestDistance = camDistance
furthestNode = faces[k][i]
end
end
end
--Connect these faces together with four lines
for i=1,4 do
table.insert(drawLines, {faces[1][i], faces[2][i]})
end
--
for i,draw in ipairs (drawLines) do
if(not vectorCompare(draw[1], furthestNode)) and(not vectorCompare(draw[2], furthestNode)) then
drawLine(unpack(draw))
end
end
end
end
end
addEventHandler("onClientRender", root, renderGridlines)
function drawLine(vecOrigin, vecTarget)
local startX, startY = getScreenFromWorldPosition(vecOrigin[1], vecOrigin[2], vecOrigin[3],10)
local endX,endY = getScreenFromWorldPosition(vecTarget[1], vecTarget[2], vecTarget[3], 10)
if not startX or not startY or not endX or not endY then
return false
end
return dxDrawLine(startX, startY, endX, endY, colour, thickness, false)
end
function vectorCompare(vec1,vec2)
if vec1[1] == vec2[1] and vec1[2] == vec2[2] and vec1[3] == vec2[3] then return true end
end
function getOffsetRelativeToElement(element, x, y, z)
--Convert this into a lua matrix
local elementMatrix = matrix{getElementMatrix(element)}
elementMatrix = matrix{x, y, z} * elementMatrix
return elementMatrix
end