User File #638730391668557799

Upload All User Files

#638730391668557799 - [DQ9] Show Rank/Seed/Location/35th AT output in Map Menu (USA)

DQ9_MapDetails.lua
1 download
Uploaded 7 hours ago by TKG (see all 8)
local tbl_addr = {
   -- (uint8) slot 1-8 base addr (status flag)
   0x0239C0E0,
   0x0239C2A4,
   0x0239C468,
   0x0239C62C,
   0x0239C7F0,
   0x0239C9B4,
   0x0239CB78,
   0x0239CD3C,

   -- (int32) check if map menu is open/closed
   0x020F97B8
}

local tbl_offset = {
   mapType = 0x1,       -- uint8 (normal/legacy)
   finalQuality = 0x4E, -- uint8
   mapSeed = 0x4C,      -- uint16
   location = 0x2C,     -- uint8
   mapName = 0x67       -- ascii (base addr)
}

local tbl_rankThreshold = {
   221, -- DD
   201, -- C9
   181, -- B5
   161, -- A1
   141, -- 8D
   121, -- 79
   101, -- 65
   81,  -- 51
   76,  -- 4C
   61,  -- 3D
   56,  -- 38
   2    -- 02
}

local tbl_ascii = {
   [2] = 0x20,  -- space
   [7] = 0x20,  -- space
   [8] = 0x40,  -- @
   [9] = 0x20,  -- space
   [12] = 0x20, -- space
   [13] = 0x28, -- opening parenthesis
}

-- parameters for the 35th AT position
local mult = 3248503605
local inc = 527630783

local function getHex(decimal, digits)
   return decimal and string.format("%0" .. digits .. "X", decimal) or "--"
end

local function getRank(FQ)
   for _, minFQ in ipairs(tbl_rankThreshold) do
      if FQ >= minFQ then return minFQ end
   end
end

local function getSpawn(seed)
   local hi = bit.rshift(seed, 16)
   local lo = bit.band(seed, 65535) * mult + inc
   local cr = bit.rshift(lo, 16)
   hi = bit.band(hi * mult + cr, 65535)
   return bit.band(hi, 32767)
end

local function populate_tbl_ascii(rank, seed, loc, spawn)

   -- rank (index 0-1), location (index 10-11)
   for i = 0, 1 do
      tbl_ascii[i] = string.byte(getHex(rank, 2), i + 1)
      tbl_ascii[i + 10] = string.byte(getHex(loc, 2), i + 1)
   end

   -- map seed (index 3-6)
   for i = 3, 6 do
      tbl_ascii[i] = string.byte(getHex(seed, 4), i - 2)
   end

   -- closing parenthesis
   local close = string.len(spawn) + 14
   tbl_ascii[close] = 0x29

   -- add spaces to the end
   for buffer = 1, 4 do 
      tbl_ascii[close + buffer] = 0x20
   end

   -- spawn (length 1-5)
   for i = 14, close - 1 do
      tbl_ascii[i] = string.byte(spawn, i-13)
   end
end

local function main()

   -- check if the map menu is open
   if memory.readdword(tbl_addr[9]) > 0 then
      for slot = 1, 8 do
         local baseAddr = tbl_addr[slot]

         -- check if each slot has a normal map
         if memory.readbyte(baseAddr + tbl_offset.mapType) == 1 then

            -- read/calculate map details
            local rank = getRank(memory.readbyte(baseAddr + tbl_offset.finalQuality))
            local seed = memory.readword(baseAddr + tbl_offset.mapSeed)
            local loc = memory.readbyte(baseAddr + tbl_offset.location)
            local spawn = getSpawn(seed)

            -- add map details to tbl_ascii
            populate_tbl_ascii(rank, seed, loc, spawn)

            -- write map details to memory
            for index, asciiChar in pairs(tbl_ascii) do
               memory.writebyte(baseAddr + tbl_offset.mapName + index, asciiChar)
            end
         end
      end
   end
end

gui.register (main)