User File #21814299609428444

Upload All User Files

#21814299609428444 - NES Rad Racer - Not-so-basic Lua HUD (v2)

RadRacer_v2.lua
1024 downloads
Uploaded 4/1/2015 9:29 AM by FatRatKnight (see all 245)
It's starting to get a little crowded.
Mid-top: CPU car info. Figured out two unknowns and rearranged them to be just beneath the main speed, as they are base speed and RNG speed range. As well, I pick at the RNG and predict the next three potential velocities (white) the car-to-be will have.
Right: Listing of the RNG period of 28 numbers. If the RNG isn't at one of those 28, a second column and predicted values is shown.
Bottom-left: Timers, as before. Frame timer in both decimal and hexidecimal, and the time remaining.
Bottom-mid: Mostly the same as before. Position and road curve, speed and sub-speed, then distance and related calculations. Distance calculations now use an internal number to find a certain multiplier the game uses. The script is calibrated to report the remaining distance in five of the eight courses, since I can't find an automatic process to do so as of this time.
Bottom-right: These numbers are to be analyzed. The darkened ones are for bouncing off of cars. The lighter one is reporting which course I'm in.
Additionally, I have an extending green line and an orange line to indicate progress through the course.
--*****************************************************************************
local Draw= {}
--*****************************************************************************
--Coordinates is the top-left pixel of the 3x5 digit.
--Used for drawing compact, colored numbers.

local Px,Li= gui.pixel, gui.line

Draw[0]= function(x,y,c) -- ###
    Li(x  ,y  ,x  ,y+4,c)-- # #
    Li(x+2,y  ,x+2,y+4,c)-- # #
    Px(x+1,y  ,c)        -- # #
    Px(x+1,y+4,c)        -- ###
end

Draw[1]= function(x,y,c) --  #
    Li(x  ,y+4,x+2,y+4,c)-- ##
    Li(x+1,y  ,x+1,y+3,c)--  #
    Px(x  ,y+1,c)        --  #
end                      -- ###

Draw[2]= function(x,y,c) -- ###
    Li(x  ,y  ,x+2,y  ,c)--   #
    Li(x  ,y+3,x+2,y+1,c)-- ###
    Li(x  ,y+4,x+2,y+4,c)-- #
    Px(x  ,y+2,c)        -- ###
    Px(x+2,y+2,c)
end

Draw[3]= function(x,y,c) -- ###
    Li(x  ,y  ,x+1,y  ,c)--   #
    Li(x  ,y+2,x+1,y+2,c)-- ###
    Li(x  ,y+4,x+1,y+4,c)--   #
    Li(x+2,y  ,x+2,y+4,c)-- ###
end

Draw[4]= function(x,y,c) -- # #
    Li(x  ,y  ,x  ,y+2,c)-- # #
    Li(x+2,y  ,x+2,y+4,c)-- ###
    Px(x+1,y+2,c)        --   #
end                      --   #

Draw[5]= function(x,y,c) -- ###
    Li(x  ,y  ,x+2,y  ,c)-- #
    Li(x  ,y+1,x+2,y+3,c)-- ###
    Li(x  ,y+4,x+2,y+4,c)--   #
    Px(x  ,y+2,c)        -- ###
    Px(x+2,y+2,c)
end

Draw[6]= function(x,y,c) -- ###
    Li(x  ,y  ,x+2,y  ,c)-- #
    Li(x  ,y+1,x  ,y+4,c)-- ###
    Li(x+2,y+2,x+2,y+4,c)-- # #
    Px(x+1,y+2,c)        -- ###
    Px(x+1,y+4,c)
end
                         -- ###
Draw[7]= function(x,y,c) --   #
    Li(x  ,y  ,x+1,y  ,c)--  ##
    Li(x+2,y  ,x+1,y+4,c)--  #
end                      --  #

