From 510be9a9a16b36a27e4951202bb53e2faa924ab5 Mon Sep 17 00:00:00 2001 From: shru Date: Fri, 14 Dec 2018 16:57:56 -0500 Subject: [PATCH] Item Randomizer works! Need full database though!! --- README.md | 4 +++ src/conf.lua | 41 +++++++++++++++++++++++ src/database/items.lua | 31 +++++++++++++++-- src/item_deck.lua | 9 +++-- src/main.lua | 62 ++++++++++++++++++---------------- src/tsc_file.lua | 75 +++++++++++++++++++++++++++++++----------- 6 files changed, 166 insertions(+), 56 deletions(-) create mode 100644 src/conf.lua diff --git a/README.md b/README.md index 31a2607..aafae44 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ Cave Story Randomizer ===================== +Issues +------ + +- Bubbler (and other weapons?) can't break blocks in the First Cave. diff --git a/src/conf.lua b/src/conf.lua new file mode 100644 index 0000000..7a22133 --- /dev/null +++ b/src/conf.lua @@ -0,0 +1,41 @@ +if io then + io.stdout:setvbuf("no") +end + +local seed = os.time() +math.randomseed(seed) + +function love.conf(t) + t.window = { + title = "Cave Story Randomizer", + -- icon = 'icon.png', + width = 640, + height = 480, + resizable = false, + } + t.version = '11.1' + t.console = false + t.identity = 'CaveStoryRandomizer' + t.accelerometerjoystick = false + t.gammacorrect = false + + -- t.releases = { + -- -- This is the name of the zip archive which contains your game. + -- title = 'Cave Story Randomizer', + -- -- This is the name of your game's executable. + -- package = 'Cave Story Randomizer', + -- loveVersion = '11.1', + -- version = nil, + -- author = 'shru', + -- email = nil, + -- description = nil, + -- homepage = 'https://shru.itch.io/cave-story-randomizer', + -- -- MacOS needs this. + -- identifier = 'CaveStoryRandomizer', + -- excludeFileList = { + -- -- '.+%.ase', + -- }, + -- compile = false, + -- releaseDirectory = 'releases', + -- } +end diff --git a/src/database/items.lua b/src/database/items.lua index 35bf472..d67685f 100644 --- a/src/database/items.lua +++ b/src/database/items.lua @@ -1,5 +1,21 @@ return { -- Weapons + -- wXXX = { + -- name = "", + -- map = "", + -- getText = "", + -- command = "", + -- displayCmd = "", + -- kind = "weapon", + -- }, + wBubbler = { + name = "Bubbler", + map = "Comu", + getText = "Got the =Bubbler=!", + command = "= 1, 'No applicable items!') -- Select an item. local selected = _.sample(applicable) - local index = self._indexMap[selected] + local index = indexMap[selected] table.remove(self._left, index) - self._indexMap[selected] = nil return selected end diff --git a/src/main.lua b/src/main.lua index 0037285..15b39c0 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,5 +1,3 @@ -io.stdout:setvbuf("no") - require 'lib.strict' Class = require 'lib.classic' @@ -8,7 +6,7 @@ Serpent = require 'lib.serpent' lf = love.filesystem -local LOG_LEVEL = 5 +local LOG_LEVEL = 4 local function _log(level, prefix, text, ...) if LOG_LEVEL >= level then print(prefix .. text, ...) @@ -20,9 +18,16 @@ function logNotice(...) _log(3, 'NOTICE: ', ...) end function logInfo(...) _log(4, 'INFO: ', ...) end function logDebug(...) _log(5, 'DEBUG: ', ...) end -local TSC_FILES = { - 'Pole.tsc', -} +local TSC_FILES = {} +do + local ITEM_DATA = require 'database.items' + for k, v in pairs(ITEM_DATA) do + local filename = v.map .. '.tsc' + if _.contains(TSC_FILES, filename) == false then + table.insert(TSC_FILES, filename) + end + end +end -- function love.load() -- -- readPXM('Pole.pxm') @@ -32,32 +37,42 @@ local TSC_FILES = { -- end function love.directorydropped(path) - -- Mount - assert(lf.mount(path, 'data')) - local items = lf.getDirectoryItems('/data') + -- Mount. + local mountPath = 'mounted-data' + assert(lf.mount(path, mountPath)) + local items = lf.getDirectoryItems('/' .. mountPath) local containsStage = _.contains(items, 'Stage') assert(containsStage) - local dirStage = '/data/Stage' + local dirStage = '/' .. mountPath .. '/Stage' local tscFiles = {} for _, filename in ipairs(TSC_FILES) do local path = dirStage .. '/' .. filename local TscFile = require 'tsc_file' tscFiles[filename] = TscFile(path) - - -- decoded = stringReplace(decoded, ITEM_DATA.wPolar.command, ITEM_DATA.iPanties.command) - -- decoded = stringReplace(decoded, ITEM_DATA.wPolar.getText, ITEM_DATA.iPanties.getText) - -- decoded = stringReplace(decoded, ITEM_DATA.wPolar.displayCmd, ITEM_DATA.iPanties.displayCmd) end + -- Create ItemDeck. local ItemDeck = require 'item_deck' local itemDeck = ItemDeck() - print(Serpent.line(itemDeck:getWeapon())) - print(Serpent.line(itemDeck:getAny())) - tscFiles['Pole.tsc']:writeTo('Testing.tsc') + -- Place random weapon in Hermit Gunsmith. + tscFiles['Pole.tsc']:replaceItem(itemDeck:getWeapon()) - -- Unmount + -- Replace all items. + for _, tscFile in pairs(tscFiles) do + while tscFile:hasUnreplacedItems() do + tscFile:replaceItem(itemDeck:getAny()) + end + end + + -- Write modified files. + for filename, tscFile in pairs(tscFiles) do + local path = '/data/Stage/' .. filename + tscFile:writeTo(path) + end + + -- Unmount. assert(lf.unmount(path)) end @@ -66,14 +81,3 @@ function love.keypressed(key) love.event.push('quit') end end - --- #0200 --- = 1 +end + +local function _stringReplace(text, needle, replacement) + local i = text:find(needle, 1, true) + if i == nil then + logWarning(('Unable to replace "%s" with "%s"'):format(needle, replacement)) + return text + end + local len = needle:len() + local j = i + len - 1 + assert((i % 1 == 0) and (i > 0) and (i <= j), tostring(i)) + assert((j % 1 == 0), tostring(j)) + local a = text:sub(1, i - 1) + local b = text:sub(j + 1) + return a .. replacement .. b +end + +function C:replaceItem(replacement) + assert(self:hasUnreplacedItems()) + local original = table.remove(self._unreplaced) + self._text = _stringReplace(self._text, original.command, replacement.command) + self._text = _stringReplace(self._text, original.getText, replacement.getText) + self._text = _stringReplace(self._text, original.displayCmd, replacement.displayCmd) + + local template = "[%s] %s -> %s" + logNotice(template:format(self._mapName, original.name, replacement.name)) end function C:writeTo(path) local encoded = self:_codec(self._text, 'encode') - local tmpFile, err = io.open(path, MODE_WRITE_BINARY) + local filepath = lf.getSourceBaseDirectory() .. path + logInfo('writing TSC to: ' .. filepath) + + local file, err = io.open(filepath, MODE_WRITE_ERASE_EXISTING) assert(err == nil, err) - tmpFile:write(encoded) - tmpFile:flush() - tmpFile:close() + file:write(encoded) + file:flush() + file:close() end function C:_codec(text, mode) @@ -66,18 +115,4 @@ function C:_codec(text, mode) return decoded end -local function stringReplace(text, needle, replacement) - local i = text:find(needle, 1, true) - if i == nil then - return text - end - local len = needle:len() - local j = i + len - 1 - assert((i % 1 == 0) and (i > 0) and (i <= j), tostring(i)) - assert((j % 1 == 0), tostring(j)) - local a = text:sub(1, i - 1) - local b = text:sub(j + 1) - return a .. replacement .. b -end - return C