Skip to content

Commit

Permalink
Pulling data from memory during game play and displaying on emu screen
Browse files Browse the repository at this point in the history
  • Loading branch information
nihonjinrxs committed Sep 12, 2022
0 parents commit fe248f2
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Lua.diagnostics.globals": [
"memory",
"emu",
"rom",
"gui"
]
}
45 changes: 45 additions & 0 deletions display.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local function generatePrintables(TrackedValues)
local printables = {
"ROM Name: "..rom.getfilename(),
"ROM Hash: "..rom.gethash("md5"),
}
if TrackedValues.map.parsedValue and TrackedValues.map.parsedValue == "None" then
return printables
end
local orderedKeys = {
"map", "level", "xp", "hp", "mp", "strength", "agility", "ap", "dp", "gold", "keys",
}
for i, key in ipairs(orderedKeys) do
local value = TrackedValues[key]
if value.parsedValue then
table.insert(printables, value.label..": "..value.parsedValue)
end
end
return printables
end

local function displayOSD(TrackedValues)
local printables = generatePrintables(TrackedValues)
local textX = 0
local textY = 10
local printablesCount = #(printables)
for i = 1,printablesCount do
gui.text(textX, textY, printables[i])
textY = textY + 10
end
end

local function logValues(TrackedValues)
local valuesCount = #(TrackedValues)
print("Found "..valuesCount.." tracked values:")
for _key,value in pairs(TrackedValues) do
if value.memValue then
print(" "..value.label..": "..value.memValue)
end
end
end

return {
displayOSD = displayOSD,
logValues = logValues,
}
41 changes: 41 additions & 0 deletions lookups.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local nonParseables = {
"level", "xp", "strength", "agility", "ap", "dp", "gold", "keys",
}

local maps = {
[0x00] = "None",
[0x01] = "World Map",
[0x02] = "Charlock Castle",
[0x03] = "Hauksness",
[0x04] = "Tantegel Castle",
[0x05] = "Throne Room",
[0x06] = "Dragonlord's Lair",
[0x07] = "Kol",
[0x08] = "Brecconary",
[0x09] = "Garinham",
[0x0A] = "Cantlin",
[0x0B] = "Rimuldar",
[0x0C] = "Sun Shrine",
[0x0D] = "Rain Shrine",
[0x0E] = "Magic Temple",
[0x0F] = "Charlock B1",
[0x10] = "Charlock B2",
[0x11] = "Charlock B3",
[0x12] = "Charlock B4",
[0x13] = "Charlock B5",
[0x14] = "Charlock B6",
[0x15] = "Swamp Cave",
[0x16] = "Mountain Cave B1",
[0x17] = "Mountain Cave B2",
[0x18] = "Garin's Grave B1",
[0x19] = "Garin's Grave B2",
[0x1A] = "Garin's Grave B3",
[0x1B] = "Garin's Grave B4",
[0x1C] = "Erdrick's Cave B1",
[0x1D] = "Erdrick's Cave B2",
}

return {
nonParseables = nonParseables,
maps = maps,
}
64 changes: 64 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
_G.DWRAutoMapTracker = {}

-- Set the speed of the emulator
emu.speedmode("normal")

local parsers = require "parsers"
local display = require "display"
-- local tracker_gui = require "tracker_gui"

-- Declare variables to track
DWRAutoMapTracker.Trackables = {
playerX = { label = "Player X", address = 0x3A, size = 8 },
playerY = { label = "Player Y", address = 0x3B, size = 8 },
playerV = { label = "Player V", address = 0x3E, size = 8 },
playerH = { label = "Player H", address = 0x42, size = 8 },
map = { label = "Map", address = 0x45, size = 8 },
rngState = { label = "RNG State", address = 0x94, size = 16 },
xp = { label = "XP", address = 0xBA, size = 16 },
gold = { label = "Gold", address = 0xBC, size = 16 },
keys = { label = "Keys", address = 0xBF, size = 8 },
hp = { label = "HP", address = 0xC5, size = 8 },
hpMax = { label = "Max HP", address = 0xCA, size = 8 },
mp = { label = "MP", address = 0xC6, size = 8 },
mpMax = { label = "Max MP", address = 0xCB, size = 8 },
level = { label = "Level", address = 0xC7, size = 8 },
strength = { label = "Strength", address = 0xC8, size = 8 },
agility = { label = "Agility", address = 0xC9, size = 8 },
ap = { label = "AP", address = 0xCC, size = 8 },
dp = { label = "DP", address = 0xCD, size = 8 },
}
DWRAutoMapTracker.TrackedValues = {}

-- DWRAutoMapTracker.UI = tracker_gui.initUI()

-- Functions for updating memory state
local function readTrackableFromMemory(trackable)
if (trackable.size == 8) then
return {
label = trackable.label,
memValue = memory.readbyte(trackable.address)
}
elseif (trackable.size == 16) then
return {
label = trackable.label,
memValue = memory.readword(trackable.address)
}
end
end

local function updateTracked()
for key,value in pairs(DWRAutoMapTracker.Trackables) do
DWRAutoMapTracker.TrackedValues[key] = readTrackableFromMemory(value)
end
end

-- Start emulator loop
while true do
updateTracked()
parsers.parseAll(DWRAutoMapTracker.TrackedValues)
display.displayOSD(DWRAutoMapTracker.TrackedValues)
-- display.logValues(DWRAutoMapTracker.TrackedValues)

emu.frameadvance()
end
37 changes: 37 additions & 0 deletions parsers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local lookups = require "lookups"

local function noParse(byteValue)
return byteValue
end

local function parseMap(mapByte)
return lookups.maps[mapByte]
end

local function parseWithMax(valueByte, maxByte)
return string.format("%d / %d", valueByte, maxByte)
end

local function parseAll(TrackedValues)
for i,name in ipairs(lookups.nonParseables) do
if TrackedValues[name].memValue then
TrackedValues[name].parsedValue = noParse(TrackedValues[name].memValue)
end
end
if TrackedValues.hp.memValue and TrackedValues.hpMax.memValue then
TrackedValues.hp.parsedValue = parseWithMax(TrackedValues.hp.memValue, TrackedValues.hpMax.memValue)
end
if TrackedValues.mp.memValue and TrackedValues.mpMax.memValue then
TrackedValues.mp.parsedValue = parseWithMax(TrackedValues.mp.memValue, TrackedValues.mpMax.memValue)
end
if TrackedValues.map.memValue then
TrackedValues.map.parsedValue = parseMap(TrackedValues.map.memValue)
end
end

return {
parseAll = parseAll,
-- noParse = noParse,
-- parseMap = parseMap,
-- parseWithMax = parseWithMax,
}

0 comments on commit fe248f2

Please sign in to comment.