-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper.lua
162 lines (137 loc) · 4.96 KB
/
helper.lua
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---A function to get the x variable of a table, used in IsInPoly
---@param tbl vector2
---@return number
local function getX(tbl)
return tbl.x
end
---A function to get the y variable of a table, used in IsInPoly
---@param tbl vector2
---@return number
local function getY(tbl)
return tbl.y
end
---Gets the minimum value in a vector2 array
---@param tbl vector2[]
---@param getValue fun(thisTable: vector2): number
---@return number
local function min(tbl, getValue)
local minValue
for i = 1, #tbl do
local value = getValue(tbl[i])
minValue = not minValue and value or value < minValue and value or minValue
end
return minValue
end
---Gets the maximum value in a vector2 array
---@param tbl vector2[]
---@param getValue fun(thisTable: vector2): number
---@return number
local function max(tbl, getValue)
local maxValue
for i = 1, #tbl do
local value = getValue(tbl[i])
maxValue = not maxValue and value or value > maxValue and value or maxValue
end
return maxValue
end
---Determines whether the point is inside the given polygon
---@param poly vector2[]
---@param point vector2
---@return boolean
function IsInPoly(poly, point)
local minX = min(poly, getX)
local minY = min(poly, getY)
local maxX = max(poly, getX)
local maxY = max(poly, getY)
if point.x < minX or point.x > maxX or point.y < minY or point.y > maxY then
return false
end
local isMatch = false
local j = #poly
for i = 1, #poly do
local data = poly[i]
local data2 = poly[j]
-- When the position is right on a point, count it as a match.
if data.x == point.x and data.y == point.y then
return true
end
-- When the position is right on a point, count it as a match.
if data2.x == point.x and data2.y == point.y then
return true
end
-- When the position is on a horizontal line, count it as a match.
if data.x == data2.x and point.x == data.x and point.y >= math.min(data.y, data2.y) and point.y <= math.max(data.y, data2.y) then
return true
end
-- When the position is on a vertical line, count it as a match.
if data.y == data2.y and point.y == data.y and point.x >= math.min(data.x, data2.x) and point.x <= math.max(data.x, data2.x) then
return true
end
if ((data.y > point.y) ~= (data2.y > point.y)) and point.x < ((data2.x - data.x) * (point.y - data.y) / (data2.y - data.y) + data.x) then
isMatch = not isMatch
end
j = i
end
return isMatch
end
---Gets the 2d offset position relative to the start position
---@param startPosition vector2
---@param magnitude number
---@param heading number
---@param multiplier integer
---@return vector2
function GetOffsetPosition(startPosition, magnitude, heading, multiplier)
local degree = math.pi / 180
local headingDegrees = heading * degree
local cosX = multiplier * math.cos(headingDegrees)
local sinY = multiplier * math.sin(headingDegrees)
return vec2(startPosition.x + magnitude * cosX, startPosition.y + magnitude * sinY)
end
---@return integer
function GetDebugCursorDirectionInput()
local newDirection = -1
if IsDisabledControlPressed(0, 34) then -- INPUT_MOVE_LEFT_ONLY
newDirection = Directions.Left
elseif IsDisabledControlPressed(0, 35) then -- INPUT_MOVE_RIGHT_ONLY
newDirection = Directions.Right
elseif IsDisabledControlPressed(0, 32) then -- INPUT_MOVE_UP_ONLY
newDirection = Directions.Up
elseif IsDisabledControlPressed(0, 33) then -- INPUT_MOVE_DOWN_ONLY
newDirection = Directions.Down
end
return newDirection
end
---@return number
function GetDebugCursorSpeedInput()
local debugCursorSpeed = 0
if IsControlPressed(0, 127) then -- INPUT_VEH_SUB_PITCH_UP_ONLY
debugCursorSpeed = 0.00025
elseif IsControlPressed(0, 128) then -- INPUT_VEH_SUB_PITCH_DOWN_ONLY
debugCursorSpeed = -0.00025
end
return debugCursorSpeed
end
---@param spriteName string
---@param position vector2
---@param heading number
---@param width number
---@param height number
function DrawDebugSprite(spriteName, position, heading, width, height)
DrawSprite('MPCircuitHack', spriteName, position.x, position.y, width, height, heading, 255, 255, 255, 255)
end
---@param position vector2
---@param heading number
function DebugPortSprite(position, heading)
local width = (heading == 0 or heading == 180) and 0.02 or 0.0325
local height = (heading == 0 or heading == 180) and 0.055 or 0.03
DrawDebugSprite('GenericPort', position, heading, width, height)
DrawRect(position.x, position.y, 0.005, 0.01, 20, 255, 20, 255)
end
function GetPortDebugHeading(currentHeading)
if IsControlPressed(0, 124) then -- INPUT_VEH_SUB_TURN_LEFT_ONLY
currentHeading -= 90
elseif IsControlPressed(0, 125) then -- INPUT_VEH_SUB_TURN_RIGHT_ONLY
currentHeading += 90
end
return currentHeading
end