Been a while since I last did any lua scripting. Had water damage to my previous laptop, and haven't set this new one up with TAS tools.
Language: lua
for i = PosToIndex(basex-32,basey-32), PosToIndex((camx+boundx+32),(camy+boundy+32)) do
Let me see if I'm reading that right...
Start: PosToIndex(basex-32,basey-32)
End: PosToIndex((camx+boundx+32),(camy+boundy+32))
Step: 1 (
implied)
So this loop iterates from the index of the top-left corner to the index of the bottom-right corner. However wide the map is, this will also catch every tile to the right and left of our target region, except for the top row off the left and the bottom row off the right. I think I see why the checks for in-bounds, which do add a bit more processing.
Language: lua
local x1, x2= math.floor(camx/16), math.floor((camx+319)/16)
local y1, y2= math.floor(camy/16), math.floor((camy+223)/16)
for yTile= y1, y2 do
for xTile= x1, x2 do
local i= xTile + yTile*xblock
One quick thought here. You have your index, and which tile by x and y. You're also only looking at in-bounds tiles, so we shouldn't need checks for out of bounds. Well, aside from strange cases where the camera shouldn't be.
I don't happen to know whether the for loop would call the ending parameter multiple times. The PosToIndex((camx+boundx+32),(camy+boundy+32)) part. I set a variable above the loop and use that rather than the call to be sure it fetches the value and not make function calls every loop. Again, a habit of mine to be sure.
Language: lua
if unit == nil or workinglast>0 then
if (unit == nil) or (workinglast>0) then
I have bad memories about the order of operations involving compare and logical operations. From now on, I bracket the compares I want done in parentheses, just in case. I don't know how well my bad memories apply here, but I use parentheses readily for my own sanity.
As for your cache, every time the
working variable resets to zero (working == 0 ; workinglast ~= 0), assign an empty table to cache. Don't even bother trying to blank out the individual elements, let the garbage collector kill the older table. I'm assuming the map doesn't modify itself as long as
working remains zero.
Language: lua
cache[i]= cache[i] or GetBlock(x,y)
if cache[i] then
The
or logical operation will have a value equal to the left parameter if it is not
false or
nil, and won't even run the second parameter in that case. If the left parameter turns out to be
false or
nil, then it will use the second parameter, making the function calls if any exist there. It's a nifty shortcut to remember.
Language: lua
if val>0 then
if val==0x80 then -- WALL
col = 0x00ffffff -- white
line(x1,y1,x1,y2,col+op) -- left
line(x2,y1,x2,y2,col+op) -- right
elseif val==0x81 then -- CEILING
col = 0x00ffffff -- white
line(x1,y2,x2,y2,col+op) -- bottom
--And so on
I don't like a chain of elseifs in my code. I do whatever I can to turn it into a table instead:
Language: lua
local white = 0x00ffffff
local orange= 0x00ff8800
local opout = 0x33000000 -- outer opacity
local opin = 0x66000000 -- inner opacity
local op = 0xff000000
local DrawTile= {
[0]= function() end, --do nothing if zero
[0x80]= function(x1,y1,x2,y2)
line(x1,y1,x1,y2,white+op) -- left
line(x2,y1,x2,y2,white+op) -- right
end,
--and so on
}
local function DefaultDrawTile(x1,y1,x2,y2)
box(x1,y1,x2,y2,col+opin,col+opout)
end
--Pretend we're in your function now
local Fn= DrawTile[i] or DefaultDrawTile
Fn(x1,y1,x2,y2)
I've got just these thoughts to start. If it's still not fast enough, I can see if I can reduce the code further. These are all the points that strike out at me right now.