Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ctmirrors.lua
- -- Script designed for DeSmuME
- -- Usage: Load the US ROM of Chrono Trigger DS, load your save, run this script, close the Lua window, then save your game
- -- You now have the following:
- -- Nu Arcana x7
- -- Uranian Mirror x1
- -- Pontic Mirror x2
- -- Promethean Mirror x1
- -- Aresian Mirror x2
- -- Item Encyclopedia entries for all of the above
- -- Lua is a terrible language and you should kill yourself if you like it
- -- If modifying this script for your own purposes, you may find this Google Sheet that I've created (and am still working on) to be useful
- -- https://docs.google.com/spreadsheets/d/1x0rUPHkSwbUqhCnvJYKhIgAJEVe2DUS-XyZxj1bwu5Y/pubhtml
- -- memory address to start looping through, and the maximum number of 4-byte item records that can be there
- local start_address = 0x02072ED4
- local length = 59
- -- array representing all items to add, and their item encyclopedia offsets
- -- items[i][1] = Item ID
- -- items[i][2] = Category ID
- -- items[i][3] = quantity (max 0x63, which is 99 in decimal)
- -- items[i][4] = value to be used by the code, leave it zero or Unexpected Things(tm) will happen
- local items = {
- { 0x2B, 0x30, 0x07, 0 }, -- Nu Arcana x7
- { 0x2C, 0x30, 0x01, 0 }, -- Uranian Mirror x1
- { 0x2D, 0x30, 0x02, 0 }, -- Pontic Mirror x2
- { 0x2E, 0x30, 0x01, 0 }, -- Promethean Mirror x1
- { 0x2F, 0x30, 0x01, 0 }, -- Hadean Mirror x1
- { 0x30, 0x30, 0x02, 0 } -- Aresian Mirror x2
- }
- -- item encyclopedia modifications to make
- -- encyclopedia[i][1] = address
- -- encyclopedia[i][2] = bitmask to be bitwise OR'd with the value at that address
- local encyclopedia = {
- { 0x0207454A, 0xF8 }, -- Everything except the Aresian Mirror
- { 0x0207454B, 0x01 } -- Aresian Mirror
- }
- -- Everything below this line is absolutely perfect (except for the fact that it's written in Lua)
- -- therefore, if you modify it, you're a bad person.
- -- unless you're modifying it to make it be in a different language and somehow still have it work in DeSmuME
- -- in which case, you're amazing, share your Lua library that's making that possible with me so I can be free as well
- -- WHY ARE LUA ARRAYS ONE-BASED
- -- WHY
- -- anyway, this is just to name my numeric indices so the code is more readable
- local item_id = 1
- local category_id = 2
- local quantity = 3
- local inv_slot = 4
- local address = 1
- local bitmask = 2
- -- also I'm just going to ignore lexical scoping by declaring everything I'm using as high up as possible
- -- this is because I come from real, non-toy languages where variables have to be declared at the beginning of the function
- -- besides, it ain't particularly clean to be declaring variables in the middle of your code
- -- also, Lua doesn't have any kind of "strict mode" and will happily let you use uninitialized variables
- local idx
- local itemidx = 1
- local curraddr
- local item
- -- okay, so.
- -- For some stupid reason, emulators are stuck on Lua 5.1
- -- this means no bitwise operators, as found in Lua 5.3
- -- to "gloss over" this, DeSmuME includes a language extension that adds a 'bit' library
- -- instead of, you know, using Lua 5.3
- -- yay for shoehorning in functionality that you could get by simply updating Lua?
- -- anyway
- -- Item encyclopedia entries, go.
- -- this is actually the easy part.
- for idx, item in ipairs( encyclopedia ) do
- memory.writebyte( item[address], bit.bor( memory.readbyte( item[address] ), item[bitmask] ) )
- end
- -- See? Look how easy that was.
- -- now the hard part, which is actually giving the items to the player
- -- look to see if each item already exists
- -- if it does, make a note of the address
- for idx = 0, length - 1 do
- curraddr = start_address + ( idx * 4 )
- for itemidx, item in ipairs( items ) do
- if memory.readbyte( curraddr ) == item[item_id] and
- memory.readbyte( curraddr + 1 ) == item[category_id] then
- item[inv_slot] = curraddr
- end
- end
- end
- -- now find an empty inventory slot for any item that the player doesn't already have
- itemidx = 1
- for idx = 0, length - 1 do -- see? zero-based arrays are far more intuitive from a programming perspective
- curraddr = start_address + ( idx * 4 ) -- because then math like this (essentially pointer math) Just Works(tm) without a shitty "gotta subtract one" in there
- while itemidx <= #items and items[itemidx][inv_slot] ~= 0 do
- itemidx = itemidx + 1 -- because Lua was designed by geniuses, we can't do itemidx += 1 or itemidx++
- end
- if itemidx > #items then
- break -- exit our search early if we've got addresses for all the items
- end
- if memory.readdword( curraddr ) == 0 then
- items[itemidx][inv_slot] = curraddr -- found an empty inventory slot, store that sucker
- end
- end
- -- now go through and actually do things
- for itemidx, item in ipairs( items ) do
- memory.writebyte( item[inv_slot] , item[item_id] )
- memory.writebyte( item[inv_slot] + 1, item[category_id] )
- memory.writebyte( item[inv_slot] + 2, item[quantity] )
- memory.writebyte( item[inv_slot] + 3, 0x00 ) -- just for sanity's sake (mmm, sake)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement