I need to create lua script to encode polyline coordinates using google maps polyline encoding algorithm. This has to be for lua5.1 as the it is intended to be run inside Redis which is equipped with lua5.1. The following code is based on google golang equivalent.
https://github.com/googlemaps/google-maps-services-go/blob/master/polyline.go
-- Encode a polyline in Google Maps format
function Encode(path)
local prevLat, prevLng = 0, 0
local out = ""
for _, point in ipairs(path) do
local lat = math.floor(point.Lat * 1e5)
local lng = math.floor(point.Lng * 1e5)
out = out .. encodeInt(lat - prevLat)
out = out .. encodeInt(lng - prevLng)
prevLat, prevLng = lat, lng
end
return out
end
-- Encode an int64 value in Google Maps format
function encodeInt(v)
print(v)
if v < 0 then
v = bit.bxor(bit.lshift(v, 1), 0xffffffff)
else
v = bit.lshift(v, 1)
end
local encoded_num = ""
while v >= 0x20 do
encoded_num = encoded_num .. string.char(0x20 + (bit.band(v, 0x1f)) + 63)
v = bit.rshift(v, 5)
end
encoded_num = encoded_num .. string.char(v + 63)
return encoded_num
end
local path = { { Lat = 38.5, Lng = -120.2 }, { Lat = 40.7, Lng = -120.95 }, { Lat = 43.252, Lng = -126.453 } }
local encoded_polyline = Encode(path)
print(encoded_polyline)
-- should output: _p~iF~ps|U_ulLnnqC_mqNvxq`#
However it outputs this error:
lua:2: attempt to index global 'bit' (a nil value)
It seems that bit is not available for lua5.1!.
if v < 0 then
v = bit.bxor(bit.lshift(v, 1), 0xffffffff)
else
v = bit.lshift(v, 1)
end
is equivalent to
v = v < 0 and -1 - 2*v or 2*v
You can also replace bit.band(v, 0x1f) with v % 32
and bit.rshift(v, 5) with (v - v%32)/32
Related
i really hope im right here, as on a discord server no one wants to help me for some reason.
We run a Project for a game called Garry's Mod and currently try to get a "Toxic Gas" Script Working.
But we face the following issue
[ERROR] gamemodes/zombierp/plugins/toxicgas/sh_plugin.lua:44: attempt to index local 'item' (a boolean value)
1. IsEquippingGasmask - gamemodes/zombierp/plugins/toxicgas/sh_plugin.lua:44
2. v - gamemodes/zombierp/plugins/toxicgas/sh_plugin.lua:121
3. unknown - gamemodes/helix/gamemode/core/libs/sh_plugin.lua:477
And me being fairly new to Lua, im just completly confused and dont know how to fix it.
Here is the Full Script
local config = {
smokeColor = Color(63, 127, 0),
smokeAlpha = 110,
maskItem = "gasmask",
smokeSpawnerDistance = 100 -- distance between the smoke emitters on the box line
}
local PLUGIN = PLUGIN
PLUGIN.name = "Toxic Gas"
PLUGIN.author = ""
PLUGIN.description = ""
PLUGIN.positions = PLUGIN.positions or {}
PLUGIN.smokeStacks = PLUGIN.smokeStacks or {}
function PLUGIN:LoadData()
PLUGIN.positions = self:GetData()
self:UpdateWorldData()
end
function PLUGIN:SaveData()
self:SetData(PLUGIN.positions)
self:UpdateWorldData()
end
function PLUGIN:UpdateWorldData()
SetNetVar("toxicGasPositions", PLUGIN.positions)
-- global netvar doesn't seem to sync without this
for _, ply in pairs(player.GetAll()) do
ply:SyncVars()
end
end
function PLUGIN:IsEquippingGasmask(ply)
local character = ply:GetCharacter()
if not character then return false end
local inventoryID = character:GetInventory():GetID()
local inventory = ix.item.inventories[inventoryID]
for x, items in pairs(inventory.slots) do
for y, item in pairs(items) do
if item.uniqueID == config.maskItem
and item:GetData("equip") == true then
return true
end
end
end
return false
end
local function GetBoxLine(min, max)
local deltaX = math.abs(min.x - max.x)
local deltaY = math.abs(min.y - max.y)
local lineStart, lineEnd
if deltaX < deltaY then
lineStart = Vector(min.x + (max.x - min.x) / 2, min.y, min.z)
lineEnd = Vector(min.x + (max.x - min.x) / 2, min.y + (max.y - min.y), min.z)
else
lineStart = Vector(min.x, min.y + (max.y - min.y) / 2, min.z)
lineEnd = Vector(min.x + (max.x - min.x), min.y + (max.y - min.y) / 2, min.z)
end
return lineStart, lineEnd
end
if SERVER then
function PLUGIN:Think()
for idx, gasBox in pairs(PLUGIN.positions) do
if PLUGIN.smokeStacks[idx] == nil then
local min, max = gasBox.min, gasBox.max
local startSmoke, endSmoke = GetBoxLine(min, max)
PLUGIN.smokeStacks[idx] = {
count = math.floor(startSmoke:Distance(endSmoke) / config.smokeSpawnerDistance),
stacks = {}
}
for i = 1, PLUGIN.smokeStacks[idx].count do
local smoke = ents.Create("env_smokestack")
smoke:SetPos(startSmoke + (endSmoke - startSmoke):GetNormalized() * (i) * config.smokeSpawnerDistance)
smoke:SetKeyValue("InitialState", "1")
smoke:SetKeyValue("WindAngle", "0 0 0")
smoke:SetKeyValue("WindSpeed", "0")
smoke:SetKeyValue("rendercolor", tostring(config.smokeColor))
smoke:SetKeyValue("renderamt", tostring(config.smokeAlpha))
smoke:SetKeyValue("SmokeMaterial", "particle/particle_smokegrenade.vmt")
smoke:SetKeyValue("BaseSpread", tostring(config.smokeSpawnerDistance))
smoke:SetKeyValue("SpreadSpeed", "10")
smoke:SetKeyValue("Speed", "32")
smoke:SetKeyValue("StartSize", "32")
smoke:SetKeyValue("EndSize", "32")
smoke:SetKeyValue("roll", "8")
smoke:SetKeyValue("Rate", "64")
smoke:SetKeyValue("JetLength", tostring(max.z - min.z))
smoke:SetKeyValue("twist", "6")
smoke:Spawn()
smoke:Activate()
smoke.Think = function()
if PLUGIN.positions[idx] == nil then
smoke:Remove()
end
end
PLUGIN.smokeStacks[idx].stacks[i] = smoke
end
end
end
for _, ply in pairs(player.GetAll()) do
local pos = ply:EyePos()
if not ply:Alive() then continue end
local canBreathe = false
if not canBreathe then
canBreathe = self:IsEquippingGasmask(ply)
end
if not canBreathe then
for _, gasBox in pairs(PLUGIN.positions) do
if pos:WithinAABox(gasBox.min, gasBox.max) then
ply.nextGasDamage = ply.nextGasDamage or CurTime()
if CurTime() >= ply.nextGasDamage then
ply.nextGasDamage = CurTime() + .75
ply:TakeDamage(6)
ix.util.Notify("You are choking. You need a gas mask.", ply)
end
break
end
end
end
end
end
end
if CLIENT then
-- toggles showing toxic gas boxes when in noclip/observer
CreateConVar("ix_toxicgas_observer", "0", FCVAR_ARCHIVE)
local function IsInRange(min, max, scale)
local localPos = LocalPlayer():GetPos()
local distance = min:Distance(max)
if localPos:Distance(min + ((max - min) / 2)) <= distance * scale then
return true
end
return false
end
function PLUGIN:PostDrawTranslucentRenderables()
local toxicGasPositions = GetNetVar("toxicGasPositions")
if toxicGasPositions == nil then return end
for _, gasBox in pairs(toxicGasPositions) do
local min, max = gasBox.min, gasBox.max
if not IsInRange(min, max, 3) then continue end
local observerCvar = GetConVar("ix_toxicgas_observer")
if LocalPlayer():IsAdmin()
and LocalPlayer():GetMoveType() == MOVETYPE_NOCLIP
and observerCvar and observerCvar:GetBool() then
render.DrawWireframeBox(min, Angle(), Vector(0, 0, 0), max - min, Color(142, 222, 131, 255), false)
local startSmoke, endSmoke = GetBoxLine(min, max)
render.DrawLine(startSmoke, endSmoke, Color(0, 255, 0), false)
end
end
end
function PLUGIN:HUDPaint()
-- this is an FPS killer tbh
--[[
local toxicGasPositions = game.GetWorld():GetNetVar("toxicGasPositions")
if toxicGasPositions == nil then return end
local inToxicGas = false
local center
local cornerDist
for _, gasBox in pairs(toxicGasPositions) do
local min, max = gasBox.min, gasBox.max
center = min + ((max - min) / 2)
cornerDist = min:Distance(max)
if LocalPlayer():EyePos():WithinAABox(min, max) then
inToxicGas = true
continue
end
end
if inToxicGas then
local isEquippingGasmask = self:IsEquippingGasmask(LocalPlayer())
local distance = LocalPlayer():EyePos():Distance(center)
ix.util.DrawBlurAt(0, 0, ScrW(), ScrH(), 1, 0.2, isEquippingGasmask and 50 or 255)
end
]]
end
end
ix.command.Add("AddToxicGas", {
description = "Adds a toxic gas box from where you're standing and where you're looking at.",
adminOnly = true,
OnRun = function(self, client)
local pos = client:GetPos()
local tr = client:GetEyeTrace()
if not tr then return end
local hitPos = tr.HitPos
table.insert(PLUGIN.positions, {
min = pos, max = hitPos
})
PLUGIN:SaveData()
return "Added toxic gas."
end
})
ix.command.Add("RemoveToxicGas", {
description = "Removes the closest toxic gas point relative to you.",
adminOnly = true,
OnRun = function(self, client)
local closestDistance = -1
local closestIndex = -1
for idx, gasBox in pairs(PLUGIN.positions) do
local min, max = gasBox.min, gasBox.max
local center = min + ((max - min) / 2)
local distance = client:GetPos():Distance(center)
if closestDistance == -1 or distance < closestDistance then
closestDistance = distance
closestIndex = idx
end
end
if closestIndex ~= -1 then
table.remove(PLUGIN.positions, closestIndex)
if PLUGIN.smokeStacks[closestIndex] then
for k, v in pairs(PLUGIN.smokeStacks[closestIndex].stacks) do
v:Remove()
end
table.remove(PLUGIN.smokeStacks, closestIndex)
end
PLUGIN:SaveData()
return "Removed 1 toxic gas box."
else
return "Could not find any toxic gas to remove!"
end
end
})
I Really hope someone can help me with that as im trying since 2 days now
Try replacing for y, item in pairs(items) do with for item, _ in pairs(items) do.
Reason: there is a chance that items is a set, i.e., a Lua table, in which keys are set members and values are true.
change line 44
if item.uniqueID == config.maskItem
either by refusing boolean values
if type(item) ~= 'boolean' and item.uniqueID == config.maskItem
or only allowing tables, because they could possibly contain .uniqueID
if type(item) == 'table' and item.uniqueID == config.maskItem
Since I have a collision check function in Lua:
function onCollision(obj1,obj2)
obj1x = obj1.left
obj1y = obj1.top
obj1w = obj1.width
obj1h = obj1.height
obj2x = obj2.left
obj2y = obj2.top
obj2w = obj2.width
obj2h = obj2.height
if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then
return true
end
end
And I did make some panels as bricks using array table on my form, with this function, I did collision check between ball with each form sides without problems.
function createBricks()
for row = 1, brickRows do
bricks[row] = {}
for col = 1, brickColumns do
local x = (col - 1) * (width + gap) -- x offset
local y = (row - 1) * (height + gap) -- y offset
local newBrick = createPanel(gamePanel)
newBrick.width = brickWidth
newBrick.height = brickHeight
newBrick.top = y + 15
newBrick.left = x + 15
newBrick.BorderStyle = 'bsNone'
if level == 1 then newBrick.color = '65407' -- green
elseif level == 2 then newBrick.color = '858083' -- red
elseif level == 3 then newBrick.color = '9125192' -- brown
elseif level == 4 then newBrick.color = math.random(8,65255) end
bricks[row][col] = newBrick
end
end
end
Next how to detect if the ball collided with the bricks?. So far I did:
for row = 1, brickRows do
bricks[row] = {}
for col = 1, brickColumns do
dBrick = bricks[row][col]
if onCollision(gameBall,dBrick) then
dBrick.destroy() -- destroy the collided brick
end
end
end
I want to learn how to implement this collision logic in VB Net script which VB script easier for me, I did the whole game project using VB Net, now I try to re-write the project using CE Lua.
Private brickArray(brickRows, brickColumns) As Rectangle
Private isBrickEnabled(brickRows, brickColumns) As Boolean
For rows As Integer = 0 To brickRows
For columns As Integer = 0 To brickColumns
If Not isBrickEnabled(rows, columns) Then Continue For
If gameBall.IntersectsWith(brickArray(rows, columns)) Then
isBrickEnabled(rows, columns) = False
If gameBall.X + 10 < brickArray(rows, columns).X Or _
gameBall.X > brickArray(rows, columns).X + brickArray(rows, columns).Width _
Then
xVel = -xVel
Else
yVel = -yVel
End If
End If
Next
Next
And also this private sub, how to write it CE Lua?
Sub loadBricks()
Dim xOffset As Integer = 75, yOffset As Integer = 100
For row As Integer = 0 To brickRows
For column As Integer = 0 To brickColumns
brickArray(row, column) = New Rectangle(xOffset, yOffset, brickWidth, brickHeight)
xOffset += brickWidth + 10
isBrickEnabled(row, column) = True
Next
yOffset += brickHeight + 10
xOffset = 75
Next
End Sub
Function getBrickCount() As Integer
Dim Count As Integer = 0
For Each brick As Boolean In isBrickEnabled
If brick = True Then Count += 1
Next
Return Count
End Function
function onCollision(obj1,obj2)
obj1x = obj1.left
obj1y = obj1.top
obj1w = obj1.width
obj1h = obj1.height
obj2x = obj2.left
obj2y = obj2.top
obj2w = obj2.width
obj2h = obj2.height
if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then
return true
end
end
Then to detect collision the collision and remove from the table:
-- Drawback table
local function tcount( t )
local c = 0
for k,v in pairs(t) do
c = c + 1
end
return c
end
local count = #brickArray
for x = 1, count do
if onCollision(gameBall, brickArray[x]) then
if gameBall.Left + 10 < brickArray[x].Left or gameBall.Left > brickArray[x].Left + brickArray[x].Width then
xVel = -xVel else yVel = -yVel
end
playSound(findTableFile('strikeball.wav'))
brickArray[x] = brickArray[count]
brickArray[x] = x
brickArray[count] = nil
brickArray[x].Visible = false
tcount(brickArray)
end
end
The code above detected the collision and remove the object from the table, but that is not removed from display. How to remove the bricks from the table and display, using Cheat Engine Lua script?.
Im using Dijkstra algorythm code from this site: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Lua
Unfortunately It doesnt work for current edges table.
I have determined that the problem disappears when I delete the connection from 35 -> 36, but It doesnt solve the problem.
-- Graph definition
local edges = {
[34] = {[35] = 1,[37] = 1,},
[35] = {[34] = 1,[36] = 1,[46] = 1,},
[36] = {[35] = 1,[37] = 1,},
[37] = {[34] = 1,[36] = 1,},
[38] = {[46] = 1,},
[46] = {[35] = 1,[38] = 1,},
}
-- Fill in paths in the opposite direction to the stated edges
function complete (graph)
for node, edges in pairs(graph) do
for edge, distance in pairs(edges) do
if not graph[edge] then graph[edge] = {} end
graph[edge][node] = distance
end
end
end
-- Create path string from table of previous nodes
function follow (trail, destination)
local path, nextStep = destination, trail[destination]
while nextStep do
path = nextStep .. " " .. path
nextStep = trail[nextStep]
end
return path
end
-- Find the shortest path between the current and destination nodes
function dijkstra (graph, current, destination, directed)
if not directed then complete(graph) end
local unvisited, distanceTo, trail = {}, {}, {}
local nearest, nextNode, tentative
for node, edgeDists in pairs(graph) do
if node == current then
distanceTo[node] = 0
trail[current] = false
else
distanceTo[node] = math.huge
unvisited[node] = true
end
end
repeat
nearest = math.huge
for neighbour, pathDist in pairs(graph[current]) do
if unvisited[neighbour] then
tentative = distanceTo[current] + pathDist
if tentative < distanceTo[neighbour] then
distanceTo[neighbour] = tentative
trail[neighbour] = current
end
if tentative < nearest then
nearest = tentative
nextNode = neighbour
end
end
end
unvisited[current] = false
current = nextNode
until unvisited[destination] == false or nearest == math.huge
return distanceTo[destination], follow(trail, destination)
end
-- Main procedure
print("Directed:", dijkstra(edges, 34, 38, true))
print("Undirected:", dijkstra(edges, 34, 38, false))
I recieve the output of inf, 38 with current egdes table content but when I delete the connection between 35 -> 36 it gives an good output - 3, 34 35 46 38
For easier understand im uploading the graphic representation of edges table: https://i.imgur.com/FFF22C1.png
As you can see the route is corrent when we start from 34 -> 35 -> 46 -> 38 but as I sad It works only when connection from 35 to 36 is not existing.
Why it is not working in the case showed in my code?
This is an example of Dijkstra algorithm implementation
-- Graph definition
local edges = {
[34] = {[35] = 1,[37] = 1,},
[35] = {[34] = 1,[36] = 1,[46] = 1,},
[36] = {[35] = 1,[37] = 1,},
[37] = {[34] = 1,[36] = 1,},
[38] = {[46] = 1,},
[46] = {[35] = 1,[38] = 1,},
}
local starting_vertex, destination_vertex = 34, 38
local function create_dijkstra(starting_vertex)
local shortest_paths = {[starting_vertex] = {full_distance = 0}}
local vertex, distance, heap_size, heap = starting_vertex, 0, 0, {}
return
function (adjacent_vertex, edge_length)
if adjacent_vertex then
-- receiving the information about adjacent vertex
local new_distance = distance + edge_length
local adjacent_vertex_info = shortest_paths[adjacent_vertex]
local pos
if adjacent_vertex_info then
if new_distance < adjacent_vertex_info.full_distance then
adjacent_vertex_info.full_distance = new_distance
adjacent_vertex_info.previous_vertex = vertex
pos = adjacent_vertex_info.index
else
return
end
else
adjacent_vertex_info = {full_distance = new_distance, previous_vertex = vertex, index = 0}
shortest_paths[adjacent_vertex] = adjacent_vertex_info
heap_size = heap_size + 1
pos = heap_size
end
while pos > 1 do
local parent_pos = (pos - pos % 2) / 2
local parent = heap[parent_pos]
local parent_info = shortest_paths[parent]
if new_distance < parent_info.full_distance then
heap[pos] = parent
parent_info.index = pos
pos = parent_pos
else
break
end
end
heap[pos] = adjacent_vertex
adjacent_vertex_info.index = pos
elseif heap_size > 0 then
-- which vertex neighborhood to ask for?
vertex = heap[1]
local parent = heap[heap_size]
heap[heap_size] = nil
heap_size = heap_size - 1
if heap_size > 0 then
local pos = 1
local last_node_pos = heap_size / 2
local parent_info = shortest_paths[parent]
local parent_distance = parent_info.full_distance
while pos <= last_node_pos do
local child_pos = pos + pos
local child = heap[child_pos]
local child_info = shortest_paths[child]
local child_distance = child_info.full_distance
if child_pos < heap_size then
local child_pos2 = child_pos + 1
local child2 = heap[child_pos2]
local child2_info = shortest_paths[child2]
local child2_distance = child2_info.full_distance
if child2_distance < child_distance then
child_pos = child_pos2
child = child2
child_info = child2_info
child_distance = child2_distance
end
end
if child_distance < parent_distance then
heap[pos] = child
child_info.index = pos
pos = child_pos
else
break
end
end
heap[pos] = parent
parent_info.index = pos
end
local vertex_info = shortest_paths[vertex]
vertex_info.index = nil
distance = vertex_info.full_distance
return vertex
end
end,
shortest_paths
end
local vertex, dijkstra, shortest_paths = starting_vertex, create_dijkstra(starting_vertex)
while vertex and vertex ~= destination_vertex do
-- send information about all adjacent vertexes of "vertex"
for adjacent_vertex, edge_length in pairs(edges[vertex]) do
dijkstra(adjacent_vertex, edge_length)
end
vertex = dijkstra() -- now dijkstra is asking you about the neighborhood of another vertex
end
if vertex then
local full_distance = shortest_paths[vertex].full_distance
local path = vertex
while vertex do
vertex = shortest_paths[vertex].previous_vertex
if vertex then
path = vertex.." "..path
end
end
print(full_distance, path)
else
print"Path not found"
end
Is there a way if a string is close to a string in a table it will replace it with the one in the table?
Like a spellcheck function, that searches through a table and if the input is close to one in the table it will fix it , so the one in the table and the string is the same?
You can use this code :) Reference code is from here : https://github.com/badarsh2/Algorithm-Implementations/blob/master/Levenshtein_distance/Lua/Yonaba/levenshtein.lua
local function min(a, b, c)
return math.min(math.min(a, b), c)
end
local function matrix(row,col)
local m = {}
for i = 1,row do m[i] = {}
for j = 1,col do m[i][j] = 0 end
end
return m
end
local function lev(strA,strB)
local M = matrix(#strA+1,#strB+1)
local i, j, cost
local row, col = #M, #M[1]
for i = 1, row do M[i][1] = i - 1 end
for j = 1, col do M[1][j] = j - 1 end
for i = 2, row do
for j = 2, col do
if (strA:sub(i - 1, i - 1) == strB:sub(j - 1, j - 1)) then cost = 0
else cost = 1
end
M[i][j] = min(M[i-1][j] + 1,M[i][j - 1] + 1,M[i - 1][j - 1] + cost)
end
end
return M[row][col]
end
local refTable = {"hell", "screen"}
local function getClosestWord(pInput, pTable, threesold)
cDist = -1
cWord = ""
for key, val in pairs(pTable) do
local levRes = lev(pInput, val)
if levRes < cDist or cDist == -1 then
cDist = levRes
cWord = val
end
end
print(cDist)
if cDist <= threesold then
return cWord
else
return pInput
end
end
a = getClosestWord("hello", refTable, 3)
b = getClosestWord("screw", refTable, 3)
print(a, b)
Third parameter is threesold, if min distance is higher than threesold, word is not replaced.
I try to parse gpx files and to output encoded polylines (Google algorithm)
test.gpx
<trkseg>
<trkpt lon="-120.2" lat="38.5"/>
<trkpt lon="-120.95" lat="40.7"/>
<trkpt lon="-126.453" lat="43.252"/>
</trkseg>
I managed most of it, but have trouble with encoding the numbers
gpx2epl:
file = io.open(arg[1], "r")
io.input(file)
--
function round(number, precision)
return math.floor(number*math.pow(10,precision)+0.5) / math.pow(10,precision)
end
function encodeNumber(number)
return number
end
--
local Olatitude = 0
local Olongitude = 0
--
while true do
local line = io.read()
if line == nil
then
break
end
if string.match(line, "trkpt") then
local latitude
local longitude
local encnum
latitude = string.match(line, 'lat="(.-)"')
longitude = string.match(line, 'lon="(.-)"')
latitude = round(latitude,5)*100000
longitude = round(longitude,5)*100000
encnum = encodeNumber(latitude-Olatitude)
print(encnum)
encnum = encodeNumber(longitude-Olongitude)
print(encnum)
Olatitude = latitude
Olongitude = longitude
end
end
This script produces the expected output (see: Google Link), with the exception of encoded latitude and longitude.
3850000
-12020000
220000
-75000
255200
-550300
Mapquest provides an implementation in Javascript:
function encodeNumber(num) {
var num = num << 1;
if (num < 0) {
num = ~(num);
}
var encoded = '';
while (num >= 0x20) {
encoded += String.fromCharCode((0x20 | (num & 0x1f)) + 63);
num >>= 5;
}
encoded += String.fromCharCode(num + 63);
return encoded;
}
Can this be done in Lua? Can somebody please help me out. I have no idea how to implement this in Lua.
Edit:
Based on Doug's advice, I did:
function encodeNumber(number)
local num = number
num = num * 2
if num < 0
then
num = (num * -1) - 1
end
while num >= 32
do
local num2 = 32 + (num % 32) + 63
print(string.char(num2))
num = num / 32
end
print(string.char(num + 63) .. "\n-----")
end
encodeNumber(3850000) -- _p~iF
encodeNumber(-12020000) -- ~ps|U
encodeNumber(220000) -- _ulL
encodeNumber(-75000) -- nnqC
encodeNumber(255200) -- _mqN
encodeNumber(-550300) -- vxq`#
It's near expected output, but only near ... Any hint?
Taking encodeNumber piecemeal...
var num = num << 1;
This is just num = num * 2
num = ~(num);
This is num = (- num) - 1
0x20 | (num & 0x1f)
Is equivalent to 32 + (num % 32)
num >>= 5
Is equivalent to num = math.floor(num / 32)
ADDENDUM
To concatenate the characters, use a table to collect them:
function encodeNumber(number)
local num = number
num = num * 2
if num < 0
then
num = (num * -1) - 1
end
local t = {}
while num >= 32
do
local num2 = 32 + (num % 32) + 63
table.insert(t,string.char(num2))
num = math.floor(num / 32) -- use floor to keep integer portion only
end
table.insert(t,string.char(num + 63))
return table.concat(t)
end