aedenthorn / ValheimMods

Other
148 stars 105 forks source link

[RepairRequiresMats] Fix: Cumulative Repair Cost for Higher Item Qualities #53

Closed brandon-clair closed 2 years ago

brandon-clair commented 3 years ago

I believe the Piece.Requirement() generated in RepairRecipe() needs to have its GetAmount() method overridden to return the cumulative amount of items required for the requested item level. By default, it will return only the requirements needed to craft or upgrade the item to the desired level. This (assuming a materialRequirementMult of 1.0) effectively makes, for instance, a level 1 Hammer cost 3 Wood and 2 Stone to repair, and a level 2 Hammer cost 1 Wood and 1 Stone.

Here's the current code:

https://github.com/aedenthorn/ValheimMods/blob/5159b855541b537d35ff529791d03ef862dda695/RepairRequiresMats/BepInExPlugin.cs#L225-L246

And here's what I'm guessing needs to happen. Consider this pseudocode, I'm not familiar enough with this language to know precisely how to implement the fix.

 for (int i = 0; i < fullReqs.Count; i++) 
 { 

     var req = new Piece.Requirement()
     { 
         m_resItem = fullReqs[i].m_resItem, 
         m_amountPerLevel = Mathf.FloorToInt(fullReqs[i].m_amountPerLevel * percent * materialRequirementMult.Value), 
         m_amount = Mathf.FloorToInt(fullReqs[i].m_amount * percent * materialRequirementMult.Value), 

         // Something like this? I don't know this language well enough to know how to override methods.
         private static GetAmount(Int item_quality) {
             int amount = self.m_amount;
             if (item_quality > 1) {
                 amount += self.amountPerLevel * item_quality;
             }
             return amount;
         }
     };

     // Use the overridden method
     if (req.GetAmount(item.m_quality) > 0) 
     { 
         reqs.Add(req); 
     } 
 } 
aedenthorn commented 3 years ago

it was a mistake to use GetAmount at all, since the requirement is a custom made one that doesn't actually use quality. see the new version

brandon-clair commented 3 years ago

Edit:

Nope, I probably just misunderstand the engine still. I just tried the latest release, 0.4.3, and it worked exactly as expected. Thanks for taking care of this!


Original Post:

Cool! I think I'm following along better, and there may still be a problem with this calculation, throwing off the game's recipe logic:

https://github.com/aedenthorn/ValheimMods/blob/eb62ed46c9cc696bfd67b70854e43f8c7e67a050/RepairRequiresMats/BepInExPlugin.cs#L263-L268

During the validation of a recipe, when building the list of items to sort into the freeRepairs, notEnoughRepairs, or enoughRepairs lists, even though a cumulative amount of requirements are considered, given the items level, that cost is not reflected by the consumption logic.

I think that this section, when checking to see what items should be consumed for the repair, also needs to consider the quality of the item. It looks like it's only using the build cost of the current level of the item, not the current level plus all the levels preceding it.

https://github.com/aedenthorn/ValheimMods/blob/eb62ed46c9cc696bfd67b70854e43f8c7e67a050/RepairRequiresMats/BepInExPlugin.cs#L195-L206