modular_texture_api.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --the positions in the texture table at which each texture goes
  2. local key_index_map =
  3. {
  4. arm_armor_left = 1,
  5. arm_armor_right = 2,
  6. arm_skin_left = 3,
  7. arm_skin_right = 4,
  8. torso_armor = 5,
  9. hair = 6,
  10. head = 7,
  11. helmet = 8,
  12. leg_armor_left = 9,
  13. leg_armor_right = 10,
  14. leg_skin_left = 11,
  15. leg_skin_right = 12,
  16. torso_skin = 13,
  17. }
  18. local classes = {}
  19. local function default_process(self)
  20. return self[1]
  21. end
  22. --adds functions to the mod api table
  23. return function(classname)
  24. table.insert(classes, classname)
  25. local textures = {}
  26. --register a texture. Example:
  27. --[[
  28. dwarf_characters.register_texture("texture",
  29. {
  30. {
  31. "helmet.png",
  32. process =
  33. function(self)
  34. return self[1] .. "^[multiply:#"ABCDEF"
  35. end
  36. }
  37. })
  38. ]]
  39. dwarf_characters["register_" .. classname] = function(name, def)
  40. assert(not textures[name], "There already is a "..
  41. classname ..
  42. " texture registered with name " .. name)
  43. local postprocessed = {}
  44. for k, t in pairs(def)
  45. do
  46. if not t.process
  47. then
  48. t.process = default_process
  49. end
  50. postprocessed[key_index_map[k]] = t
  51. end
  52. textures[name] = postprocessed
  53. end
  54. --overwrites the fields of the base with textures registered under
  55. --<name>. During this the process function is called with mod
  56. --as argument
  57. dwarf_characters["apply_" .. classname] = function(base, name, mod)
  58. if textures[name]
  59. then
  60. for k, t in pairs(textures[name])
  61. do
  62. base[k] = t:process(mod)
  63. end
  64. end
  65. end
  66. --returns a list of all texture names registered in this class
  67. dwarf_characters["get_" .. classname .."_list"] = function()
  68. local ret = {}
  69. local i = 0
  70. for name, _ in pairs(textures)
  71. do
  72. i = i + 1
  73. ret[i] = name
  74. end
  75. return ret
  76. end
  77. dwarf_characters["get_random_" .. classname] = function()
  78. local list = dwarf_characters["get_" .. classname .."_list"]()
  79. return list[math.random(#list)]
  80. end
  81. end