forked from mkafrin/PolyZone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComboZone.lua
More file actions
359 lines (318 loc) · 10.6 KB
/
ComboZone.lua
File metadata and controls
359 lines (318 loc) · 10.6 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
local mapMinX, mapMinY, mapMaxX, mapMaxY = -3700, -4400, 4500, 8000
local xDivisions = 34
local yDivisions = 50
local xDelta = (mapMaxX - mapMinX) / xDivisions
local yDelta = (mapMaxY - mapMinY) / yDivisions
ComboZone = {}
-- Finds all values in tblA that are not in tblB, using the "id" property
local function tblDifference(tblA, tblB)
local diff
for _, a in ipairs(tblA) do
local found = false
for _, b in ipairs(tblB) do
if b.id == a.id then
found = true
break
end
end
if not found then
diff = diff or {}
diff[#diff+1] = a
end
end
return diff
end
local function _differenceBetweenInsideZones(insideZones, newInsideZones)
local insideZonesCount, newInsideZonesCount = #insideZones, #newInsideZones
if insideZonesCount == 0 and newInsideZonesCount == 0 then
-- No zones to check
return false, nil, nil
elseif insideZonesCount == 0 and newInsideZonesCount > 0 then
-- Was in no zones last check, but in 1 or more zones now (just entered all zones in newInsideZones)
return true, copyTbl(newInsideZones), nil
elseif insideZonesCount > 0 and newInsideZonesCount == 0 then
-- Was in 1 or more zones last check, but in no zones now (just left all zones in insideZones)
return true, nil, copyTbl(insideZones)
end
-- Check for zones that were in insideZones, but are not in newInsideZones (zones the player just left)
local leftZones = tblDifference(insideZones, newInsideZones)
-- Check for zones that are in newInsideZones, but were not in insideZones (zones the player just entered)
local enteredZones = tblDifference(newInsideZones, insideZones)
local isDifferent = enteredZones ~= nil or leftZones ~= nil
return isDifferent, enteredZones, leftZones
end
local function _circleRectCollide(circleX, circleY, radius, rectX, rectY, rectWidth, rectLength)
-- temporary variables to set edges for testing
local testX = circleX
local testY = circleY
-- which edge is closest?
if circleX < rectX then testX = rectX -- test left edge
elseif circleX > rectX + rectWidth then testX = rectX + rectWidth end -- right edge
if circleY < rectY then testY = rectY -- top edge
elseif circleY > rectY + rectLength then testY = rectY + rectLength end -- bottom edge
-- get distance from closest edges
local distX = circleX - testX
local distY = circleY - testY
return distX * distX + distY * distY < radius * radius
end
local function _addZoneToRows(rows, zone)
local radius = zone.radius or zone.boundingRadius
local minY = (zone.center.y - radius - mapMinY) // yDelta
local maxY = (zone.center.y + radius - mapMinY) // yDelta
for i=minY, maxY do
local row = rows[i] or {}
row[#row+1] = zone
rows[i] = row
end
end
local function _zonesInGridCell(x, y, zones)
local zonesInCell = {}
local startX = mapMinX + xDelta * x
local startY = mapMinY + yDelta * y
for _, zone in ipairs(zones) do
-- For each zone, append to zonesInCell IF it is inside the grid cell at x,y
local zoneCenter = zone.center
local radius = zone.radius or zone.boundingRadius
if _circleRectCollide(zoneCenter.x, zoneCenter.y, radius, startX, startY, xDelta, yDelta) then
zonesInCell[#zonesInCell+1] = zone
end
end
return zonesInCell
end
local function _getGridCell(pos)
local x = (pos.x - mapMinX) // xDelta
local y = (pos.y - mapMinY) // yDelta
return x, y
end
function ComboZone:draw()
local zones = self.zones
for i=1, #zones do
local zone = zones[i]
if zone and not zone.destroyed then
zone:draw()
end
end
end
local function _initDebug(zone, options)
if options.debugBlip then zone:addDebugBlip() end
if not options.debugPoly then
return
end
Citizen.CreateThread(function()
while not zone.destroyed do
zone:draw()
Citizen.Wait(0)
end
end)
end
function ComboZone:new(zones, options)
options = options or {}
local rows = {}
-- Add a unique id for each zone in the ComboZone and add to rows cache
for i=1, #zones do
local zone = zones[i]
if zone then
zone.id = i
end
_addZoneToRows(rows, zone)
end
local useGrid = options.useGrid
if useGrid == nil and #zones >= 25 then
useGrid = true
end
local zone = {
name = tostring(options.name) or nil,
zones = zones,
useGrid = useGrid,
rows = rows,
grid = {},
debugPoly = options.debugPoly or false,
data = options.data or {},
isComboZone = true,
}
setmetatable(zone, self)
self.__index = self
return zone
end
function ComboZone:Create(zones, options)
local zone = ComboZone:new(zones, options)
_initDebug(zone, options)
AddEventHandler("polyzone:pzcomboinfo", function ()
zone:printInfo()
end)
return zone
end
function ComboZone:getZones(point)
if not self.useGrid then
return self.zones
end
local grid = self.grid
local gridX, gridY = _getGridCell(point)
local row = grid[gridY]
if row == nil then
row = {}
end
if row[gridX] == nil then
local zonesInCell = _zonesInGridCell(gridX, gridY, self.rows[gridY] or {})
row[gridX] = zonesInCell
grid[gridY] = row
end
return grid[gridY][gridX]
end
function ComboZone:AddZone(zone)
local zones = self.zones
local newIndex = #zones+1
zone.id = newIndex
zones[newIndex] = zone
self.grid = {}
_addZoneToRows(self.rows, zone)
if self.useGrid == nil and newIndex >= 25 then
self.useGrid = true
end
if self.debugBlip then zone:addDebugBlip() end
end
function ComboZone:isPointInside(point, zoneName)
if self.destroyed then
print("[PolyZone] Warning: Called isPointInside on destroyed zone {name=" .. self.name .. "}")
return false, {}
end
local zones = self:getZones(point)
if #zones == 0 then return false end
for i=1, #zones do
local zone = zones[i]
if zone and (zoneName == nil or zoneName == zone.name) and zone:isPointInside(point) then
return true, zone
end
end
return false, nil
end
function ComboZone:isPointInsideExhaustive(point, insideZones)
if self.destroyed then
print("[PolyZone] Warning: Called isPointInside on destroyed zone {name=" .. self.name .. "}")
return false, {}
end
if insideZones ~= nil then
insideZones = clearTbl(insideZones)
else
insideZones = {}
end
local zones = self:getZones(point)
if #zones == 0 then return false, insideZones end
for i=1, #zones do
local zone = zones[i]
if zone and zone:isPointInside(point) then
insideZones[#insideZones+1] = zone
end
end
return #insideZones > 0, insideZones
end
function ComboZone:destroy()
PolyZone.destroy(self)
local zones = self.zones
for i=1, #zones do
local zone = zones[i]
if zone and not zone.destroyed then
zone:destroy()
end
end
end
function ComboZone:onPointInOut(getPointCb, onPointInOutCb, waitInMS)
-- Localize the waitInMS value for performance reasons (default of 500 ms)
local _waitInMS = 500
if waitInMS ~= nil then _waitInMS = waitInMS end
Citizen.CreateThread(function()
local isInside = nil
local insideZone = nil
while not self.destroyed do
if not self.paused then
local point = getPointCb()
local newIsInside, newInsideZone = self:isPointInside(point)
if newIsInside ~= isInside then
onPointInOutCb(newIsInside, point, newInsideZone or insideZone)
isInside = newIsInside
insideZone = newInsideZone
end
end
Citizen.Wait(_waitInMS)
end
end)
end
function ComboZone:onPointInOutExhaustive(getPointCb, onPointInOutCb, waitInMS)
-- Localize the waitInMS value for performance reasons (default of 500 ms)
local _waitInMS = 500
if waitInMS ~= nil then _waitInMS = waitInMS end
Citizen.CreateThread(function()
local isInside, insideZones = nil, {}
local newIsInside, newInsideZones = nil, {}
while not self.destroyed do
if not self.paused then
local point = getPointCb()
newIsInside, newInsideZones = self:isPointInsideExhaustive(point, newInsideZones)
local isDifferent, enteredZones, leftZones = _differenceBetweenInsideZones(insideZones, newInsideZones)
if newIsInside ~= isInside or isDifferent then
isInside = newIsInside
insideZones = copyTbl(newInsideZones)
onPointInOutCb(isInside, point, insideZones, enteredZones, leftZones)
end
end
Citizen.Wait(_waitInMS)
end
end)
end
function ComboZone:onPlayerInOut(onPointInOutCb, waitInMS)
self:onPointInOut(PolyZone.getPlayerPosition, onPointInOutCb, waitInMS)
end
function ComboZone:onPlayerInOutExhaustive(onPointInOutCb, waitInMS)
self:onPointInOutExhaustive(PolyZone.getPlayerPosition, onPointInOutCb, waitInMS)
end
function ComboZone:addEvent(eventName, zoneName)
if self.events == nil then self.events = {} end
local internalEventName = eventPrefix .. eventName
RegisterNetEvent(internalEventName)
self.events[eventName] = AddEventHandler(internalEventName, function (...)
if self:isPointInside(PolyZone.getPlayerPosition(), zoneName) then
TriggerEvent(eventName, ...)
end
end)
end
function ComboZone:removeEvent(name)
PolyZone.removeEvent(self, name)
end
function ComboZone:addDebugBlip()
self.debugBlip = true
local zones = self.zones
for i=1, #zones do
local zone = zones[i]
if zone then zone:addDebugBlip() end
end
end
function ComboZone:printInfo()
local zones = self.zones
local polyCount, boxCount, circleCount, entityCount, comboCount = 0, 0, 0, 0, 0
for i=1, #zones do
local zone = zones[i]
if zone then
if zone.isEntityZone then entityCount = entityCount + 1
elseif zone.isCircleZone then circleCount = circleCount + 1
elseif zone.isComboZone then comboCount = comboCount + 1
elseif zone.isBoxZone then boxCount = boxCount + 1
elseif zone.isPolyZone then polyCount = polyCount + 1 end
end
end
local name = self.name ~= nil and ("\"" .. self.name .. "\"") or nil
print("-----------------------------------------------------")
print("[PolyZone] Info for ComboZone { name = " .. tostring(name) .. " }:")
print("[PolyZone] Total zones: " .. #zones)
if boxCount > 0 then print("[PolyZone] BoxZones: " .. boxCount) end
if circleCount > 0 then print("[PolyZone] CircleZones: " .. circleCount) end
if polyCount > 0 then print("[PolyZone] PolyZones: " .. polyCount) end
if entityCount > 0 then print("[PolyZone] EntityZones: " .. entityCount) end
if comboCount > 0 then print("[PolyZone] ComboZones: " .. comboCount) end
print("-----------------------------------------------------")
end
function ComboZone:setPaused(paused)
self.paused = paused
end
function ComboZone:isPaused()
return self.paused
end