Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
added resource data
  • Loading branch information
DevBlocky committed Mar 9, 2019
0 parents commit 744e66f
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
postals.json
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 BlockBa5her

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Nearest Postals
This script displays a nearest postal next to where PLD would go and also has a command to draw a route to a specific postal

## Installation
There are 2 ways to install it, and I recommend the first
1. Run the following command in a terminal
- `git clone https://github.com/blockba5her/nearest-postal.git`
2. Click the `Download` button up top to download the source code, then put it in your resources folder
3. As of now, this script supports 2 postal maps. From what I have seen, these are the 2 most popular
- `new-postals.json` -> [New and Improved Postals](https://forum.fivem.net/t/release-modified-street-names-w-postal-numbers/8717)
- `old-postals.json` -> [Original Postals](https://forum.fivem.net/t/release-modified-street-names-w-postal-numbers/8717)
4. To setup the postal map rename your file from what it was to `postals.json`

## Command
To draw a route to a certain postal, type `/postal [postalName]` and to remove just type `/postal`

It will automatically remove the route when within 150m of the destination

## Development
This script provides a simple way of working on a new postal map
1. In the script, enabled the local variable `dev` near the bottom
2. Restart the script into game
3. Teleport to the first postal code in numerical order
4. Type `setnext [postalCode]` where postalCode is the postal that you are at
5. Type `next` to insert it
6. Teleport to the next postal code in numerical order
7. Type `next` to insert it
8. Repeat from step 6 on

When done with that, you can print all of the postals you just inserted into console with the `json` command

## Discord
Join my [discord](https://discord.gg/ZcTayce) for support and more scripts
4 changes: 4 additions & 0 deletions __resource.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

client_script 'cl.lua'
file 'postals.json'
153 changes: 153 additions & 0 deletions cl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
local raw = LoadResourceFile(GetCurrentResourceName(), 'postals.json')
local postals = json.decode(raw)

local nearest = nil
local pBlip = nil

local function round(val, decimal)
return decimal and (math.floor((val * 10^decimal) + 0.5) / (10^decimal)) or (math.floor(val + 0.5))
end

-- thread for finding nearest postal
Citizen.CreateThread(function()
while true do
local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1)))

local nd = -1
local ni = -1
for i, p in ipairs(postals) do
local d = math.sqrt((x - p.x)^2 + (y - p.y)^2) -- pythagorean theorem
if nd == -1 or d < nd then
ni = i
nd = d
end
end

if ni ~= -1 then
nearest = {dist = nd, i = ni}
end

Wait(100)
end
end)
-- text display thread
Citizen.CreateThread(function()
while true do
if nearest then
local text = ('~y~Nearest Postal~w~: %s (~g~%sm~w~)'):format(postals[nearest.i].code, tostring(round(nearest.dist, 2)))
SetTextScale(0.42, 0.42)
SetTextFont(4)
SetTextProportional(false)
SetTextColour(255, 255, 255, 255)
SetTextEntry("STRING")
SetTextCentre(0)
SetTextOutline()
AddTextComponentString(text)
DrawText(0.175, 0.963)
end
Wait(2)
end
end)

-- blip thread
Citizen.CreateThread(function()
while true do
if pBlip then
local p = GetEntityCoords(GetPlayerPed(-1))
local b = {x = pBlip.p.x, y = pBlip.p.y}
local dx, dy = math.abs(p.x - b.x), math.abs(p.y - b.y)
local d = math.sqrt(dx^2 + dy^2)
if d < 100.0 then
RemoveBlip(pBlip.hndl)
pBlip = nil
end
end
Wait(100)
end
end)
RegisterCommand('postal', function(source, args, raw)
if #args < 1 then
if pBlip then
RemoveBlip(pBlip.hndl)
pBlip = nil
end
return
end
local n = string.upper(args[1])

local fp = nil
for _, p in ipairs(postals) do
if string.upper(p.code) == n then
fp = p
end
end

if fp then
if pBlip then
RemoveBlip(pBlip.hndl)
end
pBlip = {hndl = AddBlipForCoord(fp.x, fp.y, 0.0), p = fp}
SetBlipRoute(pBlip.hndl, true)
SetBlipColour(pBlip.hndl, 3)
SetBlipRouteColour(pBlip.hndl, 3)
BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName('Postal Route '..pBlip.p.code)
EndTextCommandSetBlipName(pBlip.hndl)

TriggerEvent('chat:addMessage', {
color = {255,0,0},
args = {
'Postals',
'Drawing a route to '..fp.code
}
})
else
TriggerEvent('chat:addMessage', {
color = {255,0,0},
args = {
'Postals',
'That postal code doesn\'t exist'
}
})
end
end)

--[[Development shit]]
local dev = false
if dev then
local devLocal = json.decode(raw)
local next = 0

RegisterCommand('setnext', function(src, args, raw)
local n = tonumber(args[1])
if n ~= nil then
next = n
print('next '..next)
return
end
print('invalid '..n)
end)
RegisterCommand('next', function(src, args, raw)
for i, d in ipairs(devLocal) do
if d.code == tostring(next) then
print('duplicate '..next)
return
end
end
local coords = GetEntityCoords(GetPlayerPed(-1))
table.insert(devLocal, {code = tostring(next), x = coords.x, y = coords.y})
print('insert '..next)
next = next + 1
end)
RegisterCommand('remove', function(src, args, raw)
if #devLocal > 0 then
local data = table.remove(devLocal, #devLocal)
print('remove '..data.code)
else
print('invalid')
end
end)
RegisterCommand('json', function(src, args, raw)
print(json.encode(devLocal))
end)
end
1 change: 1 addition & 0 deletions new-postals.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions old-postals.json

Large diffs are not rendered by default.

0 comments on commit 744e66f

Please sign in to comment.