forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.lua
More file actions
244 lines (199 loc) · 7.07 KB
/
utilities.lua
File metadata and controls
244 lines (199 loc) · 7.07 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
-----------------------------------------------------------------
-- File : /lua/utilities.lua
-- Author(s): John Comes, Gordon Duclos
-- Summary : Utility functions for scripts.
-- Copyright � 2005 Gas Powered Games, Inc. All rights reserved.
-----------------------------------------------------------------
function GetDistanceBetweenTwoEntities(entity1, entity2)
return VDist3(entity1:GetPosition(),entity2:GetPosition())
end
-- Function originally created to check if a Mass Storage can be queued in a location without overlapping
function CanBuildInSpot(originUnit, unitId, pos)
local bp = __blueprints[unitId]
local mySkirtX = bp.Physics.SkirtSizeX / 2
local mySkirtZ = bp.Physics.SkirtSizeZ / 2
-- Find the distance between my skirt and the skirt of a potential Quantum Gateway
local xDiff = mySkirtX + 5 -- Using 5 because that's half the size of a Quantum Gateway, the largest stock structure
local zDiff = mySkirtZ + 5
-- Full extent of search rectangle
local x1 = pos.x - xDiff
local z1 = pos.z - zDiff
local x2 = pos.x + xDiff
local z2 = pos.z + zDiff
-- Find all the units in that rectangle
local units = GetUnitsInRect(Rect(x1, z1, x2, z2))
-- Filter it down to structures and experimentals only
units = EntityCategoryFilterDown(categories.STRUCTURE + categories.EXPERIMENTAL, units)
-- Bail if there's nothing in range
if not units[1] then return false end
for _, struct in units do
if struct ~= originUnit then
local structPhysics = struct:GetBlueprint().Physics
local structPos = struct:GetPosition()
-- These can be positive or negative, so we need to make them positive using math.abs
local xDist = math.abs(pos.x - structPos.x)
local zDist = math.abs(pos.z - structPos.z)
local skirtDiffx = mySkirtX + (structPhysics.SkirtSizeX / 2)
local skirtDiffz = mySkirtZ + (structPhysics.SkirtSizeZ / 2)
-- Check if the axis difference is smaller than the combined skirt distance
-- If it is, we overlap, and can't build here
if xDist < skirtDiffx and zDist < skirtDiffz then
return false
end
end
end
return true
end
-- Note: Includes allied units in selection!!
function GetEnemyUnitsInSphere(unit, position, radius)
local x1 = position.x - radius
local y1 = position.y - radius
local z1 = position.z - radius
local x2 = position.x + radius
local y2 = position.y + radius
local z2 = position.z + radius
local UnitsinRec = GetUnitsInRect(Rect(x1, z1, x2, z2))
-- Check for empty rectangle
if not UnitsinRec then
return UnitsinRec
end
local RadEntities = {}
for _, v in UnitsinRec do
local dist = VDist3(position, v:GetPosition())
if unit.Army ~= v.Army and dist <= radius then
table.insert(RadEntities, v)
end
end
return RadEntities
end
-- This function is like the one above, but filters out Allied units
function GetTrueEnemyUnitsInSphere(unit, position, radius, categories)
local x1 = position.x - radius
local y1 = position.y - radius
local z1 = position.z - radius
local x2 = position.x + radius
local y2 = position.y + radius
local z2 = position.z + radius
local UnitsinRec = GetUnitsInRect(Rect(x1, z1, x2, z2))
-- Check for empty rectangle
if not UnitsinRec then
return UnitsinRec
end
local RadEntities = {}
for _, v in UnitsinRec do
local dist = VDist3(position, v:GetPosition())
local vArmy = v.Army
if unit.Army ~= vArmy and not IsAlly(unit.Army, vArmy) and dist <= radius and EntityCategoryContains(categories or categories.ALLUNITS, v) then
table.insert(RadEntities, v)
end
end
return RadEntities
end
function GetDistanceBetweenTwoPoints(x1, y1, z1, x2, y2, z2)
return (math.sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2))
end
function GetDistanceBetweenTwoVectors(v1, v2)
return VDist3(v1, v2)
end
function XZDistanceTwoVectors(v1, v2)
return VDist2(v1[1], v1[3], v2[1], v2[3])
end
function GetVectorLength(v)
return math.sqrt(math.pow(v.x, 2) + math.pow(v.y, 2) + math.pow(v.z, 2))
end
function NormalizeVector(v)
local length = GetVectorLength(v)
if length > 0 then
local invlength = 1 / length
return Vector(v.x * invlength, v.y * invlength, v.z * invlength)
else
return Vector(0,0,0)
end
end
function GetDifferenceVector(v1, v2)
return Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
end
function GetDirectionVector(v1, v2)
return NormalizeVector(Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z))
end
function GetScaledDirectionVector(v1, v2, scale)
local vec = GetDirectionVector(v1, v2)
return Vector(vec.x * scale, vec.y * scale, vec.z * scale)
end
function GetMidPoint(v1, v2)
return Vector((v1.x + v2.x) * 0.5, (v1.y + v2.y) * 0.5, (v1.z + v2.z) * 0.5)
end
function GetRandomFloat(nmin, nmax)
return (Random() * (nmax - nmin) + nmin)
end
function GetRandomInt(nmin, nmax)
return math.floor(Random() * (nmax - nmin + 1) + nmin)
end
function GetRandomOffset(sx, sy, sz, scalar)
sx = sx * scalar
sy = sy * scalar
sz = sz * scalar
local x = Random() * sx - (sx * 0.5)
local y = Random() * sy
local z = Random() * sz - (sz * 0.5)
return x, y, z
end
function GetRandomOffset2(sx, sy, sz, scalar)
sx = sx * scalar
sy = sy * scalar
sz = sz * scalar
local x = Random(-1.0, 1.0) * sx - (sx * 0.5)
local y = Random(-1.0, 1.0) * sy
local z = Random(-1.0, 1.0) * sz - (sz * 0.5)
return x, y, z
end
function GetClosestVector(vFrom, vToList)
local dist, cDist, retVec = 0
if vToList then
dist = GetDistanceBetweenTwoVectors(vFrom, vToList[1])
retVec = vToList[1]
end
for kTo, vTo in vToList do
cDist = GetDistanceBetweenTwoVectors(vFrom, vTo)
if dist > cDist then
dist = cDist
retVec = vTo
end
end
return retVec
end
function Cross(v1, v2)
return Vector((v1.y * v2.z) - (v1.z * v2.y), (v1.z * v2.x) - (v1.x * v2.z), (v1.x * v2.y) - (v1.y - v2.x))
end
function DotP(v1, v2)
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z)
end
function GetAngleInBetween(v1, v2)
-- Normalize the vectors
local vec1 = {}
local vec2 = {}
vec1 = NormalizeVector(v1)
vec2 = NormalizeVector(v2)
local dotp = DotP(vec1, vec2)
return math.acos(dotp) * (360 / (math.pi * 2))
end
function UserConRequest(string)
if not Sync.UserConRequests then
Sync.UserConRequests = {}
end
table.insert(Sync.UserConRequests, string)
end
-----------------------------------------------------------------
-- TableCat - Concatenates multiple tables into one single table
-----------------------------------------------------------------
function TableCat(...)
local ret = {}
for index = 1, table.getn(arg) do
if arg[index] ~= nil then
for k, v in arg[index] do
table.insert(ret, v)
end
end
end
return ret
end