@@ -14,8 +14,6 @@ local ignoreElementWalls = true -- 'false' not supported yet
14
14
15
15
local isEnabled = false
16
16
17
- local root = getRootElement ()
18
-
19
17
local camX , camY , camZ
20
18
21
19
local selectedElement
@@ -36,27 +34,12 @@ local hasRotation = {
36
34
ped = true ,
37
35
}
38
36
39
- -- PRIVATE
40
37
local onClientMouseMove
41
38
42
- local mta_getElementRotation = getElementRotation
43
- local function getElementRotation (element )
44
- local elementType = getElementType (element )
45
- if elementType == " player" or elementType == " ped" then
46
- return 0 ,0 ,getPedRotation (element )
47
- elseif elementType == " object" then
48
- return mta_getElementRotation (element )
49
- elseif elementType == " vehicle" then
50
- return mta_getElementRotation (element )
51
- end
52
- end
53
-
54
39
local function getCoordsWithBoundingBox (origX , origY , origZ )
55
40
if (not collisionless ) then
56
41
local newX , newY , newZ = origX , origY , origZ
57
- if (not ignoreElementWalls ) then
58
- -- hard stuff
59
- else
42
+ if ignoreElementWalls then
60
43
local surfaceFound , surfaceX , surfaceY , surfaceZ , element = processLineOfSight (origX , origY , origZ + SURFACE_ERROR_CORRECTION_OFFSET , origX , origY , origZ + minZ , true , true , true , true , true , true , false , true , selectedElement )
61
44
if (surfaceFound ) then
62
45
newZ = surfaceZ + centerToBaseDistance
@@ -68,87 +51,88 @@ local function getCoordsWithBoundingBox(origX, origY, origZ)
68
51
end
69
52
end
70
53
71
- local function processCursorMove (absoluteX ,absoluteY )
72
- if not absoluteX then
73
- local relX ,relY = getCursorPosition ()
74
- absoluteX ,absoluteY = relX * g_screenX ,relY * g_screenY
54
+ local function processCursorMove (absoluteX , absoluteY )
55
+ if not absoluteX or not absoluteY then
56
+ local relX , relY = getCursorPosition ()
57
+ absoluteX , absoluteY = relX * g_screenX , relY * g_screenY
75
58
end
76
59
-- process line, checking for water and surfaces
77
60
local worldX , worldY , worldZ = getWorldFromScreenPosition (absoluteX , absoluteY , MAX_DISTANCE )
78
61
-- make sure there is a camera position
79
- if (not camX ) then return end
80
- local surfaceFound , surfaceX , surfaceY , surfaceZ , element = processLineOfSight (camX , camY , camZ , worldX , worldY , worldZ , true , true , true , true , true , true , false , true , selectedElement )
81
- local waterFound , waterX , waterY , waterZ = testLineAgainstWater (camX , camY , camZ , worldX , worldY , worldZ )
82
-
83
- -- check if surfaces are not too far
84
- local surfaceDistance
85
- local waterDistance
86
- if (surfaceFound ) then
87
- surfaceDistance = math.sqrt ((surfaceX - camX )^ 2 + (surfaceY - camY )^ 2 + (surfaceZ - camZ )^ 2 )
88
- if (surfaceDistance > maxMoveDistance ) then
89
- surfaceFound = false
90
- end
91
- end
92
- if (waterFound ) then
93
- waterDistance = math.sqrt ((waterX - camX )^ 2 + (waterY - camY )^ 2 + (waterZ - camZ )^ 2 )
94
- if (waterDistance > maxMoveDistance ) then
95
- waterFound = false
96
- end
97
- end
62
+ if camX and camY then
63
+ local surfaceFound , surfaceX , surfaceY , surfaceZ , element = processLineOfSight (camX , camY , camZ , worldX , worldY , worldZ , true , true , true , true , true , true , false , true , selectedElement )
64
+ local waterFound , waterX , waterY , waterZ = testLineAgainstWater (camX , camY , camZ , worldX , worldY , worldZ )
98
65
99
- -- raise height if pickup or a marker
100
- if (getElementType (selectedElement ) == " pickup" ) or (getElementType (selectedElement ) == " marker" ) then
66
+ -- check if surfaces are not too far
67
+ local surfaceDistance
68
+ local waterDistance
101
69
if (surfaceFound ) then
102
- surfaceZ = surfaceZ + 1
70
+ surfaceDistance = math.sqrt ((surfaceX - camX )^ 2 + (surfaceY - camY )^ 2 + (surfaceZ - camZ )^ 2 )
71
+ if (surfaceDistance > maxMoveDistance ) then
72
+ surfaceFound = false
73
+ end
103
74
end
104
75
if (waterFound ) then
105
- waterZ = waterZ + 1
76
+ waterDistance = math.sqrt ((waterX - camX )^ 2 + (waterY - camY )^ 2 + (waterZ - camZ )^ 2 )
77
+ if (waterDistance > maxMoveDistance ) then
78
+ waterFound = false
79
+ end
106
80
end
107
- end
108
81
109
- -- check if water or surface was found
110
- if (surfaceFound and waterFound ) then
111
- -- if both found, compare distances
112
- if (waterDistance >= surfaceDistance ) then
82
+ -- raise height if pickup or a marker
83
+ if (getElementType (selectedElement ) == " pickup" ) or (getElementType (selectedElement ) == " marker" ) then
84
+ if (surfaceFound ) then
85
+ surfaceZ = surfaceZ + 1
86
+ end
87
+ if (waterFound ) then
88
+ waterZ = waterZ + 1
89
+ end
90
+ end
91
+
92
+ -- check if water or surface was found
93
+ if (surfaceFound and waterFound ) then
94
+ -- if both found, compare distances
95
+ if (waterDistance >= surfaceDistance ) then
96
+ if (not collisionless ) then
97
+ centerToBaseDistance = exports .edf :edfGetElementDistanceToBase (selectedElement )
98
+ local finalX , finalY , finalZ = getCoordsWithBoundingBox (surfaceX , surfaceY , surfaceZ )
99
+ setElementPosition (selectedElement , finalX , finalY , finalZ )
100
+ else
101
+ setElementPosition (selectedElement , surfaceX , surfaceY , surfaceZ )
102
+ end
103
+ else
104
+ if (not collisionless ) then
105
+ centerToBaseDistance = exports .edf :edfGetElementDistanceToBase (selectedElement )
106
+ local finalX , finalY , finalZ = getCoordsWithBoundingBox (waterX , waterY , waterZ )
107
+ setElementPosition (selectedElement , finalX , finalY , finalZ )
108
+ else
109
+ setElementPosition (selectedElement , waterX , waterY , waterZ )
110
+ end
111
+ end
112
+ elseif (surfaceFound ) then
113
113
if (not collisionless ) then
114
114
centerToBaseDistance = exports .edf :edfGetElementDistanceToBase (selectedElement )
115
115
local finalX , finalY , finalZ = getCoordsWithBoundingBox (surfaceX , surfaceY , surfaceZ )
116
116
setElementPosition (selectedElement , finalX , finalY , finalZ )
117
117
else
118
118
setElementPosition (selectedElement , surfaceX , surfaceY , surfaceZ )
119
119
end
120
- else
120
+ elseif ( waterFound ) then
121
121
if (not collisionless ) then
122
122
centerToBaseDistance = exports .edf :edfGetElementDistanceToBase (selectedElement )
123
123
local finalX , finalY , finalZ = getCoordsWithBoundingBox (waterX , waterY , waterZ )
124
124
setElementPosition (selectedElement , finalX , finalY , finalZ )
125
125
else
126
126
setElementPosition (selectedElement , waterX , waterY , waterZ )
127
127
end
128
+ else -- in air
129
+ local tempDistance = math.sqrt ((worldX - camX )^ 2 + (worldY - camY )^ 2 + (worldZ - camZ )^ 2 )
130
+ local distanceRatio = maxMoveDistance / tempDistance
131
+ local x = camX + (worldX - camX ) * distanceRatio
132
+ local y = camY + (worldY - camY ) * distanceRatio
133
+ local z = camZ + (worldZ - camZ ) * distanceRatio
134
+ setElementPosition (selectedElement , x , y , z )
128
135
end
129
- elseif (surfaceFound ) then
130
- if (not collisionless ) then
131
- centerToBaseDistance = exports .edf :edfGetElementDistanceToBase (selectedElement )
132
- local finalX , finalY , finalZ = getCoordsWithBoundingBox (surfaceX , surfaceY , surfaceZ )
133
- setElementPosition (selectedElement , finalX , finalY , finalZ )
134
- else
135
- setElementPosition (selectedElement , surfaceX , surfaceY , surfaceZ )
136
- end
137
- elseif (waterFound ) then
138
- if (not collisionless ) then
139
- centerToBaseDistance = exports .edf :edfGetElementDistanceToBase (selectedElement )
140
- local finalX , finalY , finalZ = getCoordsWithBoundingBox (waterX , waterY , waterZ )
141
- setElementPosition (selectedElement , finalX , finalY , finalZ )
142
- else
143
- setElementPosition (selectedElement , waterX , waterY , waterZ )
144
- end
145
- else -- in air
146
- local tempDistance = math.sqrt ((worldX - camX )^ 2 + (worldY - camY )^ 2 + (worldZ - camZ )^ 2 )
147
- local distanceRatio = maxMoveDistance / tempDistance
148
- local x = camX + (worldX - camX ) * distanceRatio
149
- local y = camY + (worldY - camY ) * distanceRatio
150
- local z = camZ + (worldZ - camZ ) * distanceRatio
151
- setElementPosition (selectedElement , x , y , z )
152
136
end
153
137
end
154
138
@@ -163,17 +147,16 @@ local function zoomWithMouseWheel(key, keyState)
163
147
speed = zoomSpeed .medium
164
148
end
165
149
166
- if getCommandState " zoom_in" then
150
+ if getCommandState ( " zoom_in" ) then
167
151
maxMoveDistance = math.max (maxMoveDistance - speed , MIN_DISTANCE )
168
- processCursorMove ()
152
+ processCursorMove ()
169
153
else -- if key == "zoom_out"
170
154
maxMoveDistance = math.min (maxMoveDistance + speed , MAX_DISTANCE )
171
- processCursorMove ()
155
+ processCursorMove ()
172
156
end
173
157
end
174
158
end
175
159
176
-
177
160
local function onClientCursorMove_cursor (_ , _ , absoluteX , absoluteY )
178
161
if (selectedElement ) then
179
162
if ignoreFirst then
@@ -184,7 +167,6 @@ local function onClientCursorMove_cursor(_, _, absoluteX, absoluteY )
184
167
end
185
168
end
186
169
187
-
188
170
local function rotateWithMouseWheel (key , keyState )
189
171
if (not rotationless ) and getCommandState (" mod_rotate" ) then
190
172
rotX , rotY , rotZ = getElementRotation (selectedElement )
@@ -292,10 +274,10 @@ function detachElement()
292
274
293
275
-- sync position/rotation
294
276
local tempPosX , tempPosY , tempPosZ = getElementPosition (selectedElement )
295
- triggerServerEvent (" syncProperty" , getLocalPlayer () , " position" , {tempPosX , tempPosY , tempPosZ }, exports .edf :edfGetAncestor (selectedElement ))
277
+ triggerServerEvent (" syncProperty" , localPlayer , " position" , {tempPosX , tempPosY , tempPosZ }, exports .edf :edfGetAncestor (selectedElement ))
296
278
if hasRotation [getElementType (selectedElement )] then
297
279
rotX , rotY , rotZ = getElementRotation (selectedElement )
298
- triggerServerEvent (" syncProperty" , getLocalPlayer () , " rotation" , {rotX , rotY , rotZ }, exports .edf :edfGetAncestor (selectedElement ))
280
+ triggerServerEvent (" syncProperty" , localPlayer , " rotation" , {rotX , rotY , rotZ }, exports .edf :edfGetAncestor (selectedElement ))
299
281
end
300
282
selectedElement = nil
301
283
@@ -330,11 +312,7 @@ function setZoomSpeeds(slow, medium, fast)
330
312
end
331
313
332
314
function getAttachedElement ()
333
- if (selectedElement ) then
334
- return selectedElement
335
- else
336
- return false
337
- end
315
+ return selectedElement
338
316
end
339
317
340
318
function getMaxMoveDistance ()
@@ -350,29 +328,27 @@ function getZoomSpeeds()
350
328
end
351
329
352
330
function disable ()
353
- if (not isEnabled ) then
354
- return false
331
+ if isEnabled then
332
+ removeEventHandler (" onClientCursorMove" , root , onClientCursorMove_cursor )
333
+ unbindControl (" quick_rotate_increase" , " down" , rotateWithMouseWheel )
334
+ unbindControl (" quick_rotate_decrease" , " down" , rotateWithMouseWheel )
335
+ unbindControl (" zoom_in" , " down" , zoomWithMouseWheel )
336
+ unbindControl (" zoom_out" , " down" , zoomWithMouseWheel )
337
+ isEnabled = false
355
338
end
356
- -- remove events, unbind keys
357
- removeEventHandler (" onClientCursorMove" , root , onClientCursorMove_cursor )
358
- unbindControl (" quick_rotate_increase" , " down" , rotateWithMouseWheel )
359
- unbindControl (" quick_rotate_decrease" , " down" , rotateWithMouseWheel )
360
- unbindControl (" zoom_in" , " down" , zoomWithMouseWheel )
361
- unbindControl (" zoom_out" , " down" , zoomWithMouseWheel )
362
- isEnabled = false
339
+ return true
363
340
end
364
341
365
342
function enable ()
366
- if isEnabled then
367
- return false
343
+ if not isEnabled then
344
+ ignoreFirst = true
345
+ addEventHandler (" onClientCursorMove" , root , onClientCursorMove_cursor )
346
+ bindControl (" quick_rotate_increase" , " down" , rotateWithMouseWheel )
347
+ bindControl (" quick_rotate_decrease" , " down" , rotateWithMouseWheel )
348
+ bindControl (" zoom_in" , " down" , zoomWithMouseWheel )
349
+ bindControl (" zoom_out" , " down" , zoomWithMouseWheel )
350
+ setTimer (processCursorMove , 50 , 1 ) -- Lazy but we have to wait for MTA to switch modes
351
+ isEnabled = true
368
352
end
369
- -- add events, bind keys
370
- ignoreFirst = true
371
- addEventHandler (" onClientCursorMove" , root , onClientCursorMove_cursor )
372
- bindControl (" quick_rotate_increase" , " down" , rotateWithMouseWheel ) -- rotate left
373
- bindControl (" quick_rotate_decrease" , " down" , rotateWithMouseWheel ) -- rotate right
374
- bindControl (" zoom_in" , " down" , zoomWithMouseWheel ) -- zoom in
375
- bindControl (" zoom_out" , " down" , zoomWithMouseWheel ) -- zoom out
376
- setTimer ( processCursorMove , 50 , 1 ) -- Lazy but we have to wait for MTA to switch modes
377
- isEnabled = true
353
+ return true
378
354
end
0 commit comments