Advertisement
Zarklord

customtechtree.lua

Feb 10th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.73 KB | None | 0 0
  1. --this means the method hooks get proccessed once
  2. if GLOBAL.rawget(GLOBAL,"AddNewTechType") and GLOBAL.rawget(GLOBAL,"AddTechHint") and GLOBAL.rawget(GLOBAL,"AddPrototyperTree") then
  3.     AddNewTechType = GLOBAl.AddNewTechType
  4.     AddTechHint = GLOBAl.AddTechHint
  5.     AddPrototyperTree = GLOBAl.AddPrototyperTree
  6.     return
  7. end
  8.  
  9.  
  10. --keep a separate list of all the custom techs for use with techname_bonus
  11. --more effecient than itterating through all the techs... just go through the custom ones...
  12. local CUSTOM_TECH_BONUS = {}
  13. local CUSTOM_TECH_KEY = {}
  14. local CUSTOM_TECH_HINT = {}
  15.  
  16. local TechTree = GLOBAL.require("techtree")
  17.  
  18. function AddNewTechType(techtype)
  19.     --add tech to all tech list
  20.     TechTree.AVAILABLE_TECH[#TechTree.AVAILABLE_TECH + 1] = techtype
  21.     --add to this list for custom bonus proccessing
  22.     CUSTOM_TECH_BONUS[#CUSTOM_TECH_BONUS + 1] = techtype
  23.     --set global.tech.none.techtype to 0 to prevent crashes
  24.     GLOBAL.TECH.NONE[techtype] = 0
  25.     --add our new techtype to all existing recipes as 0 or if its already there then we leave it as it should be
  26.     --we only need to do this once as all recipes from here on out will have it at 0 or provided value
  27.     for k, v in pairs(GLOBAL.AllRecipes) do
  28.         v.level[techtype] = (v.level[techtype] or 0)
  29.     end
  30.     --add our new techtype to all existing prototyping machines as 0 or if its already there then we leave it as it should be
  31.     --we only need to do this once as all future proto trees will have it at 0 or provided value
  32.     for k, v in pairs(TUNING.PROTOTYPER_TREES) do
  33.         v[techtype] = (v[techtype] or 0)
  34.     end
  35. end
  36.  
  37. function AddTechHint(Tech,NewHint)
  38.     --adding the tech that you want to one list and the hint to the next
  39.     CUSTOM_TECH_KEY[#CUSTOM_TECH_KEY + 1] = Tech
  40.     CUSTOM_TECH_HINT[#CUSTOM_TECH_HINT + 1] = NewHint
  41. end
  42.  
  43. function AddPrototyperTree(TreeName,Tech)
  44.     --adds a prototyper tree to TUNING.PROTOTYPER_TREES how it should be done
  45.     --note: this is only for what the prototyper machine should output
  46.     --creating a tech for recipes can be done by doing: GLOBAL.TECH.TECHNAME = {TECHTYPE = 1, OTHERTECHTYPE = 3}
  47.     TUNING.PROTOTYPER_TREES[TreeName] = TechTree.Create(Tech)
  48. end
  49.  
  50. --cant edit the replica with AddComponentPostInit
  51. local function BuilderReplicaTech(replica)
  52.     if not GLOBAL.TheWorld.ismastersim then return end
  53.     local Old_SetTechTrees = replica.SetTechTrees
  54.     function replica:SetTechTrees(techlevels)
  55.         --we have to deal with custom bonus's in here as we dont have a good place to put custom code in Builder:EvaluateTechTrees
  56.         --its passed by reference so its perfectly fine to do it here
  57.         if self.inst.components.builder ~= nil and self.inst.components.builder.current_prototyper ~= nil then
  58.             --it handles our bonus correctly if it doesnt have a prototyper machine found...
  59.             local techChanged = false
  60.             for i, v in ipairs(CUSTOM_TECH_BONUS) do
  61.                 local currentTech = techlevels[v] or 0
  62.                 techlevels[v] = (techlevels[v] or 0) + (self.inst.components.builder[string.lower(v).."_bonus"] or 0)
  63.                 if currentTech ~= techlevels[v] then
  64.                     techChanged = true
  65.                 end
  66.             end
  67.             --if we change the tech tree reupdate any dependencies by calling this again
  68.             --this isnt ideal but this is still the best way currently
  69.             if techChanged then
  70.                 self.inst:PushEvent("techtreechange", { level = self.accessible_tech_trees })
  71.             end
  72.         end
  73.         Old_SetTechTrees(self,techlevels)
  74.     end
  75. end
  76.  
  77. local function CompareTech(recipetech,createdtech)
  78.     --comparine two tech trees
  79.     for i, v in ipairs(TechTree.AVAILABLE_TECH) do
  80.         if (recipetech[v] or 0) == (createdtech[v] or 0) then
  81.             return false
  82.         end
  83.     end
  84.     return true
  85. end
  86.  
  87. local function RecipeHint(recipepopup)
  88.     if not GLOBAL.TheWorld.ismastersim then return end
  89.     local recipe_hint = nil
  90.  
  91.     local Old_setMultilineTruncatedString = recipepopup.teaser.SetMultilineTruncatedString
  92.     function recipepopup.teaser:SetMultilineTruncatedString(str, maxlines, maxwidth, maxcharsperline, ellipses)
  93.         --only do stuff if the str is the CANTRESEARCH text and also make sure recipe_hint isnt nil
  94.         if str == GLOBAL.STRINGS.UI.CRAFTING.CANTRESEARCH and recipe_hint ~= nil then
  95.             for i, v in ipairs(CUSTOM_TECH_KEY) do
  96.                 --compare each custom tech to the recipe's tech if it matches exactly then its the same tech and can have a custom hint
  97.                 if CompareTech(recipe_hint.level,v) then
  98.                     --set str as custom hint
  99.                     str = CUSTOM_TECH_HINT[i]
  100.                     break
  101.                 end
  102.             end
  103.         end
  104.         Old_setMultilineTruncatedString(self, str, maxlines, maxwidth, maxcharsperline, ellipses)
  105.     end
  106.  
  107.     local Old_Refresh = recipepopup.Refresh
  108.     function recipepopup:Refresh()
  109.         --get the recipe so i can compare tech level
  110.         recipe_hint = self.recipe
  111.         Old_Refresh(self)
  112.         recipe_hint = nil
  113.     end
  114. end
  115.  
  116. local function PlayerClassifiedTech(inst)
  117.     for i, v in ipairs(CUSTOM_TECH_BONUS) do
  118.         inst[string.lower(v) .. "bonus"] = GLOBAL.net_tinybyte(inst.GUID, "builder." .. string.lower(v) .. "_bonus")
  119.     end
  120. end
  121.  
  122. local function DoMethodHooks()
  123.     AddClassPostConstruct("widgets/recipepopup", RecipeHint)
  124.     AddClassPostConstruct("components/builder_replica",BuilderReplicaTech)
  125.     AddPrefabPostInit("player_classified",PlayerClassifiedTech)
  126. end
  127.  
  128. GLOBAL.AddNewTechType = AddNewTechType
  129. GLOBAL.AddTechHint = AddTechHint
  130. GLOBAL.AddPrototyperTree = AddPrototyperTree
  131.  
  132. DoMethodHooks()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement