init.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. advtrains_livery_database = {}
  2. local callback_functions = {}
  3. local livery_templates = {}
  4. local registered_wagons = {}
  5. local predefined_liveries = {}
  6. local overlays_per_template_limit = 6
  7. -------------------------------------------------------------------------------
  8. -- External Utilities:
  9. function advtrains_livery_database.clone_livery_design(src_livery_design)
  10. if not src_livery_design then
  11. return nil
  12. end
  13. local livery_design = {
  14. wagon_type = src_livery_design.wagon_type,
  15. livery_template_name = src_livery_design.livery_template_name,
  16. }
  17. if src_livery_design.overlays then
  18. livery_design.overlays = {}
  19. for seq_num, overlay in pairs(src_livery_design.overlays) do
  20. livery_design.overlays[seq_num] = {
  21. id = overlay.id,
  22. color = overlay.color,
  23. }
  24. end
  25. end
  26. return livery_design
  27. end
  28. function advtrains_livery_database.clone_textures(src)
  29. if not src then
  30. return nil
  31. end
  32. local textures = {}
  33. for k, texture in pairs(src) do
  34. textures[k] = texture
  35. end
  36. return textures
  37. end
  38. function advtrains_livery_database.get_overlays_per_template_limit()
  39. return overlays_per_template_limit
  40. end
  41. -------------------------------------------------------------------------------
  42. -- Internal Utilities:
  43. local function is_valid_livery_design(livery_design)
  44. return livery_design and
  45. livery_design.wagon_type and
  46. livery_design.livery_template_name
  47. end
  48. local function get_wagon_type_prefix(wagon_type)
  49. local delimiter_pos, _ = string.find(wagon_type, ":")
  50. if not delimiter_pos or delimiter_pos < 2 then
  51. return
  52. end
  53. return wagon_type:sub(1, delimiter_pos - 1)
  54. end
  55. local function get_template(wagon_type, base_texture)
  56. for name, template in pairs(livery_templates[wagon_type]) do
  57. if template.base_textures[1] == base_texture then
  58. return name, template
  59. end
  60. end
  61. end
  62. -------------------------------------------------------------------------------
  63. -- Every mod that needs to register one or more livery templates should call
  64. -- this function exactly once.
  65. function advtrains_livery_database.register_mod(mod_name, optional_callback_functions)
  66. assert(mod_name, "Missing required mod name")
  67. if not callback_functions[mod_name] then
  68. if optional_callback_functions then
  69. callback_functions[mod_name] = {
  70. on_custom_get_livery_design_from_textures = optional_callback_functions.on_custom_get_livery_design_from_textures, -- Function parameters: (wagon_type, livery_textures, wagon_id)
  71. on_pre_get_livery_design_from_textures = optional_callback_functions.on_pre_get_livery_design_from_textures, -- Function parameters: (wagon_type, livery_textures, wagon_id, livery_design)
  72. on_custom_get_livery_textures_from_design = optional_callback_functions.on_custom_get_livery_textures_from_design, -- Function parameters: (livery_design, wagon_id)
  73. on_post_get_livery_textures_from_design = optional_callback_functions.on_post_get_livery_textures_from_design, -- Function parameters: (livery_design, wagon_id, livery_textures)
  74. }
  75. else
  76. callback_functions[mod_name] = {}
  77. end
  78. else
  79. minetest.debug("WARNING: An attempt to register mod '"..mod_name.."' multiple times with the train livery database is not supported and has been blocked.")
  80. end
  81. end
  82. -------------------------------------------------------------------------------
  83. -- This function should be called exactly once per wagon type being registered
  84. -- for a mod, regardless of how many templates are being registered for the
  85. -- wagon type.
  86. --
  87. -- Note: The mod_name parameter is optional if both wagon_type prefix is not
  88. -- null and not equal to "advtrains".
  89. function advtrains_livery_database.register_wagon(wagon_type, mod_name)
  90. assert(wagon_type, "Invalid wagon type")
  91. -- Determine the effective wagon_mod. This is the mod whose callback
  92. -- functions will be used for the given wagon_type and livery_template_name.
  93. local actual_wagon_mod = nil
  94. local wagon_type_prefix = get_wagon_type_prefix(wagon_type)
  95. if wagon_type_prefix and wagon_type_prefix ~= "advtrains" then
  96. actual_wagon_mod = wagon_type_prefix
  97. elseif mod_name and mod_name ~="advtrains" then
  98. -- The mod_name parameter is only used if the wagon_type_prefix is nil
  99. -- or not "advtrains". If used, it must be neither nil nor "advtrains".
  100. -- Its purpose is to allow mods that define their wagons in the
  101. -- "advtrains" namespace to register livery templates that use callback
  102. -- functions that are specific to the owning mod.
  103. actual_wagon_mod = mod_name
  104. end
  105. assert(actual_wagon_mod, "Invalid wagon_mod parameter") -- The wagon_mod parameter must be neither nil nor equal to "advtrains" if wagon_type_prefix is nil or equal to "advtrains"
  106. assert(callback_functions[actual_wagon_mod], "Attempt to register a wagon_type for an unregistered mod ('"..actual_wagon_mod.."').")
  107. if not registered_wagons[wagon_type] then
  108. registered_wagons[wagon_type] = {
  109. mod_name = actual_wagon_mod,
  110. }
  111. end
  112. end
  113. -- Use this funtion to determine the mod for which a given wagon type was
  114. -- registered. This is need because some mods define their wagon_type using
  115. -- the prefix "advtrains" rather than their mod name.
  116. function advtrains_livery_database.get_wagon_mod_name(wagon_type)
  117. if wagon_type and registered_wagons[wagon_type] then
  118. return registered_wagons[wagon_type].mod_name
  119. end
  120. return
  121. end
  122. -- This function should be called once for each livery template that is
  123. -- registered for a given wagon type.
  124. function advtrains_livery_database.add_livery_template(wagon_type, livery_template_name, base_textures, livery_mod, overlay_count, designer, texture_license, texture_creator, notes)
  125. assert(wagon_type, "Invalid wagon type")
  126. assert(registered_wagons[wagon_type], "Attempt to register a livery template for an unregistered wagon type ('"..wagon_type.."').")
  127. assert(livery_template_name, "Invalid livery template name")
  128. assert(base_textures, "Invalid base textures")
  129. assert(base_textures[1], "Missing base texture")
  130. assert(livery_mod, "Missing livery mod name")
  131. assert(overlay_count, "Missing overlay count")
  132. assert(callback_functions[livery_mod], "Attempt to add a livery template for an unregistered mod ('"..livery_mod.."').")
  133. if not livery_templates[wagon_type] then
  134. livery_templates[wagon_type] = {}
  135. end
  136. if not livery_templates[wagon_type][livery_template_name] then
  137. -- Only add the livery template if another livery template for the
  138. -- given wagon type that has the same base texture has not been
  139. -- previously registered. In other words, for a given wagon_type, each
  140. -- livery template should have a unique base_textures[1]. This
  141. -- restriction is used so that base_texture[1] of given wagon can be
  142. -- used to identify its livery template. A workaround for this
  143. -- restriction for the template creator is to duplicate the texture
  144. -- file and give it a unique name.
  145. local existing_livery_template_name, _ = get_template(wagon_type, base_textures[1])
  146. assert(not existing_livery_template_name,
  147. "Attempt to register a livery template ('"..livery_template_name..
  148. "') that does not have a uniquely named base texture for wagon type ('"..wagon_type..
  149. "'). A previously registered livery template ('"..(existing_livery_template_name or "<nil>")..
  150. "') already uses the same texture file ('"..base_textures[1].."').")
  151. livery_templates[wagon_type][livery_template_name] = {
  152. base_textures = advtrains_livery_database.clone_textures(base_textures),
  153. designer = designer,
  154. texture_license = texture_license,
  155. texture_creator = texture_creator,
  156. notes = notes,
  157. livery_mod = livery_mod,
  158. expected_overlay_count = overlay_count,
  159. overlays = {},
  160. }
  161. end
  162. end
  163. -- This function is called to register an overlay for a given livery template.
  164. -- It should be called once for each overlay in the template.
  165. function advtrains_livery_database.add_livery_template_overlay(wagon_type, livery_template_name, overlay_id, overlay_name, overlay_slot_idx, overlay_texture, overlay_alpha)
  166. assert(wagon_type, "Invalid wagon type")
  167. assert(livery_template_name, "Invalid livery template name")
  168. assert(overlay_id, "Invalid overlay id")
  169. assert(overlay_name, "Invalid overlay name")
  170. assert(overlay_slot_idx and tonumber(overlay_slot_idx) > 0, "Invalid overlay slot index")
  171. assert(overlay_texture, "Invalid overlay texture")
  172. assert(livery_templates[wagon_type], "wagon type, '"..wagon_type.."' not registered")
  173. assert(livery_templates[wagon_type][livery_template_name], "livery template name, '"..livery_template_name.."' not registered for wagon type, '"..wagon_type.."'")
  174. local overlay_count = #livery_templates[wagon_type][livery_template_name].overlays
  175. if overlay_count >= overlays_per_template_limit then
  176. minetest.debug(
  177. "Failed to add overlay ('"..overlay_name..
  178. "') for livery template ('"..livery_template_name..
  179. "') of wagon type ('"..wagon_type..
  180. "') due to exceeding overlays per template limit.")
  181. return
  182. end
  183. if livery_templates[wagon_type][livery_template_name].overlays[overlay_id] then
  184. minetest.debug(
  185. "Failed to add overlay ('"..overlay_name..
  186. "') for livery template ('"..livery_template_name..
  187. "') of wagon type ('"..wagon_type..
  188. "') due to overlay id "..overlay_id.." having already been added.")
  189. return
  190. end
  191. if overlay_count >= livery_templates[wagon_type][livery_template_name].expected_overlay_count then
  192. -- This could be due to a mod not correctly specifying the number of
  193. -- overlays that that will be in the template when it called
  194. -- add_livery_template() or it could be an attempt by one mod to update
  195. -- the overlays of a template created by another mod.
  196. minetest.debug(
  197. "Failed to add overlay ('"..overlay_name..
  198. "') for livery template ('"..livery_template_name..
  199. "') of wagon type ('"..wagon_type..
  200. "') due to exceeding the expected number of overlays.")
  201. return
  202. end
  203. livery_templates[wagon_type][livery_template_name].overlays[overlay_id] = {
  204. name =overlay_name,
  205. slot_idx = tonumber(overlay_slot_idx),
  206. texture = overlay_texture,
  207. alpha = overlay_alpha and tonumber(overlay_alpha) or 255,
  208. }
  209. end
  210. -- This function registers a predefined livery for a wagon type. Predefined
  211. -- liveries are optional.
  212. -- Note that predefined livery names must be unique per wagon type.
  213. --
  214. -- Ideally, predefined liveries should be used to showcase examples of a few
  215. -- designs that are possibile for a given templete. Registering too many
  216. -- predefined liveries for a given template could be counter produtive since
  217. -- a player could become overwhelmed with the number of options. That concern
  218. -- could be addressed if the tool that displays predefined liveries provides a
  219. -- filtering and/or search mechanism.
  220. function advtrains_livery_database.add_predefined_livery(design_name, livery_design, mod_name, notes)
  221. assert(design_name, "Invalid design name")
  222. assert(is_valid_livery_design(livery_design), "Invalid livery design")
  223. assert(mod_name, "Invalid mod name")
  224. assert(callback_functions[mod_name], "Attempt to add predefined livery for an unregistered mod ('"..mod_name.."').")
  225. -- Don't add predefined liveries that reference a livery template that is
  226. -- not already registered.
  227. if not livery_templates[livery_design.wagon_type] or not livery_templates[livery_design.wagon_type][livery_design.livery_template_name] then
  228. minetest.debug("Failed to add predefined livery design ('"..design_name.."') for wagon type ('"..livery_design.wagon_type.."') due to unknown livery template ('"..livery_design.livery_template_name.."')")
  229. return false
  230. end
  231. if not predefined_liveries[livery_design.wagon_type] then
  232. predefined_liveries[livery_design.wagon_type] = {}
  233. end
  234. if not predefined_liveries[livery_design.wagon_type][design_name] then
  235. predefined_liveries[livery_design.wagon_type][design_name] = {
  236. mod_name = mod_name,
  237. notes = notes,
  238. livery_design = advtrains_livery_database.clone_livery_design(livery_design),
  239. }
  240. return true
  241. else
  242. minetest.debug("Failed to add predefined livery design due to duplicate name ('"..design_name.."') for wagon type ('"..livery_design.wagon_type.."')")
  243. end
  244. return false
  245. end
  246. -- Get the list of names of predefined liveries for a given wagon type.
  247. function advtrains_livery_database.get_predefined_livery_names(wagon_type)
  248. local names = {}
  249. if wagon_type and predefined_liveries[wagon_type] then
  250. for name, predefined_livery in pairs(predefined_liveries[wagon_type]) do
  251. table.insert(names, {livery_name = name, livery_template_name = predefined_livery.livery_design.livery_template_name})
  252. end
  253. end
  254. return names
  255. end
  256. -- Get the predefined livery for a given wagon type and predefined livery name.
  257. -- Note that predefined livery names are unique per wagon type.
  258. function advtrains_livery_database.get_predefined_livery(wagon_type, design_name)
  259. if wagon_type and design_name and predefined_liveries[wagon_type] and predefined_liveries[wagon_type][design_name] then
  260. return advtrains_livery_database.clone_livery_design(predefined_liveries[wagon_type][design_name].livery_design)
  261. end
  262. return
  263. end
  264. -------------------------------------------------------------------------------
  265. function advtrains_livery_database.get_wagon_livery_overlay_name(wagon_type, livery_template_name, overlay_seq_number)
  266. assert(wagon_type, "Invalid wagon type")
  267. assert(livery_template_name, "Invalid livery template name")
  268. assert(overlay_seq_number, "Invalid overlay sequence number")
  269. if livery_templates[wagon_type] and
  270. livery_templates[wagon_type][livery_template_name] and
  271. livery_templates[wagon_type][livery_template_name].overlays and
  272. livery_templates[wagon_type][livery_template_name].overlays[overlay_seq_number] then
  273. return livery_templates[wagon_type][livery_template_name].overlays[overlay_seq_number].name
  274. end
  275. end
  276. function advtrains_livery_database.get_wagon_livery_template(wagon_type, livery_template_name)
  277. assert(wagon_type, "Invalid wagon type")
  278. assert(livery_template_name, "Invalid livery template name")
  279. if not livery_templates[wagon_type] or not livery_templates[wagon_type][livery_template_name] then
  280. return nil
  281. end
  282. -- Create a copy of the template
  283. local wagon_livery_template = {
  284. base_textures = advtrains_livery_database.clone_textures(livery_templates[wagon_type][livery_template_name].base_textures),
  285. designer = livery_templates[wagon_type][livery_template_name].designer,
  286. texture_license = livery_templates[wagon_type][livery_template_name].texture_license,
  287. texture_creator = livery_templates[wagon_type][livery_template_name].texture_creator,
  288. notes = livery_templates[wagon_type][livery_template_name].notes,
  289. livery_mod = livery_templates[wagon_type][livery_template_name].livery_mod,
  290. overlays = {},
  291. }
  292. for id, overlay in ipairs(livery_templates[wagon_type][livery_template_name].overlays) do
  293. wagon_livery_template.overlays[id] = {
  294. name = overlay.name,
  295. slot_idx = overlay.slot_idx,
  296. texture = overlay.texture,
  297. alpha = overlay.alpha,
  298. }
  299. end
  300. return wagon_livery_template
  301. end
  302. function advtrains_livery_database.get_livery_template_names_for_wagon(wagon_type)
  303. local livery_template_names = {}
  304. if wagon_type and livery_templates[wagon_type] then
  305. for name, _ in pairs(livery_templates[wagon_type]) do
  306. table.insert(livery_template_names, name)
  307. end
  308. end
  309. return livery_template_names
  310. end
  311. -------------------------------------------------------------------------------
  312. -- Attempt to determine the livery_design for a given wagon_type from its
  313. -- livery textures. Note that if livery textures were modified by the owning
  314. -- mod due to weathering, loads, etc. then this function could fail. The mod
  315. -- should provide an implementation of on_custom_get_livery_design_from_textures()
  316. -- or on_pre_get_livery_design_from_textures() to handle such cases.
  317. function advtrains_livery_database.get_livery_design_from_textures(wagon_type, textures, wagon_id)
  318. if not wagon_type or not textures or not textures[1] then
  319. return nil
  320. end
  321. local wagon_mod_name = advtrains_livery_database.get_wagon_mod_name(wagon_type)
  322. if not wagon_mod_name then
  323. return
  324. end
  325. local livery_textures = advtrains_livery_database.clone_textures(textures)
  326. -- Allow the mod to provide its own implementation for this function.
  327. if callback_functions[wagon_mod_name] and callback_functions[wagon_mod_name].on_custom_get_livery_design_from_textures then
  328. return callback_functions[wagon_mod_name].on_custom_get_livery_design_from_textures(wagon_type, livery_textures, wagon_id)
  329. end
  330. if not livery_templates[wagon_type] then
  331. return nil
  332. end
  333. if callback_functions[wagon_mod_name] and callback_functions[wagon_mod_name].on_pre_get_livery_design_from_textures then
  334. -- Allow mods to adjust the textures in case they have been previously
  335. -- modified for loads, weathering, etc.
  336. livery_textures = callback_functions[wagon_mod_name].on_pre_get_livery_design_from_textures(wagon_type, advtrains_livery_database.clone_textures(livery_textures), wagon_id)
  337. end
  338. -- Extract the base texture from the first texture in livery_textures.
  339. -- This will be used to identify the livery_template_name.
  340. local base_texture = livery_textures[1]
  341. local i, _ = string.find(livery_textures[1], "^", 1, true)
  342. if i and i > 1 then
  343. -- Trim any modifiers that were applied to the texture
  344. base_texture = string.sub(livery_textures[1], 1, i - 1)
  345. end
  346. -- Find the livery template for the given wagon_type and base_texture.
  347. local livery_template_name, livery_template = get_template(wagon_type, base_texture)
  348. -- Abort if no matching template was found
  349. if not livery_template then
  350. return nil
  351. end
  352. -- Create a candidate livery_design
  353. local livery_design = {
  354. wagon_type = wagon_type,
  355. livery_template_name = livery_template_name,
  356. overlays = {},
  357. }
  358. -- For each overlay clause in livery_textures, extract its texture and
  359. -- attempt to identify an overlay in the livery_template that has the same
  360. -- texture. Having a fewer number of overlays in livery_textures than the
  361. -- livery_template is OK. Having more is not. Thus, if livery_textures
  362. -- contains an overlay texture than is not present in the livery_template
  363. -- then the livery_template is not a match for livery_textures.
  364. for slot_idx, livery_texture in ipairs(livery_textures) do
  365. local pos = 1
  366. while pos do
  367. local _, j = string.find(livery_texture, "^(", pos, true)
  368. local n = j
  369. if j then
  370. local m
  371. m, n = string.find(livery_texture, "^[colorize:#", pos, true)
  372. if n then
  373. local overlay_texture = string.sub(livery_texture, j + 1, m - 1)
  374. local overlay_color = string.sub(livery_texture, n, n + 6)
  375. -- Seek a matching overlay in livery_template based on the
  376. -- found texture.
  377. local found = false
  378. for id, overlay in ipairs(livery_template.overlays) do
  379. if overlay.slot_idx == slot_idx and overlay.texture:upper() == overlay_texture:upper() then
  380. livery_design.overlays[id] = {id = id, color=overlay_color}
  381. found = true
  382. end
  383. end
  384. -- Abort if livery_texture contains an overlay that is not
  385. -- present in livery_template
  386. if not found then
  387. return nil
  388. end
  389. end
  390. end
  391. pos = n
  392. end
  393. end
  394. return livery_design
  395. end
  396. -- Given an instance of the livery_design table and an optional wagon id, this
  397. -- function should return livery strings suitable for updating an advtrains
  398. -- wagon. If specified, the wagon_id parameter is used in case the base texture
  399. -- varies by instance of the wagon due to weathering, load, etc.
  400. function advtrains_livery_database.get_livery_textures_from_design(livery_design, wagon_id)
  401. assert(livery_design, "Invalid livery design")
  402. assert(livery_design.wagon_type, "Missing wagon type in livery_design")
  403. assert(livery_design.livery_template_name, "Missing livery template name in livery_design")
  404. local wagon_mod_name = advtrains_livery_database.get_wagon_mod_name(livery_design.wagon_type)
  405. if not wagon_mod_name then
  406. return nil
  407. end
  408. -- Allow the mod to provide its own implementation for this function.
  409. if callback_functions[wagon_mod_name] and callback_functions[wagon_mod_name].on_custom_get_livery_textures_from_design then
  410. return callback_functions[wagon_mod_name].on_custom_get_livery_textures_from_design(livery_design, wagon_id)
  411. end
  412. if not livery_templates[livery_design.wagon_type] or
  413. not livery_templates[livery_design.wagon_type][livery_design.livery_template_name] or
  414. not livery_templates[livery_design.wagon_type][livery_design.livery_template_name].base_textures or
  415. not livery_templates[livery_design.wagon_type][livery_design.livery_template_name].base_textures[1] then
  416. return nil
  417. end
  418. local livery_template = livery_templates[livery_design.wagon_type][livery_design.livery_template_name]
  419. local livery_textures = advtrains_livery_database.clone_textures(livery_template.base_textures)
  420. -- Append a "colorize" clause for each valid overlay in livery_design
  421. if livery_template.overlays and livery_design.overlays then
  422. local ordered_overlays = {}
  423. for seq_num, overlay in pairs(livery_design.overlays) do
  424. table.insert(ordered_overlays, {seq_num = seq_num, id = overlay.id, color = overlay.color})
  425. end
  426. table.sort(ordered_overlays, function(a, b) return a.seq_num < b.seq_num end)
  427. for _, overlay in ipairs(ordered_overlays) do
  428. if livery_template.overlays and #livery_template.overlays > 0 and tonumber(overlay.id) <= #livery_template.overlays then
  429. local slot_idx = livery_template.overlays[overlay.id].slot_idx
  430. if overlay.id and overlay.color and livery_template.overlays[overlay.id] and slot_idx and livery_template.overlays[overlay.id].texture then
  431. local alpha = livery_template.overlays[overlay.id].alpha or 255
  432. if alpha < 0 then alpha = 0 end
  433. if alpha > 255 then alpha = 255 end
  434. local overlay_texture = "^("..livery_template.overlays[overlay.id].texture.."^[colorize:"..overlay.color..":"..alpha..")"
  435. livery_textures[slot_idx] = livery_textures[slot_idx]..overlay_texture
  436. end
  437. end
  438. end
  439. end
  440. if callback_functions[wagon_mod_name] and callback_functions[wagon_mod_name].on_post_get_livery_textures_from_design then
  441. -- Allow mods to adjust the textures in case they need to be modified
  442. -- for loads, weathering, etc.
  443. livery_textures = callback_functions[wagon_mod_name].on_post_get_livery_textures_from_design(livery_design, wagon_id, livery_textures)
  444. end
  445. return livery_textures
  446. end