Draw[8]= function(x,y,c) -- ###
    Li(x  ,y  ,x  ,y+4,c)-- # #
    Li(x+2,y  ,x+2,y+4,c)-- ###
    Px(x+1,y  ,c)        -- # #
    Px(x+1,y+2,c)        -- ###
    Px(x+1,y+4,c)
end

Draw[9]= function(x,y,c) -- ###
    Li(x  ,y  ,x  ,y+2,c)-- # #
    Li(x+2,y  ,x+2,y+3,c)-- ###
    Li(x  ,y+4,x+2,y+4,c)--   #
    Px(x+1,y  ,c)        -- ###
    Px(x+1,y+2,c)
end

Draw[10]=function(x,y,c) --  #
    Li(x  ,y+1,x  ,y+4,c)-- # #
    Li(x+2,y+1,x+2,y+4,c)-- # #
    Px(x+1,y  ,c)        -- ###
    Px(x+1,y+3,c)        -- # #
end

Draw[11]=function(x,y,c) -- ##
    Li(x  ,y  ,x  ,y+4,c)-- # #
    Li(x+1,y  ,x+2,y+1,c)-- ##
    Li(x+1,y+4,x+2,y+3,c)-- # #
    Px(x+1,y+2,c)        -- ##
end

Draw[12]=function(x,y,c) --  #
    Li(x  ,y+1,x  ,y+3,c)-- # #
    Li(x+1,y  ,x+2,y+1,c)-- #
    Li(x+1,y+4,x+2,y+3,c)-- # #
end                      --  #

Draw[13]=function(x,y,c) -- ##
    Li(x  ,y  ,x  ,y+4,c)-- # #
    Li(x+2,y+1,x+2,y+3,c)-- # #
    Px(x+1,y  ,c)        -- # #
    Px(x+1,y+4,c)        -- ##
end

Draw[14]=function(x,y,c) -- ###
    Li(x  ,y  ,x  ,y+4,c)-- #
    Li(x+1,y  ,x+2,y  ,c)-- ##
    Li(x+1,y+4,x+2,y+4,c)-- #
    Px(x+1,y+2,c)        -- ###
end

Draw[15]=function(x,y,c) -- ###
    Li(x  ,y  ,x  ,y+4,c)-- #
    Li(x+1,y  ,x+2,y  ,c)-- ##
    Px(x+1,y+2,c)        -- #
end                      -- #

--*****************************************************************************
local function __DN_AnyBase(right, y, Number, c, bkgnd, div)
--*****************************************************************************
-- Works with any base from 2 to 16. Paints the input number.
-- Returns the x position where it would paint another digit.
-- It only works with integers. Rounds fractions toward zero.

    if div < 2 then return end  -- Prevents the function from never returning.

    local Digit= {}
    local Negative= false
    if Number < 0 then
        Number= -Number
        Negative= true
    end

    Number= math.floor(Number)
    c= c or "white"
    bkgnd= bkgnd or "clear"

    local i= 0
    if Number < 1 then
        Digit[1]= 0
        i= 1
    end

    while (Number >= 1) do
        i= i+1
        Digit[i]= Number % div
        Number= math.floor(Number/div)
    end

    if Negative then  i= i+1  end
    local x= right - i*4
    gui.box(x+1, y-1, right+1, y+5,bkgnd,bkgnd)

    i= 1
    while Draw[Digit[i]] do
        Draw[Digit[i]](right-2,y,c)
        right= right-4
        i=i+1
    end

    if Negative then
        gui.line(right, y+2,right-2,y+2,c)
        right= right-4
    end
    return right
end
--*****************************************************************************
local function DrawNum(right, y, Number, c, bkgnd)
--*****************************************************************************
-- Paints the input number as right-aligned. Decimal version.
    return __DN_AnyBase(right, y, Number, c, bkgnd, 10)
