Guest User

proposed changes to BookLearning_ISReadABook.lua

a guest
Dec 28th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. --Additions to give skill XP when reading
  2. BookLearning = {};
  3.  
  4. --Addition for skill configuration via lua, so it can be set from outside of this mod. You can make a simple list of items like "Farming", "Cooking" and such here
  5. BookLearning.allowedSkills = BookLearning.allowedSkills or {};
  6.  
  7. --Add skill XP gain to book reading timed action
  8. BOOKLEARNING_ISReadABook_update = ISReadABook.update
  9. function ISReadABook:update()
  10.     BOOKLEARNING_ISReadABook_update(self)
  11.     local checkSuccess;
  12.     local canLearn;
  13.     checkSuccess, canLearn = pcall(BookLearning.checkIfCanLearn, self);
  14.  
  15.     if (checkSuccess and canLearn) then
  16.         BookLearning.gainXP(self);
  17.     end
  18. end
  19.  
  20. --Sets o to true if the player can learn from this book
  21. BookLearning.checkIfCanLearn = function(self)
  22.     local o = true;
  23.  
  24.     if (not (SkillBook[self.item:getSkillTrained()] ~= nil))
  25.         or self.item:getLvlSkillTrained() > self.character:getPerkLevel(SkillBook[self.item:getSkillTrained()].perk) + 1
  26.         or self.character:HasTrait("Illiterate")
  27.         or self.item:getMaxLevelTrained() < self.character:getPerkLevel(SkillBook[self.item:getSkillTrained()].perk) + 1
  28.     then
  29.         o = false;
  30.     end
  31.  
  32.     return o;
  33. end
  34.  
  35. --Provides player XP for every page read.
  36. BookLearning.gainXP = function(self)
  37.     local trainedStuff = SkillBook[self.item:getSkillTrained()];
  38.  
  39.     if trainedStuff then
  40.         -- check if allowedSkills list exists, if so check if this is allowed
  41.         if #BookLearning.allowedSkills > 0 then
  42.             local isAllowed = false;
  43.  
  44.             for _,skill in ipairs(BookLearning.allowedSkills) do
  45.                 if tostring(trainedStuff.perk) == skill then
  46.                     isAllowed = true;
  47.                     break;
  48.                 end
  49.             end
  50.  
  51.             if not isAllowed then
  52.                 return;
  53.             end
  54.         end
  55.         -- end check
  56.  
  57.         local readPercent = (self.item:getAlreadyReadPages() / self.item:getNumberOfPages()) * 100;
  58.         if readPercent > 100 then
  59.             readPercent = 100;
  60.         end
  61.  
  62.         local targetSkillLevel = self.item:getLvlSkillTrained() + self.item:getNumLevelsTrained() - 1;
  63.         local bookMaxXP = 0;
  64.         for i=1,targetSkillLevel do
  65.             bookMaxXP = (bookMaxXP + BookLearning.xpByLevel[i]);
  66.         end
  67.         local targetXP = bookMaxXP * (readPercent / 100);
  68.         local xpGained = math.ceil(targetXP - self.character:getXp():getXP(trainedStuff.perk));
  69.  
  70.         --Fix rounding error for XP on last page
  71.         if readPercent == 100 then
  72.             xpGained = xpGained + 0.1;
  73.         end
  74.  
  75.         if xpGained > 0 then
  76.             self.character:getXp():AddXP(trainedStuff.perk, xpGained);
  77.         end
  78.     end
  79. end
  80.  
  81. --XP per skill level as of 41.50
  82. BookLearning.xpByLevel = {};
  83. BookLearning.xpByLevel[0] = 0;
  84. BookLearning.xpByLevel[1] = 75;
  85. BookLearning.xpByLevel[2] = 150;
  86. BookLearning.xpByLevel[3] = 300;
  87. BookLearning.xpByLevel[4] = 750;
  88. BookLearning.xpByLevel[5] = 1500;
  89. BookLearning.xpByLevel[6] = 3000;
  90. BookLearning.xpByLevel[7] = 4500;
  91. BookLearning.xpByLevel[8] = 6000;
  92. BookLearning.xpByLevel[9] = 7500;
  93. BookLearning.xpByLevel[10] = 9000;
Advertisement
Add Comment
Please, Sign In to add comment