patterns.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. -- LUALOCALS < ---------------------------------------------------------
  2. local error, ipairs, minetest, nodecore, pairs, rawset, string, type
  3. = error, ipairs, minetest, nodecore, pairs, rawset, string, type
  4. local string_gsub, string_lower
  5. = string.gsub, string.lower
  6. -- LUALOCALS > ---------------------------------------------------------
  7. local modname = minetest.get_current_modname()
  8. nodecore.register_concrete_pattern,
  9. nodecore.registered_concrete_patterns
  10. = nodecore.mkreg()
  11. nodecore.register_concrete_etchable,
  12. nodecore.registered_concrete_etchables
  13. = nodecore.mkreg()
  14. local function applytile(tiles, spec)
  15. if not spec then return tiles end
  16. local newt = {}
  17. for k, v in pairs(tiles) do
  18. if type(spec) == "function" then
  19. newt[k] = spec(v)
  20. else
  21. newt[k] = v .. spec
  22. end
  23. end
  24. return newt
  25. end
  26. local function patttile(etch, patt)
  27. if patt.blank then return function(...) return ... end end
  28. return function(t)
  29. return t .. "^(" .. patt.pattern_tile .. "^[opacity:"
  30. .. (etch.pattern_opacity or 64) .. ")"
  31. end
  32. end
  33. local function regetched(basenode, etch, patt)
  34. basenode = nodecore.underride({}, basenode)
  35. basenode.alternate_loose = nil
  36. basenode.after_dig_node = nil
  37. basenode.node_dig_prediction = nil
  38. basenode.silktouch = nil
  39. basenode.strata = nil
  40. local plyname = modname .. ":" .. etch.name .. "_" .. patt.name .. "_ply"
  41. if not minetest.registered_nodes[plyname] then
  42. local def = {}
  43. nodecore.underride(def, etch.pliant)
  44. nodecore.underride(def, etch)
  45. nodecore.underride(def, patt)
  46. nodecore.underride(def, basenode)
  47. def.tiles = applytile(def.tiles, patttile(etch, patt))
  48. def.tiles = applytile(def.tiles, etch.pliant_tile)
  49. def.name = nil
  50. def.description = (patt.blank and "" or (patt.description .. " "))
  51. .. "Pliant " .. basenode.description
  52. def.pattern_def = patt
  53. def.etch_def = etch
  54. minetest.register_node(":" .. plyname, def)
  55. end
  56. if not patt.blank then
  57. local pattname = modname .. ":" .. etch.name .. "_" .. patt.name
  58. if not minetest.registered_nodes[pattname] then
  59. local def = {}
  60. nodecore.underride(def, etch.solid)
  61. nodecore.underride(def, etch)
  62. nodecore.underride(def, patt)
  63. nodecore.underride(def, basenode)
  64. def.tiles = applytile(def.tiles, patttile(etch, patt))
  65. def.name = nil
  66. def.description = (patt.blank and "" or (patt.description .. " "))
  67. .. basenode.description
  68. def.pattern_def = patt
  69. def.etch_def = etch
  70. minetest.register_node(":" .. pattname, def)
  71. end
  72. end
  73. end
  74. local mudgroups = {
  75. cracky = 0,
  76. crumbly = 1,
  77. snappy = 0,
  78. choppy = 0
  79. }
  80. local function buildpatterns()
  81. for _, patt in pairs(nodecore.registered_concrete_patterns) do
  82. patt.name = patt.name or string_gsub(string_lower(patt.description),
  83. "%W", "_")
  84. patt.pattern_tile = patt.pattern_tile or string_gsub(
  85. "#_etched.png^[mask:#_pattern_" .. patt.name
  86. .. ".png", "#", modname)
  87. end
  88. for _, etch in pairs(nodecore.registered_concrete_etchables) do
  89. if not etch.basename then return error("etchable basename required") end
  90. etch.name = etch.name or string_gsub(string_lower(string_gsub(
  91. etch.basename, "^nc_", "")), "%W", "_")
  92. etch.pliant_tile = etch.pliant_tile or "^(" .. modname
  93. .. "_pliant.png^[opacity:" .. (etch.pliant_opacity or 64) .. ")"
  94. etch.pliant = etch.pliant or {}
  95. etch.pliant.groups = etch.pliant.groups or mudgroups
  96. etch.pliant.groups.concrete_etchable = 1
  97. etch.solid = etch.solid or {}
  98. etch.drop_in_place = etch.drop_in_place or etch.basenode
  99. end
  100. for _, etch in pairs(nodecore.registered_concrete_etchables) do
  101. local basenode = minetest.registered_nodes[etch.basename]
  102. if basenode then
  103. for _, patt in pairs(nodecore.registered_concrete_patterns) do
  104. regetched(basenode, etch, patt)
  105. end
  106. end
  107. end
  108. end
  109. minetest.after(0, function()
  110. local patts = nodecore.registered_concrete_patterns
  111. for i, patt in ipairs(patts) do
  112. patt.next = patts[(i < #patts) and (i + 1) or 1]
  113. end
  114. end)
  115. for k in pairs({
  116. register_concrete_pattern = true,
  117. register_concrete_etchable = true
  118. }) do
  119. local old = nodecore[k];
  120. rawset(nodecore, k, function(...)
  121. local function helper(...)
  122. buildpatterns()
  123. return ...
  124. end
  125. return helper(old(...))
  126. end)
  127. end
  128. nodecore.register_concrete_pattern({description = "Blank", blank = true})
  129. nodecore.register_concrete_pattern({description = "Bricky"})
  130. nodecore.register_concrete_pattern({description = "Vermy"})
  131. nodecore.register_concrete_pattern({description = "Hashy"})
  132. nodecore.register_concrete_pattern({description = "Bindy"})
  133. nodecore.register_concrete_pattern({description = "Verty"})
  134. nodecore.register_concrete_pattern({description = "Horzy"})
  135. nodecore.register_concrete_pattern({description = "Boxy"})
  136. nodecore.register_concrete_pattern({description = "Iceboxy"})