end
--*****************************************************************************
local function DrawNumx(right, y, Number, c, bkgnd)
--*****************************************************************************
-- Paints the input number as right-aligned. Hexadecimal version.
    return __DN_AnyBase(right, y, Number, c, bkgnd, 16)
end

-------------------------------------------------------------------------------
local R1u,R1s= memory.readbyte, memory.readbytesigned
local R2u,R2s= memory.readword, memory.readwordsigned


--*****************************************************************************
local function Roll(r) -- Apparently, this is the RNG function
--*****************************************************************************
    if r == 0 then return 1 end  -- Can't tell what, really.
    r= r*3
    if r >= 384 then r= r+1 end  -- High bit before r*3 probably determines +1
    return r%256                 -- Fit within one byte
end    -- Address 0059 holds the RNG value

--I've pre-generated the table.
--local Init_RNG= {0,1,3,9,27,81,243,218,143,174,11,33,99,41,123,113,83}
local Loop_RNG= {249,236,197,80,240,209,116,92,20,60,180,29,87,5,15,45,
                 135,150,195,74,222,155,210,119,101, 47,141,168}

--*****************************************************************************
local function r_tbl()
--*****************************************************************************
    local Clr= "grey"
    local RNG= R1u(0x0059)

    local Found= false
    for i= 1, #Loop_RNG do
        if RNG == Loop_RNG[i] then
            DrawNum(254,3+i*6,Loop_RNG[i],"white","black")
            Found= true
        else
            DrawNum(254,3+i*6,Loop_RNG[i],"green","black")
        end
    end
    if Found then return end

--Down here, I'm not inside the Loop_RNG. Predict other numbers, then
    DrawNum(240,9,RNG,"white","black")
    for i= 1, 28 do   -- Place a limit, just in case.
        RNG= Roll(RNG)
        if RNG == Loop_RNG[1] then return end      -- Ah, our Loop_RNG. Done!
        DrawNum(240,9+6*i,RNG,"orange","black")
    end
end

--*****************************************************************************
local function CarDisp()  -- Dem CPU cars, man! Needs to see 'em, I say!
--*****************************************************************************
    for i= 0, 2 do
        local X=  61 + 51*i
        local addr= 0x0520+i*16
        local clr,tmr= "green","grey"
        local dst= "grey"

        local ID= R1s(addr)
        if ID < 0 then  clr,tmr= "grey","green"
        else
            dst= "green"
            local distance= R1u(addr+2)
            if distance >= 103 then dst= "orange" end
            if distance >= 112 then dst= "white"  end
            if distance >= 124 then dst= "orange" end
        end

        DrawNumx(X+10,  9,R1u(addr+ 0),clr,"black")  -- ID
        DrawNum( X+20,  9,R1u(addr+ 3),tmr,"black")  -- Respawn timer
        DrawNum( X+30,  9,R1u(addr+ 6),tmr,"black")  -- Respawn timer init
        DrawNum( X+12, 16,R1u(addr+ 1),clr,"black")  -- Speed
        DrawNum( X+12, 23,R1u(addr+ 4),tmr,"black")  -- Respawn Speed (base)
        DrawNum( X+12, 30,R1u(addr+ 5),tmr,"black")  -- Respawn Speed (RNGplus)
        DrawNum( X+25, 16,R1u(addr+ 2),dst,"black")  -- Dist main
        DrawNumx(X+34, 16,R1u(addr+ 7),clr,"black")  -- Dist sub
        DrawNum (X+25, 23,R1s(addr+ 8),clr,"black")  -- X offset (lane shift)
        DrawNumx(X+34, 23,R1u(addr+11),clr,"black")  -- X sub

        DrawNumx(X+24, 31,R1u(addr+ 9),tmr,"black")  -- Unknowns
        DrawNumx(X+32, 31,R1u(addr+10),clr,"black")
        DrawNumx(X+24, 38,R1u(addr+12),clr,"black")
        DrawNumx(X+32, 38,R1u(addr+13),clr,"black")
        DrawNumx(X+24, 45,R1u(addr+14),clr,"black")
        DrawNumx(X+32, 45,R1u(addr+15),clr,"black")

