grymlahv

edible component

Jul 3rd, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. local function oncheckbadfood(self)
  2. if self.healthvalue < 0 or (self.sanityvalue ~= nil and self.sanityvalue < 0) then
  3. self.inst:AddTag("badfood")
  4. else
  5. self.inst:RemoveTag("badfood")
  6. end
  7. end
  8.  
  9. local function onfoodtype(self, new_foodtype, old_foodtype)
  10. if old_foodtype ~= nil then
  11. self.inst:RemoveTag("edible_"..old_foodtype)
  12. end
  13. if new_foodtype ~= nil then
  14. assert(self.foodtype ~= self.secondaryfoodtype, "Edible component: The main and secondary food types cannot be set to the same value.")
  15. self.inst:AddTag("edible_"..new_foodtype)
  16. end
  17. end
  18.  
  19. local Edible = Class(function(self, inst)
  20. self.inst = inst
  21. self.healthvalue = 10
  22. self.hungervalue = 10
  23. self.sanityvalue = 0
  24. self.foodtype = FOODTYPE.GENERIC
  25. self.secondaryfoodtype = nil
  26. self.oneaten = nil
  27. self.degrades_with_spoilage = true
  28. self.gethealthfn = nil
  29.  
  30. self.temperaturedelta = 0
  31. self.temperatureduration = 0
  32.  
  33. --chill is a percentage [0, 1] of .temperatureduration
  34. --don't change this from 0 unless .temperaturedelta > 0
  35. self.chill = 0
  36. --self.nochill = false
  37.  
  38. self.stale_hunger = TUNING.STALE_FOOD_HUNGER
  39. self.stale_health = TUNING.STALE_FOOD_HEALTH
  40.  
  41. self.spoiled_hunger = TUNING.SPOILED_FOOD_HUNGER
  42. self.spoiled_health = TUNING.SPOILED_FOOD_HEALTH
  43.  
  44. self.spice = nil
  45. end,
  46. nil,
  47. {
  48. healthvalue = oncheckbadfood,
  49. sanityvalue = oncheckbadfood,
  50. foodtype = onfoodtype,
  51. secondaryfoodtype = onfoodtype,
  52. })
  53.  
  54. function Edible:OnRemoveFromEntity()
  55. self.inst:RemoveTag("badfood")
  56. if self.foodtype ~= nil then
  57. self.inst:RemoveTag("edible_"..self.foodtype)
  58. end
  59. if self.secondaryfoodtype ~= nil then
  60. self.inst:RemoveTag("edible_"..self.secondaryfoodtype)
  61. end
  62.  
  63. self.inst:RemoveTag("edible_"..FOODTYPE.BERRY)
  64. end
  65.  
  66. --Deprecated
  67. function Edible:GetWoodiness(eater) return 0 end
  68. --
  69.  
  70. function Edible:GetSanity(eater)
  71. local ignore_spoilage = not self.degrades_with_spoilage or self.sanityvalue < 0 or (eater ~= nil and eater.components.eater ~= nil and eater.components.eater.ignoresspoilage)
  72.  
  73. if not ignore_spoilage and self.inst.components.perishable ~= nil then
  74. if self.inst.components.perishable:IsStale() then
  75. if self.sanityvalue > 0 then
  76. return 0
  77. end
  78. elseif self.inst.components.perishable:IsSpoiled() then
  79. return -TUNING.SANITY_SMALL
  80. end
  81. end
  82.  
  83. local multiplier = 1
  84. if self.spice and TUNING.SPICE_MULTIPLIERS[self.spice] and TUNING.SPICE_MULTIPLIERS[self.spice].SANITY then
  85. multiplier = multiplier + TUNING.SPICE_MULTIPLIERS[self.spice].SANITY
  86. end
  87.  
  88. return self.sanityvalue * multiplier
  89. end
  90.  
  91. function Edible:GetHunger(eater)
  92. local multiplier = 1
  93. local ignore_spoilage = not self.degrades_with_spoilage or self.hungervalue < 0 or (eater ~= nil and eater.components.eater ~= nil and eater.components.eater.ignoresspoilage)
  94.  
  95. if not ignore_spoilage and self.inst.components.perishable ~= nil then
  96. if self.inst.components.perishable:IsStale() then
  97. multiplier = eater ~= nil and eater.components.eater ~= nil and eater.components.eater.stale_hunger or self.stale_hunger
  98. elseif self.inst.components.perishable:IsSpoiled() then
  99. multiplier = eater ~= nil and eater.components.eater ~= nil and eater.components.eater.spoiled_hunger or self.spoiled_hunger
  100. end
  101. end
  102.  
  103. if eater ~= nil and eater.components.foodaffinity ~= nil then
  104. local affinity_bonus = eater.components.foodaffinity:GetAffinity(self.inst)
  105. if affinity_bonus ~= nil then
  106. multiplier = multiplier * affinity_bonus
  107. end
  108. end
  109.  
  110. return multiplier * self.hungervalue
  111. end
  112.  
  113. function Edible:GetHealth(eater)
  114. local multiplier = 1
  115. local healthvalue = self.gethealthfn ~= nil and self.gethealthfn(self.inst, eater) or self.healthvalue
  116. local spice_source = self.spice
  117.  
  118. local ignore_spoilage = not self.degrades_with_spoilage or healthvalue < 0 or (eater ~= nil and eater.components.eater ~= nil and eater.components.eater.ignoresspoilage)
  119.  
  120. if not ignore_spoilage and self.inst.components.perishable ~= nil then
  121. if self.inst.components.perishable:IsStale() then
  122. multiplier = eater ~= nil and eater.components.eater ~= nil and eater.components.eater.stale_health or self.stale_health
  123. elseif self.inst.components.perishable:IsSpoiled() then
  124. multiplier = eater ~= nil and eater.components.eater ~= nil and eater.components.eater.spoiled_health or self.spoiled_health
  125. spice_source = nil
  126. end
  127. end
  128.  
  129. if spice_source and TUNING.SPICE_MULTIPLIERS[spice_source] and TUNING.SPICE_MULTIPLIERS[spice_source].HEALTH then
  130. multiplier = multiplier + TUNING.SPICE_MULTIPLIERS[spice_source].HEALTH
  131. end
  132.  
  133. return multiplier * healthvalue
  134. end
  135.  
  136. function Edible:GetDebugString()
  137. return string.format("Food type: %s, health: %2.2f, hunger: %2.2f, sanity: %2.2f", self.foodtype, self.healthvalue, self.hungervalue, self.sanityvalue)
  138. end
  139.  
  140. function Edible:SetOnEatenFn(fn)
  141. self.oneaten = fn
  142. end
  143.  
  144. function Edible:SetGetHealthFn(fn)
  145. self.gethealthfn = fn
  146. end
  147.  
  148. function Edible:OnEaten(eater)
  149. if self.oneaten ~= nil then
  150. self.oneaten(self.inst, eater)
  151. end
  152.  
  153. local delta_multiplier = 1
  154. local duration_multiplier = 1
  155.  
  156. if self.spice and TUNING.SPICE_MULTIPLIERS[self.spice] then
  157. if TUNING.SPICE_MULTIPLIERS[self.spice].TEMPERATUREDELTA then
  158. delta_multiplier = delta_multiplier + TUNING.SPICE_MULTIPLIERS[self.spice].TEMPERATUREDELTA
  159. end
  160.  
  161. if TUNING.SPICE_MULTIPLIERS[self.spice].TEMPERATUREDURATION then
  162. duration_multiplier = duration_multiplier + TUNING.SPICE_MULTIPLIERS[self.spice].TEMPERATUREDURATION
  163. end
  164. end
  165.  
  166. -- Food is an implicit heater/cooler if it has temperature
  167. if self.temperaturedelta ~= 0 and
  168. self.temperatureduration ~= 0 and
  169. self.chill < 1 and
  170. eater ~= nil and
  171. eater.components.temperature ~= nil then
  172. eater.components.temperature:SetTemperatureInBelly(self.temperaturedelta * (1 - self.chill) * delta_multiplier, self.temperatureduration * duration_multiplier)
  173. end
  174.  
  175. self.inst:PushEvent("oneaten", { eater = eater })
  176. end
  177.  
  178. function Edible:AddChill(delta)
  179. if self.temperaturedelta > 0 and not self.nochill then
  180. self.chill = math.clamp(self.chill + delta / self.temperatureduration, 0, 1)
  181. end
  182. end
  183.  
  184. function Edible:DiluteChill(item, count)
  185. if self.temperaturedelta > 0 and not self.nochill and self.inst.components.stackable ~= nil and item.components.edible ~= nil then
  186. local stacksize = self.inst.components.stackable.stacksize
  187. self.chill = (stacksize * self.chill + count * item.components.edible.chill) / (stacksize + count)
  188. end
  189. end
  190.  
  191. function Edible:OnSave()
  192. return self.chill > 0 and { chill = self.chill } or nil
  193. end
  194.  
  195. function Edible:OnLoad(data)
  196. if data.chill ~= nil and self.temperaturedelta > 0 and not self.nochill then
  197. self.chill = math.clamp(data.chill, 0, 1)
  198. end
  199. end
  200.  
  201. return Edible
Advertisement
Add Comment
Please, Sign In to add comment