Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. KEYVALUES_VERSION = "1.01"
  2.  
  3. -- Change to false to skip loading the base files
  4. LOAD_BASE_FILES = false
  5.  
  6. --[[
  7. Simple Lua KeyValues library
  8. Author: Martin Noya // github.com/MNoya
  9. Installation:
  10. - require this file inside your code
  11. Usage:
  12. - Your npc custom files will be validated on require, error will occur if one is missing or has faulty syntax.
  13. - This allows to safely grab key-value definitions in npc custom abilities/items/units/heroes
  14.  
  15. "some_custom_entry"
  16. {
  17. "CustomName" "Barbarian"
  18. "CustomKey" "1"
  19. "CustomStat" "100 200"
  20. }
  21. With a handle:
  22. handle:GetKeyValue() -- returns the whole table based on the handles baseclass
  23. handle:GetKeyValue("CustomName") -- returns "Barbarian"
  24. handle:GetKeyValue("CustomKey") -- returns 1 (number)
  25. handle:GetKeyValue("CustomStat") -- returns "100 200" (string)
  26. handle:GetKeyValue("CustomStat", 2) -- returns 200 (number)
  27.  
  28. Same results with strings:
  29. GetKeyValue("some_custom_entry")
  30. GetKeyValue("some_custom_entry", "CustomName")
  31. GetKeyValue("some_custom_entry", "CustomStat")
  32. GetKeyValue("some_custom_entry", "CustomStat", 2)
  33. - Ability Special value grabbing:
  34. "some_custom_ability"
  35. {
  36. "AbilitySpecial"
  37. {
  38. "01"
  39. {
  40. "var_type" "FIELD_INTEGER"
  41. "some_key" "-3 -4 -5"
  42. }
  43. }
  44. }
  45. With a handle:
  46. ability:GetAbilitySpecial("some_key") -- returns based on the level of the ability/item
  47. With string:
  48. GetAbilitySpecial("some_custom_ability", "some_key") -- returns "-3 -4 -5" (string)
  49. GetAbilitySpecial("some_custom_ability", "some_key", 2) -- returns -4 (number)
  50. Notes:
  51. - In case a key can't be matched, the returned value will be nil
  52. - Don't identify your custom units/heroes with the same name or it will only grab one of them.
  53. ]]
  54.  
  55. if not KeyValues then
  56. KeyValues = {}
  57. end
  58.  
  59. local split = function(inputstr, sep)
  60. if sep == nil then sep = "%s" end
  61. local t={} ; i=1
  62. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  63. t[i] = str
  64. i = i + 1
  65. end
  66. return t
  67. end
  68.  
  69. -- Load all the necessary key value files
  70. function LoadGameKeyValues()
  71. local scriptPath ="scripts/npc/"
  72. -- local override = LoadKeyValues(scriptPath.."npc_abilities_override.txt")
  73. local files = {
  74. AbilityKV = {base="npc_abilities",custom="npc_abilities_custom"},
  75. AbilityKV2 = {base="",custom="npc_abilities"},
  76. ItemKV = {base="items",custom="npc_items_custom"},
  77. UnitKV = {base="npc_units",custom="npc_units"}, -- npc_units_custom
  78. HeroKV = {base="npc_heroes",custom="npc_heroes_custom"},
  79. HeroKV2 = {base="",custom="npc_heroes"}
  80. }
  81.  
  82. -- Load and validate the files
  83. for k,v in pairs(files) do
  84. local file = {}
  85. if LOAD_BASE_FILES then
  86. file = LoadKeyValues(scriptPath..v.base..".txt")
  87. end
  88.  
  89. -- Replace main game keys by any match on the override file
  90. -- for k,v in pairs(override) do
  91. -- if file[k] then
  92. -- file[k] = v
  93. -- end
  94. -- end
  95.  
  96. local custom_file = LoadKeyValues(scriptPath..v.custom..".txt")
  97. if custom_file then
  98. for k,v in pairs(custom_file) do
  99. file[k] = v
  100. end
  101. else
  102. print("[KeyValues] Critical Error on "..v.custom..".txt")
  103. return
  104. end
  105.  
  106. GameRules[k] = file --backwards compatibility
  107. KeyValues[k] = file
  108. end
  109.  
  110. -- Merge All KVs
  111. KeyValues.All = {}
  112. for name,path in pairs(files) do
  113. for key,value in pairs(KeyValues[name]) do
  114. if not KeyValues.All[key] then
  115. KeyValues.All[key] = value
  116. end
  117. end
  118. end
  119.  
  120. -- Merge units and heroes (due to them sharing the same class CDOTA_BaseNPC)
  121. for key,value in pairs(KeyValues.HeroKV) do
  122. if not KeyValues.UnitKV[key] then
  123. KeyValues.UnitKV[key] = value
  124. else
  125. if type(KeyValues.All[key]) == "table" then
  126. print("[KeyValues] Warning: Duplicated unit/hero entry for "..key)
  127. end
  128. end
  129. end
  130.  
  131. for key,value in pairs(KeyValues.HeroKV2) do
  132. if not KeyValues.UnitKV[key] then
  133. KeyValues.UnitKV[key] = value
  134. else
  135. if type(KeyValues.All[key]) == "table" then
  136. -- print("[KeyValues] Warning: Duplicated unit/hero entry for "..key)
  137. end
  138. end
  139. end
  140.  
  141. for key,value in pairs(KeyValues.AbilityKV2) do
  142. if not KeyValues.AbilityKV[key] then
  143. KeyValues.AbilityKV[key] = value
  144. else
  145. if type(KeyValues.All[key]) == "table" then
  146. print("[KeyValues] Warning: Duplicated unit/hero entry for "..key)
  147. end
  148. end
  149. end
  150. end
  151.  
  152. -- Works for heroes and units on the same table due to merging both tables on game init
  153. function CDOTA_BaseNPC:GetKeyValue(key, level)
  154. if level then return GetUnitKV(self:GetUnitName(), key, level)
  155. else return GetUnitKV(self:GetUnitName(), key) end
  156. end
  157.  
  158. function GetKeyValueByHeroName(hero_name, key)
  159. if level then return GetUnitKV(hero_name, key, level)
  160. else return GetUnitKV(hero_name, key) end
  161. end
  162.  
  163. -- Dynamic version of CDOTABaseAbility:GetAbilityKeyValues()
  164. function CDOTABaseAbility:GetKeyValue(key, level)
  165. if level then return GetAbilityKV(self:GetAbilityName(), key, level)
  166. else return GetAbilityKV(self:GetAbilityName(), key) end
  167. end
  168.  
  169. -- Item version
  170. function CDOTA_Item:GetKeyValue(key, level)
  171. if level then return GetItemKV(self:GetAbilityName(), key, level)
  172. else return GetItemKV(self:GetAbilityName(), key) end
  173. end
  174.  
  175. function CDOTABaseAbility:GetAbilitySpecial(key)
  176. return GetAbilitySpecial(self:GetAbilityName(), key, self:GetLevel())
  177. end
  178.  
  179. -- Global functions
  180. -- Key is optional, returns the whole table by omission
  181. -- Level is optional, returns the whole value by omission
  182. function GetKeyValue(name, key, level, tbl)
  183. local t = tbl or KeyValues.All[name]
  184. if key and t then
  185. if t[key] and level then
  186. local s = split(t[key])
  187. if s[level] then return tonumber(s[level]) or s[level] -- Try to cast to number
  188. else return tonumber(s[#s]) or s[#s] end
  189. else return t[key] end
  190. else return t end
  191. end
  192.  
  193. function GetUnitKV(unitName, key, level)
  194. return GetKeyValue(unitName, key, level, KeyValues.UnitKV[unitName])
  195. end
  196.  
  197. function GetAbilityKV(abilityName, key, level)
  198. return GetKeyValue(abilityName, key, level, KeyValues.AbilityKV[abilityName])
  199. end
  200.  
  201. function GetItemKV(itemName, key, level)
  202. return GetKeyValue(itemName, key, level, KeyValues.ItemKV[itemName])
  203. end
  204.  
  205. function GetAbilitySpecial(name, key, level)
  206. local t = KeyValues.All[name]
  207. if key and t then
  208. local tspecial = t["AbilitySpecial"]
  209. if tspecial then
  210. -- Find the key we are looking for
  211. for _,values in pairs(tspecial) do
  212. if values[key] then
  213. if not level then return values[key]
  214. else
  215. local s = split(values[key])
  216. if s[level] then return tonumber(s[level]) -- If we match the level, return that one
  217. else return tonumber(s[#s]) end -- Otherwise, return the max
  218. end
  219. break
  220. end
  221. end
  222. end
  223. else return t end
  224. end
  225.  
  226. LoadGameKeyValues()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement