|
1 | | -local raw = LoadResourceFile(GetCurrentResourceName(), GetResourceMetadata(GetCurrentResourceName(), 'postal_file')) |
2 | | -local postals = json.decode(raw) |
3 | | - |
4 | | -local nearest = nil |
5 | | -local pBlip = nil |
6 | | - |
7 | | --- thread for nearest and blip |
8 | | -Citizen.CreateThread( |
9 | | - function() |
10 | | - while true do |
11 | | - local x, y = table.unpack(GetEntityCoords(GetPlayerPed(-1))) |
12 | | - |
13 | | - local ndm = -1 -- nearest distance magnitude |
14 | | - local ni = -1 -- nearest index |
15 | | - for i, p in ipairs(postals) do |
16 | | - local dm = (x - p.x) ^ 2 + (y - p.y) ^ 2 -- distance magnitude |
17 | | - if ndm == -1 or dm < ndm then |
18 | | - ni = i |
19 | | - ndm = dm |
20 | | - end |
21 | | - end |
22 | | - |
23 | | - --setting the nearest |
24 | | - if ni ~= -1 then |
25 | | - local nd = math.sqrt(ndm) -- nearest distance |
26 | | - nearest = {i = ni, d = nd} |
27 | | - end |
28 | | - |
29 | | - -- if blip exists |
30 | | - if pBlip then |
31 | | - local b = {x = pBlip.p.x, y = pBlip.p.y} -- blip coords |
32 | | - local dm = (b.x - x) ^ 2 + (b.y - y) ^ 2 -- distance magnitude |
33 | | - if dm < config.blip.distToDelete ^ 2 then |
34 | | - -- delete blip if close |
35 | | - RemoveBlip(pBlip.hndl) |
36 | | - pBlip = nil |
37 | | - end |
38 | | - end |
39 | | - |
40 | | - Wait(100) |
41 | | - end |
42 | | - end |
43 | | -) |
44 | | --- text display thread |
45 | | -Citizen.CreateThread( |
46 | | - function() |
47 | | - while true do |
48 | | - if nearest and not IsHudHidden() then |
49 | | - local text = config.text.format:format(postals[nearest.i].code, nearest.d) |
50 | | - SetTextScale(0.42, 0.42) |
51 | | - SetTextFont(4) |
52 | | - SetTextOutline() |
53 | | - BeginTextCommandDisplayText('STRING') |
54 | | - AddTextComponentSubstringPlayerName(text) |
55 | | - EndTextCommandDisplayText(config.text.posX, config.text.posY) |
56 | | - end |
57 | | - Wait(0) |
58 | | - end |
59 | | - end |
60 | | -) |
61 | | - |
62 | | -RegisterCommand( |
63 | | - 'postal', |
64 | | - function(source, args, raw) |
65 | | - if #args < 1 then |
66 | | - if pBlip then |
67 | | - RemoveBlip(pBlip.hndl) |
68 | | - pBlip = nil |
69 | | - TriggerEvent( |
70 | | - 'chat:addMessage', |
71 | | - { |
72 | | - color = {255, 0, 0}, |
73 | | - args = { |
74 | | - 'Postals', |
75 | | - config.blip.deleteText |
76 | | - } |
77 | | - } |
78 | | - ) |
79 | | - end |
80 | | - return |
81 | | - end |
82 | | - local n = string.upper(args[1]) |
83 | | - |
84 | | - local fp = nil |
85 | | - for _, p in ipairs(postals) do |
86 | | - if string.upper(p.code) == n then |
87 | | - fp = p |
88 | | - end |
89 | | - end |
90 | | - |
91 | | - if fp then |
92 | | - if pBlip then |
93 | | - RemoveBlip(pBlip.hndl) |
94 | | - end |
95 | | - pBlip = {hndl = AddBlipForCoord(fp.x, fp.y, 0.0), p = fp} |
96 | | - SetBlipRoute(pBlip.hndl, true) |
97 | | - SetBlipSprite(pBlip.hndl, config.blip.sprite) |
98 | | - SetBlipColour(pBlip.hndl, config.blip.color) |
99 | | - SetBlipRouteColour(pBlip.hndl, config.blip.color) |
100 | | - BeginTextCommandSetBlipName('STRING') |
101 | | - AddTextComponentSubstringPlayerName(config.blip.blipText:format(pBlip.p.code)) |
102 | | - EndTextCommandSetBlipName(pBlip.hndl) |
103 | | - |
104 | | - TriggerEvent( |
105 | | - 'chat:addMessage', |
106 | | - { |
107 | | - color = {255, 0, 0}, |
108 | | - args = { |
109 | | - 'Postals', |
110 | | - config.blip.drawRouteText:format(fp.code) |
111 | | - } |
112 | | - } |
113 | | - ) |
114 | | - else |
115 | | - TriggerEvent( |
116 | | - 'chat:addMessage', |
117 | | - { |
118 | | - color = {255, 0, 0}, |
119 | | - args = { |
120 | | - 'Postals', |
121 | | - config.blip.notExistText |
122 | | - } |
123 | | - } |
124 | | - ) |
125 | | - end |
126 | | - end |
127 | | -) |
128 | | - |
129 | | ---[[Development shit]] |
130 | | -local dev = false |
131 | | -if dev then |
132 | | - local devLocal = json.decode(raw) |
133 | | - local next = 0 |
134 | | - |
135 | | - RegisterCommand( |
136 | | - 'setnext', |
137 | | - function(src, args, raw) |
138 | | - local n = tonumber(args[1]) |
139 | | - if n ~= nil then |
140 | | - next = n |
141 | | - print('next ' .. next) |
142 | | - return |
143 | | - end |
144 | | - print('invalid ' .. n) |
145 | | - end |
146 | | - ) |
147 | | - RegisterCommand( |
148 | | - 'next', |
149 | | - function(src, args, raw) |
150 | | - for i, d in ipairs(devLocal) do |
151 | | - if d.code == tostring(next) then |
152 | | - print('duplicate ' .. next) |
153 | | - return |
154 | | - end |
155 | | - end |
156 | | - local coords = GetEntityCoords(GetPlayerPed(-1)) |
157 | | - table.insert(devLocal, {code = tostring(next), x = coords.x, y = coords.y}) |
158 | | - print('insert ' .. next) |
159 | | - next = next + 1 |
160 | | - end |
161 | | - ) |
162 | | - RegisterCommand( |
163 | | - 'rl', |
164 | | - function(src, args, raw) |
165 | | - if #devLocal > 0 then |
166 | | - local data = table.remove(devLocal, #devLocal) |
167 | | - print('remove ' .. data.code) |
168 | | - print('next ' .. next) |
169 | | - next = next - 1 |
170 | | - else |
171 | | - print('invalid') |
172 | | - end |
173 | | - end |
174 | | - ) |
175 | | - RegisterCommand( |
176 | | - 'remove', |
177 | | - function(src, args, raw) |
178 | | - if #args < 1 then |
179 | | - print('invalid') |
180 | | - else |
181 | | - for i, d in ipairs(devLocal) do |
182 | | - if d.code == args[1] then |
183 | | - table.remove(devLocal, i) |
184 | | - print('remove ' .. d.code) |
185 | | - return |
186 | | - end |
187 | | - end |
188 | | - print('invalid') |
189 | | - end |
190 | | - end |
191 | | - ) |
192 | | - RegisterCommand( |
193 | | - 'json', |
194 | | - function(src, args, raw) |
195 | | - print(json.encode(devLocal)) |
196 | | - end |
197 | | - ) |
198 | | -end |
199 | | - |
200 | | -exports('getPostal', function() if nearest ~= nil then return postals[nearest.i].code else return nil end end) |
| 1 | +---@class PostalData : table<number, vec> |
| 2 | +---@field code string |
| 3 | +---@type table<number, PostalData> |
| 4 | +postals = nil |
| 5 | +CreateThread(function() |
| 6 | + postals = LoadResourceFile(GetCurrentResourceName(), GetResourceMetadata(GetCurrentResourceName(), 'postal_file')) |
| 7 | + postals = json.decode(postals) |
| 8 | + for i, postal in ipairs(postals) do postals[i] = { vec(postal.x, postal.y), code = postal.code } end |
| 9 | +end) |
| 10 | + |
| 11 | +---@class NearestResult |
| 12 | +---@field code string |
| 13 | +---@field dist number |
| 14 | +nearest = nil |
| 15 | + |
| 16 | +---@class PostalBlip |
| 17 | +---@field 1 vec |
| 18 | +---@field p PostalData |
| 19 | +---@field hndl number |
| 20 | +pBlip = nil |
| 21 | + |
| 22 | +exports('getPostal', function() return nearest and nearest.code or nil end) |
0 commit comments