Your "license" is confusing and contradicts itself. I suggest either using a proper one, or getting rid of it. :P/>
A thing that's been confusing me for a long time is this: given that functions should either serve to speed up your code, reduce it, or make it more readable,
why do people insist on declaring bunches of the things in situations where they'll do none of the above?
For example, take this:
packByte(colors.white,true,true,true,true)
That results in something like
13 calls. You could've just used the number 240, which is what it'll resolve to
every single time. And you're
looping it. Argh.
And why don't people use more line breaks in their code! If I omitted the use of paragraphs in this post, people would complain about it being an unreadable wall of text. Then these same people turn around and code that way! Blagarg!
And while I'm complaining about random stuff, why do people prefer the later versions of Windows? The accessories have turned to rubbish. For example, Calculator - want access to binary/decimal conversions? Say goodbye to fractional support! You can't have both at once anymore. Why?
Who knows!And Paint! Paint! They ruined it! And
don't get me started on
Explorer! Aaaargh!
But I digress.
While there's a lot of "strange" logic in there, the only "incorrect" bit I could spot was in your loading function, when starting a new row. You reset "col" to 1, forgetting that you increased it by a further 1 on every iteration - meaning it should've been reset to 0 instead.
Although I've not tested them, here are re-writes of the two main functions in concern. They should at the least execute much faster:
Spoiler
local function nsave(filename, map)
local file, lastY = fs.open(filename, "wb"), 0
for ycount, row in pairs(map) do
for i = lastY + 1, ycount do file.write(240) end -- Let's not recompute static values.
lastY = ycount
local lastX = 0
for xcount, col in pairs(row) do
for i = lastX + 2, xcount do file.write(224) end
lastX = xcount
local bitPos = 0
while bit.blshift(1, bitPos) < col.c do bitPos = bitPos + 1 end -- Likely executes faster than solving then dividing by logarithms.
file.write((col.s and 128 or 0) + (col.e and 64 or 0) + (col.w and 32 or 0) + (col.p and 16 or 0) + bitPos) -- Absolutely executes faster than a dozen linked function calls.
end
-- "sleep" always pauses at least a 20th of a second. So instead:
os.queueEvent("moo")
os.pullEvent("moo")
end
file.close()
end
local function nload(filename)
local map, file, row, col = {}, fs.open(filename, "rb"), 0
while true do
local byte = file.read()
if not byte then
break
elseif byte == 240 then -- white, true on all, which is a newline thingy
row = row + 1
col = 0
os.queueEvent("moo")
os.pullEvent("moo")
elseif byte ~= 224 then
map[row] = map[row] or {}
map[row][col] = {["c"] = bit.blshift(1, bit.band(byte, 15)),
["s"] = bit.band(byte, 128) == 128,
["e"] = bit.band(byte, 64) == 64,
["w"] = bit.band(byte, 32) == 32,
["p"] = bit.band(byte, 16) == 16}
end
col = col + 1
end
currentMap = map -- ???
file.close()
return map
end
I sincerely suggest you not implement a binary format. You'd be better off with a textual format.
Erm…
why?From where I'm sitting, using textutils to serialise a table of data is
simpler… but
horribly inefficient, storage-wise.