--Predict the velocities of spawning cars: Three numbers in advance.
        local r= R1u(0x0059)
        for j= 0, 2 do
            r= Roll(r)
            DrawNum(X+12,37+6*j,
                R1u(addr+4)+bit.band(r,R1u(addr+5)),
                "white","black")
        end -- RNG loop

    end -- car loop
end -- function

local CourseDist= {
[0]=2084864,  -- 509 * 16 * 256
    2072576,  -- 506 * 16 * 256
    2084864,  -- 509 * 16 * 256
    2170880,  -- 530 * 16 * 256
    2093056,  -- 511 * 16 * 256   Haven't figured out what internals to
    2084864,  --Placeholder       pick from to get these distances.
    2084864,  --Placeholder       Hate manufacturing numbers myself. Eyah...
    2084864   --Placeholder
}
--*****************************************************************************
local function BasicHUD()
--*****************************************************************************
-- <=-138 >= 136 - Offroad!
    local HPos= R2s(0x0030,0x003F)
    local HClr= "green"
    if HPos <= -138 then HClr= "white" end   --Slowdown
    if HPos >=  136 then HClr= "white" end
    if HPos <= -147 then HClr= "orange" end  --Obstacles
    if HPos >=  147 then HClr= "orange" end

    DrawNum( 117,204,R1u(0x0038),"white" ,"black")   -- Speed (main)
    DrawNumx(125,204,R1u(0x0058),"grey"  ,"black")   -- Speed (sub)
    DrawNum(  62,183,R2u(0x0029),"yellow","black")   -- Frame timer (dec)
    DrawNumx( 62,189,R2u(0x0029),"yellow","black")   -- Frame timer (hex)
    DrawNum(  62,204,R1u(0x0062),"white" ,"black")   -- Game timer
    DrawNum( 137,188,R1s(0x03B0),"cyan"  ,"black")   -- Road curve
    DrawNum( 117,188,HPos       ,HClr    ,"black")   --Hor-Pos
    DrawNum( 254,210,R1u(0x00E5),"white" ,"black")   -- Stage?
--    DrawNum( 132, 45,R1u(0x0059),"white" ,"black")   -- RNG
    DrawNumx(254,183,R1u(0x005A),"grey"  ,"black")   -- BOING 1 (Need analysis)
    DrawNumx(254,189,R1u(0x005B),"grey"  ,"black")   -- BOING 2
    DrawNumx(254,195,R1u(0x005C),"grey"  ,"black")   -- BOING 3

    CarDisp()
    r_tbl()

--Distance crud
    local DM= R1u(0x00F9) -- Distance Multiplier
    local Dist= R1u(0x008F)*DM*256 + R1u(0x0031)*256 + R1u(0x0068)
    DrawNum( 116,218,Dist       ,"White" ,"black")   -- Distance traveled
    local Lost= R2u(0x0029)*255 - Dist - 121975
    DrawNum( 150,218,Lost       ,"orange","black")   -- Diff from theoretical

    local CD= CourseDist[R1u(0x00E5)] or 1
    local Remaining= CD-Dist
    local Clr= "cyan"
    if Remaining < 0 then Clr= "orange"; Remaining= -Remaining end
    DrawNum(116,225,Remaining,Clr,"black")

--Lines of Motivation
    Dist= Dist*256
    local Modu= math.floor((Dist%CD) * 256/CD) - 1
    Dist= math.floor(Dist/CD) - 1

    if     Dist >= 255 then gui.line(0,180,255 ,180,"orange")
    elseif Dist >=   0 then gui.line(0,180,Dist,180,"green")  end
    if     Modu >=   0 then gui.line(0,181,Modu,181,"orange") end
end
gui.register(BasicHUD)