Browse Source

Red XIII limits and intaller fix

Iñigo Valentin 9 hours ago
parent
commit
b986ab3d2f
3 changed files with 63 additions and 9 deletions
  1. 35 1
      data/data/scripts/battle/battle_ui.lua
  2. 9 0
      doc/TODO.md
  3. 19 8
      src/installer/InstallLog.cpp

+ 35 - 1
data/data/scripts/battle/battle_ui.lua

@@ -2188,6 +2188,29 @@ UiContainer.BattleUi = {
     -- @param index Index in the party or enemy list.
     -- @param index Index in the party or enemy list.
     -- @return A table with level, str (attack), def, mag, mdef, hp, hp_max, mp.
     -- @return A table with level, str (attack), def, mag, mdef, hp, hp_max, mp.
     get_combat_stats = function(self, side, index)
     get_combat_stats = function(self, side, index)
+        local stats = self:get_base_combat_stats(side, index)
+        local mods = self:get_combatant(side, index).stat_mods
+        if mods ~= nil then
+            for kind, percent in pairs(mods) do
+                if stats[kind] ~= nil then stats[kind] = stats[kind] + math.floor(stats[kind] * percent / 100) end
+            end
+        end
+        return stats
+    end,
+
+    --- Changes a stat modifier of a combatant, a percentage (-100 to 100) of its stat as in ff7.exe (0x5c7e55).
+    -- The modifiers last until the end of the battle.
+    --
+    -- @param kind Stat: str, mag, def, mdef, eva or dex.
+    -- @param delta Percentage added to the modifier.
+    change_stat_mod = function(self, side, index, kind, delta)
+        local combatant = self:get_combatant(side, index)
+        combatant.stat_mods = combatant.stat_mods or {}
+        combatant.stat_mods[kind] = math.max(-100, math.min(100, (combatant.stat_mods[kind] or 0) + delta))
+    end,
+
+    --- Gets the stats of a combatant without the modifiers of the limits. See {@see get_combat_stats}.
+    get_base_combat_stats = function(self, side, index)
         if side == Battle.TargetGroup.PARTY then
         if side == Battle.TargetGroup.PARTY then
             local char = Characters[self.party[index].character]
             local char = Characters[self.party[index].character]
             local stats = char.stats
             local stats = char.stats
@@ -2708,7 +2731,7 @@ UiContainer.BattleUi = {
     -- The limits of the kernel data that only heal, restore, or give and cure statuses are built from their
     -- The limits of the kernel data that only heal, restore, or give and cure statuses are built from their
     -- data (ff7.exe, ids 128 to 183): formula 2 with modifier 2 is statuses only, 2 with modifier 4 heals a
     -- data (ff7.exe, ids 128 to 183): formula 2 with modifier 2 is statuses only, 2 with modifier 4 heals a
     -- fraction (power / 32) of the maximum HP, and 0 with modifier 8 restores everything. The ones in
     -- fraction (power / 32) of the maximum HP, and 0 with modifier 8 restores everything. The ones in
-    -- SPECIAL_LIMITS are done by hand. TODO: The additional effects of the data (Red XIII's 22 and 23) are ignored.
+    -- SPECIAL_LIMITS are done by hand. Additional effects 22 and 23 (Red XIII) change stat modifiers of the user.
     --
     --
     -- @param id Attack ID of the technique.
     -- @param id Attack ID of the technique.
     get_special_limit = function(self, id)
     get_special_limit = function(self, id)
@@ -2724,6 +2747,14 @@ UiContainer.BattleUi = {
         }
         }
         if f == 2 and m == 4 then special.heal_fraction = k.power end
         if f == 2 and m == 4 then special.heal_fraction = k.power end
         if f == 0 and m == 8 then special.full = true end
         if f == 0 and m == 8 then special.full = true end
+        -- Additional effects 22 (evade) and 23 (strength) change a stat modifier of the user by (modifier - 100) percent.
+        local additional = k.additional
+        if additional ~= nil and additional.modifier ~= nil then
+            local kinds = {[22] = "eva", [23] = "str"}
+            if kinds[additional.effect] ~= nil then
+                special.user_mods = {{kind = kinds[additional.effect], delta = additional.modifier - 100}}
+            end
+        end
         for _, entry in ipairs(k.status or {}) do
         for _, entry in ipairs(k.status or {}) do
             if entry.mode == 1 then table.insert(special.cures, entry.status)
             if entry.mode == 1 then table.insert(special.cures, entry.status)
             else table.insert(special.statuses, entry.status) end
             else table.insert(special.statuses, entry.status) end
@@ -2754,6 +2785,9 @@ UiContainer.BattleUi = {
             applied = true
             applied = true
             local group = special.target == "enemies" and Battle.TargetGroup.ENEMIES or Battle.TargetGroup.PARTY
             local group = special.target == "enemies" and Battle.TargetGroup.ENEMIES or Battle.TargetGroup.PARTY
             -- Curing Death (Pulse of Life) reaches the dead allies too.
             -- Curing Death (Pulse of Life) reaches the dead allies too.
+            for _, mod in ipairs(special.user_mods or {}) do
+                self:change_stat_mod(Battle.TargetGroup.PARTY, index, mod.kind, mod.delta)
+            end
             local targets = current.targets
             local targets = current.targets
             for _, id in ipairs(special.cures or {}) do
             for _, id in ipairs(special.cures or {}) do
                 if id == BattleStatus.DEATH and group == Battle.TargetGroup.PARTY then
                 if id == BattleStatus.DEATH and group == Battle.TargetGroup.PARTY then

+ 9 - 0
doc/TODO.md

@@ -23,3 +23,12 @@ First step: play the scene with log level 3 and look at the time between the `di
 1/256 of a pixel per frame, sign of the speed) has not been checked against the original game. The far
 1/256 of a pixel per frame, sign of the speed) has not been checked against the original game. The far
 layer of mds5_4 has no such opcodes and still scrolls with the rest, and it is not known why it should
 layer of mds5_4 has no such opcodes and still scrolls with the rest, and it is not known why it should
 not.
 not.
+
+## Battle: special limits
+
+Not done yet (kernel data in ff7.exe, ids 170-176, see `get_special_limit` in `battle_ui.lua`):
+
+- Cait Sith's Slots and Dice (additional effects 8 and 15, formula 6 with power 255): they need a small
+  interactive piece and the random effects of the reels.
+- Vincent's transformations (additional effect 3 with modifiers 0 to 3): they change the model, the
+  stats and the commands during the battle.

+ 19 - 8
src/installer/InstallLog.cpp

@@ -34,23 +34,33 @@ class InstallLog::Tee : public std::streambuf{
         /**
         /**
          * Writes directly to the original stream, without capturing it.
          * Writes directly to the original stream, without capturing it.
          */
          */
-        void WriteOriginal(const std::string& text){original_->sputn(text.data(), text.size());}
+        void WriteOriginal(const std::string& text){
+            std::lock_guard<std::mutex> lock(mutex_);
+            original_->sputn(text.data(), text.size());
+        }
 
 
     protected:
     protected:
 
 
         int_type overflow(int_type c) override{
         int_type overflow(int_type c) override{
             if (c == traits_type::eof()) return traits_type::not_eof(c);
             if (c == traits_type::eof()) return traits_type::not_eof(c);
-            original_->sputc(static_cast<char>(c));
-            if (c == '\n'){
-                std::string line;
-                line.swap(line_);
-                if (!line.empty()) log_.AddCaptured(line);
+            // The streams are written from several threads (Ogre's worker threads, the audio conversions), so
+            // the line buffer and the original buffer are only touched with the lock. The line is sent to the
+            // log without it, since the log may write to the streams again.
+            std::string line;
+            {
+                std::lock_guard<std::mutex> lock(mutex_);
+                original_->sputc(static_cast<char>(c));
+                if (c == '\n') line.swap(line_);
+                else line_ += static_cast<char>(c);
             }
             }
-            else line_ += static_cast<char>(c);
+            if (!line.empty()) log_.AddCaptured(line);
             return c;
             return c;
         }
         }
 
 
-        int sync() override{return original_->pubsync();}
+        int sync() override{
+            std::lock_guard<std::mutex> lock(mutex_);
+            return original_->pubsync();
+        }
 
 
     private:
     private:
 
 
@@ -58,6 +68,7 @@ class InstallLog::Tee : public std::streambuf{
         InstallLog& log_;
         InstallLog& log_;
         std::streambuf* original_;
         std::streambuf* original_;
         std::string line_;
         std::string line_;
+        std::mutex mutex_;
 };
 };
 
 
 InstallLog::InstallLog(): counts_{0, 0, 0}{}
 InstallLog::InstallLog(): counts_{0, 0, 0}{}