Unified Dyes expands the standard dye set from 15 to up to 256 colors (depending on the object to be colored) https://content.minetest.net/packages/VanessaE/unifieddyes/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1572 lines
47KB

  1. --[[
  2. Unified Dyes
  3. This mod provides an extension to the Minetest 0.4.x dye system
  4. ==============================================================================
  5. Copyright (C) 2012-2013, Vanessa Ezekowitz
  6. Email: vanessaezekowitz@gmail.com
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. ==============================================================================
  19. --]]
  20. --=====================================================================
  21. unifieddyes = {}
  22. local creative_mode = minetest.settings:get_bool("creative_mode")
  23. local S = minetest.get_translator("unifieddyes")
  24. -- the names of the various colors here came from http://www.procato.com/rgb+index/
  25. unifieddyes.HUES_EXTENDED = {
  26. { "red", 0xff, 0x00, 0x00 },
  27. { "vermilion", 0xff, 0x40, 0x00 },
  28. { "orange", 0xff, 0x80, 0x00 },
  29. { "amber", 0xff, 0xbf, 0x00 },
  30. { "yellow", 0xff, 0xff, 0x00 },
  31. { "lime", 0xbf, 0xff, 0x00 },
  32. { "chartreuse", 0x80, 0xff, 0x00 },
  33. { "harlequin", 0x40, 0xff, 0x00 },
  34. { "green", 0x00, 0xff, 0x00 },
  35. { "malachite", 0x00, 0xff, 0x40 },
  36. { "spring", 0x00, 0xff, 0x80 },
  37. { "turquoise", 0x00, 0xff, 0xbf },
  38. { "cyan", 0x00, 0xff, 0xff },
  39. { "cerulean", 0x00, 0xbf, 0xff },
  40. { "azure", 0x00, 0x80, 0xff },
  41. { "sapphire", 0x00, 0x40, 0xff },
  42. { "blue", 0x00, 0x00, 0xff },
  43. { "indigo", 0x40, 0x00, 0xff },
  44. { "violet", 0x80, 0x00, 0xff },
  45. { "mulberry", 0xbf, 0x00, 0xff },
  46. { "magenta", 0xff, 0x00, 0xff },
  47. { "fuchsia", 0xff, 0x00, 0xbf },
  48. { "rose", 0xff, 0x00, 0x80 },
  49. { "crimson", 0xff, 0x00, 0x40 }
  50. }
  51. unifieddyes.HUES_WITH_GREY = {}
  52. for _,i in ipairs(unifieddyes.HUES_EXTENDED) do
  53. table.insert(unifieddyes.HUES_WITH_GREY, i[1])
  54. end
  55. table.insert(unifieddyes.HUES_WITH_GREY, "grey")
  56. unifieddyes.HUES_WALLMOUNTED = {
  57. "red",
  58. "orange",
  59. "yellow",
  60. "green",
  61. "cyan",
  62. "blue",
  63. "violet",
  64. "magenta"
  65. }
  66. unifieddyes.SATS = {
  67. "",
  68. "_s50"
  69. }
  70. unifieddyes.VALS = {
  71. "",
  72. "medium_",
  73. "dark_"
  74. }
  75. unifieddyes.VALS_SPLIT = {
  76. "faint_",
  77. "light_",
  78. "",
  79. "medium_",
  80. "dark_"
  81. }
  82. unifieddyes.VALS_EXTENDED = {
  83. "faint_",
  84. "pastel_",
  85. "light_",
  86. "bright_",
  87. "",
  88. "medium_",
  89. "dark_"
  90. }
  91. unifieddyes.GREYS = {
  92. "white",
  93. "light_grey",
  94. "grey",
  95. "dark_grey",
  96. "black"
  97. }
  98. unifieddyes.GREYS_EXTENDED = table.copy(unifieddyes.GREYS)
  99. for i = 1, 14 do
  100. if i ~= 0 and i ~= 4 and i ~= 8 and i ~= 11 and i ~= 15 then
  101. table.insert(unifieddyes.GREYS_EXTENDED, "grey_"..i)
  102. end
  103. end
  104. local default_dyes = {
  105. "black",
  106. "blue",
  107. "brown",
  108. "cyan",
  109. "dark_green",
  110. "dark_grey",
  111. "green",
  112. "grey",
  113. "magenta",
  114. "orange",
  115. "pink",
  116. "red",
  117. "violet",
  118. "white",
  119. "yellow"
  120. }
  121. unifieddyes.player_current_dye = {}
  122. unifieddyes.player_selected_dye = {}
  123. unifieddyes.player_last_right_clicked = {}
  124. unifieddyes.palette_has_color = {}
  125. unifieddyes.player_showall = {}
  126. -- if a node with a palette is placed in the world,
  127. -- but the itemstack used to place it has no palette_index (color byte),
  128. -- create something appropriate to make it officially white.
  129. minetest.register_on_placenode(
  130. function(pos, newnode, placer, oldnode, itemstack, pointed_thing)
  131. local def = minetest.registered_items[newnode.name]
  132. if not def
  133. or not def.palette
  134. or def.after_place_node
  135. or not placer then
  136. return false
  137. end
  138. if not string.find(itemstack:to_string(), "palette_index") then
  139. local param2
  140. local color = 0
  141. if def.palette == "unifieddyes_palette_extended.png"
  142. and def.paramtype2 == "color" then
  143. param2 = 240
  144. color = 240
  145. elseif def.palette == "unifieddyes_palette_colorwallmounted.png"
  146. and def.paramtype2 == "colorwallmounted" then
  147. param2 = newnode.param2 % 8
  148. elseif string.find(def.palette, "unifieddyes_palette_")
  149. and def.paramtype2 == "colorfacedir" then -- it's a split palette
  150. param2 = newnode.param2 % 32
  151. end
  152. if param2 then
  153. minetest.swap_node(pos, {name = newnode.name, param2 = param2})
  154. minetest.get_meta(pos):set_int("palette_index", color)
  155. end
  156. end
  157. end
  158. )
  159. -- The complementary function: strip-off the color if the node being dug is still white/neutral
  160. local function move_item(item, pos, inv, digger)
  161. if not digger then return end
  162. local creative = creative_mode or minetest.check_player_privs(digger, "creative")
  163. if inv:room_for_item("main", item)
  164. and (not creative or not inv:contains_item("main", item, true)) then
  165. inv:add_item("main", item)
  166. elseif not creative then
  167. minetest.item_drop(ItemStack(item), digger, pos)
  168. end
  169. minetest.remove_node(pos)
  170. end
  171. function unifieddyes.on_dig(pos, node, digger)
  172. if not digger then return end
  173. local playername = digger:get_player_name()
  174. if minetest.is_protected(pos, playername) then
  175. minetest.record_protection_violation(pos, playername)
  176. return
  177. end
  178. local oldparam2 = minetest.get_node(pos).param2
  179. local def = minetest.registered_items[node.name]
  180. local del_color
  181. if def.paramtype2 == "color" and oldparam2 == 240 and def.palette == "unifieddyes_palette_extended.png" then
  182. del_color = true
  183. elseif def.paramtype2 == "colorwallmounted" and math.floor(oldparam2 / 8) == 0 and def.palette == "unifieddyes_palette_colorwallmounted.png" then
  184. del_color = true
  185. elseif def.paramtype2 == "colorfacedir" and math.floor(oldparam2 / 32) == 0 and string.find(def.palette, "unifieddyes_palette_") then
  186. del_color = true
  187. end
  188. local inv = digger:get_inventory()
  189. if del_color then
  190. move_item(node.name, pos, inv, digger)
  191. else
  192. return minetest.node_dig(pos, node, digger)
  193. end
  194. end
  195. -- just stubs to keep old mods from crashing when expecting auto-coloring
  196. -- or getting back the dye on dig.
  197. function unifieddyes.recolor_on_place(foo)
  198. end
  199. function unifieddyes.after_dig_node(foo)
  200. end
  201. -- This helper function creates multiple copies of the passed node,
  202. -- for the split palette - one per hue, plus grey - and assigns
  203. -- proper palettes and other attributes
  204. function unifieddyes.generate_split_palette_nodes(name, def, drop)
  205. for _, color in ipairs(unifieddyes.HUES_WITH_GREY) do
  206. local def2 = table.copy(def)
  207. local desc_color = string.gsub(string.upper(string.sub(color, 1, 1))..string.sub(color, 2), "_", " ")
  208. if string.sub(def2.description, -1) == ")" then
  209. def2.description = string.sub(def2.description, 1, -2)..", "..desc_color.." shades)"
  210. else
  211. def2.description = def2.description.."("..desc_color.." shades)"
  212. end
  213. def2.palette = "unifieddyes_palette_"..color.."s.png"
  214. def2.paramtype2 = "colorfacedir"
  215. def2.groups.ud_param2_colorable = 1
  216. if drop then
  217. def2.drop = {
  218. items = {
  219. {items = {drop.."_"..color}, inherit_color = true },
  220. }
  221. }
  222. end
  223. minetest.register_node(":"..name.."_"..color, def2)
  224. end
  225. end
  226. -- This helper function creates a colored itemstack
  227. function unifieddyes.make_colored_itemstack(item, palette, color)
  228. local paletteidx = unifieddyes.getpaletteidx(color, palette)
  229. local stack = ItemStack(item)
  230. stack:get_meta():set_int("palette_index", paletteidx)
  231. return stack:to_string(),paletteidx
  232. end
  233. -- these helper functions register all of the recipes needed to create colored
  234. -- nodes with any of the dyes supported by that node's palette.
  235. local function register_c(craft, h, sat, val)
  236. local hue = (type(h) == "table") and h[1] or h
  237. local color = ""
  238. if val then
  239. if craft.palette == "wallmounted" then
  240. color = val..hue..sat
  241. else
  242. color = val..hue..sat
  243. end
  244. else
  245. color = hue -- if val is nil, then it's grey.
  246. end
  247. local dye = "dye:"..color
  248. local recipe = minetest.serialize(craft.recipe)
  249. recipe = string.gsub(recipe, "MAIN_DYE", dye)
  250. recipe = string.gsub(recipe, "NEUTRAL_NODE", craft.neutral_node)
  251. local newrecipe = minetest.deserialize(recipe)
  252. local coutput = craft.output or ""
  253. local output = coutput
  254. if craft.output_prefix then
  255. if craft.palette ~= "split" then
  256. output = craft.output_prefix..color..craft.output_suffix..coutput
  257. else
  258. if hue == "white" or hue == "black" or string.find(hue, "grey") then
  259. output = craft.output_prefix.."grey"..craft.output_suffix..coutput
  260. elseif hue == "pink" then
  261. dye = "dye:light_red"
  262. output = craft.output_prefix.."red"..craft.output_suffix..coutput
  263. else
  264. output = craft.output_prefix..hue..craft.output_suffix..coutput
  265. end
  266. end
  267. end
  268. local colored_itemstack =
  269. unifieddyes.make_colored_itemstack(output, craft.palette, dye)
  270. minetest.register_craft({
  271. output = colored_itemstack,
  272. type = craft.type,
  273. recipe = newrecipe
  274. })
  275. end
  276. function unifieddyes.register_color_craft(craft)
  277. local hues_table = unifieddyes.HUES_EXTENDED
  278. local sats_table = unifieddyes.SATS
  279. local vals_table = unifieddyes.VALS_SPLIT
  280. local greys_table = unifieddyes.GREYS
  281. if craft.palette == "wallmounted" then
  282. register_c(craft, "green", "", "light_")
  283. register_c(craft, "blue", "", "light_")
  284. hues_table = unifieddyes.HUES_WALLMOUNTED
  285. sats_table = {""}
  286. vals_table = unifieddyes.VALS
  287. elseif craft.palette == "extended" then
  288. vals_table = unifieddyes.VALS_EXTENDED
  289. greys_table = unifieddyes.GREYS_EXTENDED
  290. end
  291. for _, hue in ipairs(hues_table) do
  292. for _, val in ipairs(vals_table) do
  293. for _, sat in ipairs(sats_table) do
  294. if sat == "_s50" and val ~= "" and val ~= "medium_" and val ~= "dark_" then break end
  295. register_c(craft, hue, sat, val)
  296. end
  297. end
  298. end
  299. for _, grey in ipairs(greys_table) do
  300. register_c(craft, grey)
  301. end
  302. register_c(craft, "pink")
  303. end
  304. -- code borrowed from homedecor
  305. -- call this function to reset the rotation of a "wallmounted" object on place
  306. function unifieddyes.fix_rotation(pos, placer, itemstack, pointed_thing)
  307. local node = minetest.get_node(pos)
  308. local colorbits = node.param2 - (node.param2 % 8)
  309. local yaw = placer:get_look_horizontal()
  310. local dir = minetest.yaw_to_dir(yaw) -- -1.5)
  311. local pitch = placer:get_look_vertical()
  312. local fdir = minetest.dir_to_wallmounted(dir)
  313. if pitch < -(math.pi/8) then
  314. fdir = 0
  315. elseif pitch > math.pi/8 then
  316. fdir = 1
  317. end
  318. minetest.swap_node(pos, { name = node.name, param2 = fdir+colorbits })
  319. end
  320. -- use this when you have a "wallmounted" node that should never be oriented
  321. -- to floor or ceiling...
  322. function unifieddyes.fix_rotation_nsew(pos, placer, itemstack, pointed_thing)
  323. local node = minetest.get_node(pos)
  324. local colorbits = node.param2 - (node.param2 % 8)
  325. local yaw = placer:get_look_horizontal()
  326. local dir = minetest.yaw_to_dir(yaw+1.5)
  327. local fdir = minetest.dir_to_wallmounted(dir)
  328. minetest.swap_node(pos, { name = node.name, param2 = fdir+colorbits })
  329. end
  330. -- ... and use this one to force that kind of node off of floor/ceiling
  331. -- orientation after the screwdriver rotates it.
  332. function unifieddyes.fix_after_screwdriver_nsew(pos, node, user, mode, new_param2)
  333. local new_fdir = new_param2 % 8
  334. local color = new_param2 - new_fdir
  335. if new_fdir < 2 then
  336. new_fdir = 2
  337. minetest.swap_node(pos, { name = node.name, param2 = new_fdir + color })
  338. return true
  339. end
  340. end
  341. function unifieddyes.is_buildable_to(placer_name, ...)
  342. for _, pos in ipairs({...}) do
  343. local node = minetest.get_node_or_nil(pos)
  344. local def = node and minetest.registered_nodes[node.name]
  345. if not (def and def.buildable_to) or minetest.is_protected(pos, placer_name) then
  346. return false
  347. end
  348. end
  349. return true
  350. end
  351. function unifieddyes.get_hsv(name) -- expects a node/item name
  352. local hue = ""
  353. local a,b
  354. for _, i in ipairs(unifieddyes.HUES_EXTENDED) do
  355. a,b = string.find(name, "_"..i[1])
  356. if a then
  357. hue = i[1]
  358. break
  359. end
  360. end
  361. if string.find(name, "_light_grey") then hue = "light_grey"
  362. elseif string.find(name, "_lightgrey") then hue = "light_grey"
  363. elseif string.find(name, "_dark_grey") then hue = "dark_grey"
  364. elseif string.find(name, "_darkgrey") then hue = "dark_grey"
  365. elseif string.find(name, "_grey") then hue = "grey"
  366. elseif string.find(name, "_white") then hue = "white"
  367. elseif string.find(name, "_black") then hue = "black"
  368. end
  369. local sat = ""
  370. if string.find(name, "_s50") then sat = "_s50" end
  371. local val = ""
  372. if string.find(name, "dark_") then val = "dark_" end
  373. if string.find(name, "medium_") then val = "medium_" end
  374. if string.find(name, "light_") then val = "light_" end
  375. return hue, sat, val
  376. end
  377. -- code partially borrowed from cheapie's plasticbox mod
  378. -- in the function below, color is just a color string, while
  379. -- palette_type can be:
  380. --
  381. -- "extended" = 256 color palette
  382. -- "split" = 200 color palette split into pieces for colorfacedir
  383. -- "wallmounted" = 32-color abridged palette
  384. function unifieddyes.getpaletteidx(color, palette_type)
  385. local origcolor = color
  386. local aliases = {
  387. ["pink"] = "light_red",
  388. ["brown"] = "medium_orange",
  389. ["azure"] = "light_blue"
  390. }
  391. local grayscale = {
  392. ["white"] = 1,
  393. ["light_grey"] = 2,
  394. ["grey"] = 3,
  395. ["dark_grey"] = 4,
  396. ["black"] = 5,
  397. }
  398. local grayscale_extended = {
  399. ["white"] = 0,
  400. ["grey_14"] = 1,
  401. ["grey_13"] = 2,
  402. ["grey_12"] = 3,
  403. ["light_grey"] = 4,
  404. ["grey_11"] = 4,
  405. ["grey_10"] = 5,
  406. ["grey_9"] = 6,
  407. ["grey_8"] = 7,
  408. ["grey"] = 7,
  409. ["grey_7"] = 8,
  410. ["grey_6"] = 9,
  411. ["grey_5"] = 10,
  412. ["grey_4"] = 11,
  413. ["dark_grey"] = 11,
  414. ["grey_3"] = 12,
  415. ["grey_2"] = 13,
  416. ["grey_1"] = 14,
  417. ["black"] = 15,
  418. }
  419. local grayscale_wallmounted = {
  420. ["white"] = 0,
  421. ["light_grey"] = 1,
  422. ["grey"] = 2,
  423. ["dark_grey"] = 3,
  424. ["black"] = 4,
  425. }
  426. local hues_extended = {
  427. ["red"] = 0,
  428. ["vermilion"] = 1,
  429. ["orange"] = 2,
  430. ["amber"] = 3,
  431. ["yellow"] = 4,
  432. ["lime"] = 5,
  433. ["chartreuse"] = 6,
  434. ["harlequin"] = 7,
  435. ["green"] = 8,
  436. ["malachite"] = 9,
  437. ["spring"] = 10,
  438. ["aqua"] = 10,
  439. ["turquoise"] = 11,
  440. ["cyan"] = 12,
  441. ["cerulean"] = 13,
  442. ["azure"] = 14,
  443. ["skyblue"] = 14,
  444. ["sapphire"] = 15,
  445. ["blue"] = 16,
  446. ["indigo"] = 17,
  447. ["violet"] = 18,
  448. ["mulberry"] = 19,
  449. ["magenta"] = 20,
  450. ["fuchsia"] = 21,
  451. ["rose"] = 22,
  452. ["redviolet"] = 22,
  453. ["crimson"] = 23,
  454. }
  455. local hues_wallmounted = {
  456. ["red"] = 0,
  457. ["orange"] = 1,
  458. ["yellow"] = 2,
  459. ["green"] = 3,
  460. ["cyan"] = 4,
  461. ["blue"] = 5,
  462. ["violet"] = 6,
  463. ["magenta"] = 7
  464. }
  465. local shades = {
  466. [""] = 1,
  467. ["s50"] = 2,
  468. ["light"] = 3,
  469. ["medium"] = 4,
  470. ["mediums50"] = 5,
  471. ["dark"] = 6,
  472. ["darks50"] = 7,
  473. }
  474. local shades_split = {
  475. ["faint"] = 0,
  476. [""] = 1,
  477. ["s50"] = 2,
  478. ["light"] = 3,
  479. ["medium"] = 4,
  480. ["mediums50"] = 5,
  481. ["dark"] = 6,
  482. ["darks50"] = 7,
  483. }
  484. local shades_extended = {
  485. ["faint"] = 0,
  486. ["pastel"] = 1,
  487. ["light"] = 2,
  488. ["bright"] = 3,
  489. [""] = 4,
  490. ["s50"] = 5,
  491. ["medium"] = 6,
  492. ["mediums50"] = 7,
  493. ["dark"] = 8,
  494. ["darks50"] = 9
  495. }
  496. local shades_wallmounted = {
  497. [""] = 1,
  498. ["medium"] = 2,
  499. ["dark"] = 3
  500. }
  501. if string.sub(color,1,4) == "dye:" then
  502. color = string.sub(color,5,-1)
  503. elseif string.sub(color,1,12) == "unifieddyes:" then
  504. color = string.sub(color,13,-1)
  505. else
  506. return
  507. end
  508. if palette_type == "wallmounted" then
  509. if grayscale_wallmounted[color] then
  510. return (grayscale_wallmounted[color] * 8), 0
  511. end
  512. elseif palette_type == "split" then
  513. if grayscale[color] then
  514. return (grayscale[color] * 32), 0
  515. end
  516. elseif palette_type == "extended" then
  517. if grayscale_extended[color] then
  518. return grayscale_extended[color]+240, 0
  519. end
  520. end
  521. local shade = "" -- assume full
  522. if string.sub(color,1,6) == "faint_" then
  523. shade = "faint"
  524. color = string.sub(color,7,-1)
  525. elseif string.sub(color,1,7) == "pastel_" then
  526. shade = "pastel"
  527. color = string.sub(color,8,-1)
  528. elseif string.sub(color,1,6) == "light_" then
  529. shade = "light"
  530. color = string.sub(color,7,-1)
  531. elseif string.sub(color,1,7) == "bright_" then
  532. shade = "bright"
  533. color = string.sub(color,8,-1)
  534. elseif string.sub(color,1,7) == "medium_" then
  535. shade = "medium"
  536. color = string.sub(color,8,-1)
  537. elseif string.sub(color,1,5) == "dark_" then
  538. shade = "dark"
  539. color = string.sub(color,6,-1)
  540. end
  541. if string.sub(color,-4,-1) == "_s50" then
  542. shade = shade.."s50"
  543. color = string.sub(color,1,-5)
  544. end
  545. if palette_type == "wallmounted" then
  546. if color == "green" and shade == "light" then return 48,3
  547. elseif color == "brown" then return 17,1
  548. elseif color == "pink" then return 56,7
  549. elseif color == "blue" and shade == "light" then return 40,5
  550. elseif hues_wallmounted[color] and shades_wallmounted[shade] then
  551. return (shades_wallmounted[shade] * 64 + hues_wallmounted[color] * 8), hues_wallmounted[color]
  552. end
  553. else
  554. if color == "brown" then
  555. color = "orange"
  556. shade = "medium"
  557. elseif color == "pink" then
  558. color = "red"
  559. shade = "light"
  560. end
  561. if palette_type == "split" then -- it's colorfacedir
  562. if hues_extended[color] and shades_split[shade] then
  563. return (shades_split[shade] * 32), hues_extended[color]+1
  564. end
  565. elseif palette_type == "extended" then
  566. if hues_extended[color] and shades_extended[shade] then
  567. return (hues_extended[color] + shades_extended[shade]*24), hues_extended[color]
  568. end
  569. end
  570. end
  571. end
  572. function unifieddyes.get_color_from_dye_name(name)
  573. if name == "dye:black" then
  574. return "000000"
  575. elseif name == "dye:white" then
  576. return "ffffff"
  577. end
  578. local item = minetest.registered_items[name]
  579. if not item then return end
  580. local inv_image = item.inventory_image
  581. if not inv_image then return end
  582. return string.match(inv_image,"colorize:#(......):200")
  583. end
  584. -- punch-to-recolor using the airbrush
  585. function unifieddyes.on_airbrush(itemstack, player, pointed_thing)
  586. local player_name = player:get_player_name()
  587. local painting_with = nil
  588. if unifieddyes.player_current_dye[player_name] then
  589. painting_with = unifieddyes.player_current_dye[player_name]
  590. end
  591. if not painting_with then
  592. minetest.chat_send_player(player_name, "*** You need to set a color first.")
  593. minetest.chat_send_player(player_name, "*** Right-click any random node to open the color selector,")
  594. minetest.chat_send_player(player_name, "*** or shift+right-click a colorized node to use its color.")
  595. minetest.chat_send_player(player_name, "*** Be sure to click \"Accept\", or the color you select will be ignored.")
  596. return
  597. end
  598. local pos = minetest.get_pointed_thing_position(pointed_thing)
  599. if not pos then
  600. local look_angle = player:get_look_vertical()
  601. if look_angle > -1.55 then
  602. minetest.chat_send_player(player_name, "*** No node selected")
  603. else
  604. local hexcolor = unifieddyes.get_color_from_dye_name(painting_with)
  605. if hexcolor then
  606. local r = tonumber(string.sub(hexcolor,1,2),16)
  607. local g = tonumber(string.sub(hexcolor,3,4),16)
  608. local b = tonumber(string.sub(hexcolor,5,6),16)
  609. player:set_sky({r=r,g=g,b=b,a=255},"plain")
  610. end
  611. end
  612. return
  613. end
  614. local node = minetest.get_node(pos)
  615. local def = minetest.registered_items[node.name]
  616. if not def then return end
  617. if minetest.is_protected(pos, player_name) then
  618. minetest.chat_send_player(player_name, "*** Sorry, someone else owns that node.")
  619. return
  620. end
  621. if not (def.groups and def.groups.ud_param2_colorable and def.groups.ud_param2_colorable > 0) then
  622. minetest.chat_send_player(player_name, "*** That node can't be colored.")
  623. return
  624. end
  625. local palette = nil
  626. local fdir = 0
  627. if not def or not def.palette then
  628. minetest.chat_send_player(player_name, "*** That node can't be colored -- it's either undefined or has no palette.")
  629. return
  630. elseif def.palette == "unifieddyes_palette_extended.png" then
  631. palette = "extended"
  632. elseif def.palette == "unifieddyes_palette_colorwallmounted.png" then
  633. palette = "wallmounted"
  634. fdir = node.param2 % 8
  635. elseif def.palette ~= "unifieddyes_palette_extended.png"
  636. and def.palette ~= "unifieddyes_palette_colorwallmounted.png"
  637. and string.find(def.palette, "unifieddyes_palette_") then
  638. palette = "split"
  639. fdir = node.param2 % 32
  640. else
  641. minetest.chat_send_player(player_name, "*** That node can't be colored -- it has an invalid color mode.")
  642. return
  643. end
  644. local idx, hue = unifieddyes.getpaletteidx(painting_with, palette)
  645. local inv = player:get_inventory()
  646. if (not creative or not creative.is_enabled_for(player_name)) and not inv:contains_item("main", painting_with) then
  647. local suff = ""
  648. if not idx then
  649. suff = " Besides, "..string.sub(painting_with, 5).." can't be applied to that node."
  650. end
  651. minetest.chat_send_player(player_name, "*** You're in survival mode, and you're out of "..string.sub(painting_with, 5).."."..suff)
  652. return
  653. end
  654. if not idx then
  655. minetest.chat_send_player(player_name, "*** "..string.sub(painting_with, 5).." can't be applied to that node.")
  656. return
  657. end
  658. local oldidx = node.param2 - fdir
  659. local name = def.airbrush_replacement_node or node.name
  660. if palette == "split" then
  661. local modname = string.sub(name, 1, string.find(name, ":")-1)
  662. local nodename2 = string.sub(name, string.find(name, ":")+1)
  663. local oldcolor = "snozzberry"
  664. local newcolor = "razzberry" -- intentionally misspelled ;-)
  665. if def.ud_color_start and def.ud_color_end then
  666. oldcolor = string.sub(node.name, def.ud_color_start, def.ud_color_end)
  667. newcolor = string.sub(painting_with, 5)
  668. else
  669. if hue ~= 0 then
  670. newcolor = unifieddyes.HUES_EXTENDED[hue][1]
  671. else
  672. newcolor = "grey"
  673. end
  674. if def.airbrush_replacement_node then
  675. oldcolor = "grey"
  676. else
  677. local s = string.sub(def.palette, 21)
  678. oldcolor = string.sub(s, 1, string.find(s, "s.png")-1)
  679. end
  680. end
  681. name = modname..":"..string.gsub(nodename2, oldcolor, newcolor)
  682. if not minetest.registered_items[name] then
  683. minetest.chat_send_player(player_name, "*** "..string.sub(painting_with, 5).." can't be applied to that node.")
  684. return
  685. end
  686. elseif idx == oldidx then
  687. return
  688. end
  689. minetest.swap_node(pos, {name = name, param2 = fdir + idx})
  690. if not creative or not creative.is_enabled_for(player_name) then
  691. inv:remove_item("main", painting_with)
  692. return
  693. end
  694. end
  695. -- get a node's dye color based on its palette and param2
  696. function unifieddyes.color_to_name(param2, def)
  697. if not param2 or not def or not def.palette then return end
  698. if def.palette == "unifieddyes_palette_extended.png" then
  699. local color = param2
  700. local v = 0
  701. local s = 1
  702. if color < 24 then v = 1
  703. elseif color > 23 and color < 48 then v = 2
  704. elseif color > 47 and color < 72 then v = 3
  705. elseif color > 71 and color < 96 then v = 4
  706. elseif color > 95 and color < 120 then v = 5
  707. elseif color > 119 and color < 144 then v = 5 s = 2
  708. elseif color > 143 and color < 168 then v = 6
  709. elseif color > 167 and color < 192 then v = 6 s = 2
  710. elseif color > 191 and color < 216 then v = 7
  711. elseif color > 215 and color < 240 then v = 7 s = 2
  712. end
  713. if color > 239 then
  714. if color == 240 then return "white"
  715. elseif color == 244 then return "light_grey"
  716. elseif color == 247 then return "grey"
  717. elseif color == 251 then return "dark_grey"
  718. elseif color == 255 then return "black"
  719. else return "grey_"..15-(color-240)
  720. end
  721. else
  722. local h = color - math.floor(color/24)*24
  723. return unifieddyes.VALS_EXTENDED[v]..unifieddyes.HUES_EXTENDED[h+1][1]..unifieddyes.SATS[s]
  724. end
  725. elseif def.palette == "unifieddyes_palette_colorwallmounted.png" then
  726. local color = math.floor(param2 / 8)
  727. if color == 0 then return "white"
  728. elseif color == 1 then return "light_grey"
  729. elseif color == 2 then return "grey"
  730. elseif color == 3 then return "dark_grey"
  731. elseif color == 4 then return "black"
  732. elseif color == 5 then return "light_blue"
  733. elseif color == 6 then return "light_green"
  734. elseif color == 7 then return "pink"
  735. end
  736. local v = math.floor(color/8)
  737. local h = color - v * 8
  738. return unifieddyes.VALS[v]..unifieddyes.HUES_WALLMOUNTED[h+1]
  739. elseif string.find(def.palette, "unifieddyes_palette") then -- it's the split palette
  740. -- palette names in this mode are always "unifieddyes_palette_COLORs.png"
  741. local s = string.sub(def.palette, 21)
  742. local color = string.sub(s, 1, string.find(s, "s.png")-1)
  743. local v = math.floor(param2/32)
  744. if color ~= "grey" then
  745. if v == 0 then return "faint_"..color
  746. elseif v == 1 then return color
  747. elseif v == 2 then return color.."_s50"
  748. elseif v == 3 then return "light_"..color
  749. elseif v == 4 then return "medium_"..color
  750. elseif v == 5 then return "medium_"..color.."_s50"
  751. elseif v == 6 then return "dark_"..color
  752. elseif v == 7 then return "dark_"..color.."_s50"
  753. end
  754. else
  755. if v > 0 and v < 6 then return unifieddyes.GREYS[v]
  756. else return "white"
  757. end
  758. end
  759. end
  760. end
  761. local hps = 0.6 -- horizontal position scale
  762. local vps = 1.3 -- vertical position scale
  763. local vs = 0.1 -- vertical shift/offset
  764. local color_button_size = ";0.75,0.75;"
  765. local color_square_size = ";0.69,0.69;"
  766. function unifieddyes.make_readable_color(color)
  767. local s = string.gsub(color, "_", " ")
  768. s = string.gsub(s, "s50", "(low saturation)")
  769. return s
  770. end
  771. function unifieddyes.make_colored_square(hexcolor, colorname, showall, creative, painting_with, nodepalette, hp, v2, selindic, inv, explist)
  772. local dye = "dye:"..colorname
  773. local overlay = ""
  774. local colorize = minetest.formspec_escape("^[colorize:#"..hexcolor..":255")
  775. if not creative and inv:contains_item("main", dye) then
  776. overlay = "^unifieddyes_onhand_overlay.png"
  777. end
  778. local unavail_overlay = ""
  779. if not showall and not unifieddyes.palette_has_color[nodepalette.."_"..colorname]
  780. or (explist and not explist[colorname]) then
  781. if overlay == "" then
  782. unavail_overlay = "^unifieddyes_unavailable_overlay.png"
  783. else
  784. unavail_overlay = "^unifieddyes_onhand_unavailable_overlay.png"
  785. end
  786. end
  787. local tooltip = "tooltip["..colorname..";"..
  788. unifieddyes.make_readable_color(colorname)..
  789. "\n(dye:"..colorname..")]"
  790. if dye == painting_with then
  791. overlay = "^unifieddyes_select_overlay.png"
  792. selindic = "unifieddyes_white_square.png"..colorize..overlay..unavail_overlay.."]"..tooltip
  793. end
  794. local form
  795. if unavail_overlay == "" then
  796. form = "image_button["..
  797. (hp*hps)..","..(v2*vps+vs)..
  798. color_button_size..
  799. "unifieddyes_white_square.png"..colorize..overlay..unavail_overlay..";"..
  800. colorname..";]"..
  801. tooltip
  802. else
  803. form = "image["..
  804. (hp*hps)..","..(v2*vps+vs)..
  805. color_square_size..
  806. "unifieddyes_white_square.png"..colorize..overlay..unavail_overlay.."]"..
  807. tooltip
  808. end
  809. return form, selindic
  810. end
  811. function unifieddyes.show_airbrush_form(player)
  812. if not player then return end
  813. local t = {}
  814. local player_name = player:get_player_name()
  815. local painting_with = unifieddyes.player_selected_dye[player_name] or unifieddyes.player_current_dye[player_name]
  816. local creative = creative and creative.is_enabled_for(player_name)
  817. local inv = player:get_inventory()
  818. local nodepalette = "extended"
  819. local showall = unifieddyes.player_showall[player_name]
  820. t[1] = "size[14.5,8.5]label[7,-0.3;"..S("Select a color:").."]"
  821. local selindic = "unifieddyes_select_overlay.png^unifieddyes_question.png]"
  822. local last_right_click = unifieddyes.player_last_right_clicked[player_name]
  823. if last_right_click then
  824. if last_right_click.def and last_right_click.def.palette then
  825. if last_right_click.def.palette == "unifieddyes_palette_colorwallmounted.png" then
  826. nodepalette = "wallmounted"
  827. elseif last_right_click.def.palette == "unifieddyes_palette_extended.png" then
  828. t[#t+1] = "label[0.5,8.25;"..S("(Right-clicked a node that supports all 256 colors, showing them all)").."]"
  829. showall = true
  830. elseif last_right_click.def.palette ~= "unifieddyes_palette_extended.png"
  831. and last_right_click.def.palette ~= "unifieddyes_palette_colorwallmounted.png"
  832. and string.find(last_right_click.def.palette, "unifieddyes_palette_") then
  833. nodepalette = "split"
  834. end
  835. end
  836. end
  837. if not last_right_click.def.groups
  838. or not last_right_click.def.groups.ud_param2_colorable
  839. or not last_right_click.def.palette
  840. or not string.find(last_right_click.def.palette, "unifieddyes_palette_") then
  841. t[#t+1] = "label[0.5,8.25;"..S("(Right-clicked a node not supported by the Airbrush, showing all colors)").."]"
  842. end
  843. local explist = last_right_click.def.explist
  844. for v = 0, 6 do
  845. local val = unifieddyes.VALS_EXTENDED[v+1]
  846. local sat = ""
  847. local v2=(v/2)
  848. for hi, h in ipairs(unifieddyes.HUES_EXTENDED) do
  849. local hue = h[1]
  850. local hp=hi-1
  851. local r = h[2]
  852. local g = h[3]
  853. local b = h[4]
  854. local factor = 40
  855. if v > 3 then
  856. factor = 75
  857. v2 = (v-2)
  858. end
  859. local r2 = math.max(math.min(r + (4-v)*factor, 255), 0)
  860. local g2 = math.max(math.min(g + (4-v)*factor, 255), 0)
  861. local b2 = math.max(math.min(b + (4-v)*factor, 255), 0)
  862. local hexcolor = string.format("%02x", r2)..string.format("%02x", g2)..string.format("%02x", b2)
  863. local f
  864. f, selindic = unifieddyes.make_colored_square(hexcolor, val..hue..sat, showall, creative, painting_with, nodepalette, hp, v2, selindic, inv, explist)
  865. t[#t+1] = f
  866. end
  867. if v > 3 then
  868. sat = "_s50"
  869. v2 = (v-1.5)
  870. for hi, h in ipairs(unifieddyes.HUES_EXTENDED) do
  871. local hue = h[1]
  872. local hp=hi-1
  873. local r = h[2]
  874. local g = h[3]
  875. local b = h[4]
  876. local factor = 75
  877. local pr = 0.299
  878. local pg = 0.587
  879. local pb = 0.114
  880. local r2 = math.max(math.min(r + (4-v)*factor, 255), 0)
  881. local g2 = math.max(math.min(g + (4-v)*factor, 255), 0)
  882. local b2 = math.max(math.min(b + (4-v)*factor, 255), 0)
  883. local p = math.sqrt(r2*r2*pr + g2*g2*pg + b2*b2*pb)
  884. local r3 = math.floor(p+(r2-p)*0.5)
  885. local g3 = math.floor(p+(g2-p)*0.5)
  886. local b3 = math.floor(p+(b2-p)*0.5)
  887. local hexcolor = string.format("%02x", r3)..string.format("%02x", g3)..string.format("%02x", b3)
  888. local f
  889. f, selindic = unifieddyes.make_colored_square(hexcolor, val..hue..sat, showall, creative, painting_with, nodepalette, hp, v2, selindic, inv, explist)
  890. t[#t+1] = f
  891. end
  892. end
  893. end
  894. local v2=5
  895. for y = 0, 15 do
  896. local hp=15-y
  897. local hexgrey = string.format("%02x", y*17)..string.format("%02x", y*17)..string.format("%02x", y*17)
  898. local grey = "grey_"..y
  899. if y == 0 then grey = "black"
  900. elseif y == 4 then grey = "dark_grey"
  901. elseif y == 8 then grey = "grey"
  902. elseif y == 11 then grey = "light_grey"
  903. elseif y == 15 then grey = "white"
  904. end
  905. local f
  906. f, selindic = unifieddyes.make_colored_square(hexgrey, grey, showall, creative, painting_with, nodepalette, hp, v2, selindic, inv, explist)
  907. t[#t+1] = f
  908. end
  909. if not creative then
  910. t[#t+1] = "image[10,"
  911. t[#t+1] = (vps*5.55+vs)
  912. t[#t+1] = color_button_size
  913. t[#t+1] = "unifieddyes_onhand_overlay.png]label[10.7,"
  914. t[#t+1] = (vps*5.51+vs)
  915. t[#t+1] = ";"..S("Dyes").."]"
  916. t[#t+1] = "label[10.7,"
  917. t[#t+1] = (vps*5.67+vs)
  918. t[#t+1] = ";on hand]"
  919. end
  920. t[#t+1] = "image[10,"
  921. t[#t+1] = (vps*5+vs)
  922. t[#t+1] = color_button_size
  923. t[#t+1] = selindic
  924. if painting_with then
  925. t[#t+1] = "label[10.7,"
  926. t[#t+1] = (vps*4.90+vs)
  927. t[#t+1] = ";"..S("Your selection:").."]"
  928. t[#t+1] = "label[10.7,"
  929. t[#t+1] = (vps*5.07+vs)
  930. t[#t+1] = ";"
  931. t[#t+1] = unifieddyes.make_readable_color(string.sub(painting_with, 5))
  932. t[#t+1] = "]label[10.7,"
  933. t[#t+1] = (vps*5.24+vs)
  934. t[#t+1] = ";("
  935. t[#t+1] = painting_with
  936. t[#t+1] = ")]"
  937. else
  938. t[#t+1] = "label[10.7,"
  939. t[#t+1] = (vps*5.07+vs)
  940. t[#t+1] = ";"..S("Your selection").."]"
  941. end
  942. t[#t+1] = "button_exit[10.5,8;2,1;cancel;"..S("Cancel").."]button_exit[12.5,8;2,1;accept;"..S("Accept").."]"
  943. if last_right_click and last_right_click.def and nodepalette ~= "extended" then
  944. if showall then
  945. t[#t+1] = "button[0,8;2,1;show_avail;"..S("Show Available").."]"
  946. t[#t+1] = "label[2,8.25;"..S("(Currently showing all 256 colors)").."]"
  947. else
  948. t[#t+1] = "button[0,8;2,1;show_all;"..S("Show All Colors").."]"
  949. t[#t+1] = "label[2,8.25;"..S("(Currently only showing what the right-clicked node can use)").."]"
  950. end
  951. end
  952. minetest.show_formspec(player_name, "unifieddyes:dye_select_form", table.concat(t))
  953. end
  954. minetest.register_tool("unifieddyes:airbrush", {
  955. description = S("Dye Airbrush"),
  956. inventory_image = "unifieddyes_airbrush.png",
  957. use_texture_alpha = true,
  958. tool_capabilities = {
  959. full_punch_interval=0.1,
  960. },
  961. range = 12,
  962. on_use = unifieddyes.on_airbrush,
  963. on_place = function(itemstack, placer, pointed_thing)
  964. local keys = placer:get_player_control()
  965. local player_name = placer:get_player_name()
  966. local pos = minetest.get_pointed_thing_position(pointed_thing)
  967. local node
  968. local def
  969. if pos then node = minetest.get_node(pos) end
  970. if node then def = minetest.registered_items[node.name] end
  971. unifieddyes.player_last_right_clicked[player_name] = {pos = pos, node = node, def = def}
  972. if not keys.sneak then
  973. unifieddyes.show_airbrush_form(placer)
  974. elseif keys.sneak then
  975. if not pos or not def then return end
  976. local newcolor = unifieddyes.color_to_name(node.param2, def)
  977. if not newcolor then
  978. minetest.chat_send_player(player_name, "*** That node is uncolored.")
  979. return
  980. end
  981. minetest.chat_send_player(player_name, "*** Switching to "..newcolor.." for the airbrush, to match that node.")
  982. unifieddyes.player_current_dye[player_name] = "dye:"..newcolor
  983. end
  984. end
  985. })
  986. minetest.register_craft( {
  987. output = "unifieddyes:airbrush",
  988. recipe = {
  989. { "basic_materials:brass_ingot", "", "basic_materials:plastic_sheet" },
  990. { "", "default:steel_ingot", "" },
  991. { "", "", "default:steel_ingot" }
  992. },
  993. })
  994. minetest.register_on_player_receive_fields(function(player, formname, fields)
  995. if formname == "unifieddyes:dye_select_form" then
  996. local player_name = player:get_player_name()
  997. local nodepalette = "extended"
  998. local showall = unifieddyes.player_showall[player_name]
  999. local last_right_click = unifieddyes.player_last_right_clicked[player_name]
  1000. if last_right_click and last_right_click.def then
  1001. if last_right_click.def.palette then
  1002. if last_right_click.def.palette == "unifieddyes_palette_colorwallmounted.png" then
  1003. nodepalette = "wallmounted"
  1004. elseif last_right_click.def.palette ~= "unifieddyes_palette_extended.png" then
  1005. nodepalette = "split"
  1006. end
  1007. end
  1008. end
  1009. if fields.show_all then
  1010. unifieddyes.player_showall[player_name] = true
  1011. unifieddyes.show_airbrush_form(player)
  1012. return
  1013. elseif fields.show_avail then
  1014. unifieddyes.player_showall[player_name] = false
  1015. unifieddyes.show_airbrush_form(player)
  1016. return
  1017. elseif fields.quit then
  1018. if fields.accept then
  1019. local dye = unifieddyes.player_selected_dye[player_name]
  1020. if not dye then
  1021. minetest.chat_send_player(player_name, "*** Clicked \"Accept\", but no color was selected!")
  1022. return
  1023. elseif not showall
  1024. and not unifieddyes.palette_has_color[nodepalette.."_"..string.sub(dye, 5)] then
  1025. minetest.chat_send_player(player_name, "*** Clicked \"Accept\", but the selected color can't be used on the")
  1026. minetest.chat_send_player(player_name, "*** node that was right-clicked (and \"Show All\" wasn't in effect).")
  1027. if unifieddyes.player_current_dye[player_name] then
  1028. minetest.chat_send_player(player_name, "*** Ignoring it and sticking with "..string.sub(unifieddyes.player_current_dye[player_name], 5)..".")
  1029. else
  1030. minetest.chat_send_player(player_name, "*** Ignoring it.")
  1031. end
  1032. return
  1033. else
  1034. unifieddyes.player_current_dye[player_name] = dye
  1035. unifieddyes.player_selected_dye[player_name] = nil
  1036. minetest.chat_send_player(player_name, "*** Selected "..string.sub(dye, 5).." for the airbrush.")
  1037. return
  1038. end
  1039. else -- assume "Cancel" or Esc.
  1040. unifieddyes.player_selected_dye[player_name] = nil
  1041. return
  1042. end
  1043. else
  1044. local s1 = string.sub(minetest.serialize(fields), 11)
  1045. local s3 = string.sub(s1,1, string.find(s1, '"')-1)
  1046. local inv = player:get_inventory()
  1047. local creative = creative and creative.is_enabled_for(player_name)
  1048. local dye = "dye:"..s3
  1049. if (showall or unifieddyes.palette_has_color[nodepalette.."_"..s3]) and
  1050. (minetest.registered_items[dye] and (creative or inv:contains_item("main", dye))) then
  1051. unifieddyes.player_selected_dye[player_name] = dye
  1052. unifieddyes.show_airbrush_form(player)
  1053. end
  1054. end
  1055. end
  1056. end)
  1057. -- Generate all dyes that are not part of the default minetest_game dyes mod
  1058. for _, h in ipairs(unifieddyes.HUES_EXTENDED) do
  1059. local hue = h[1]
  1060. local r = h[2]
  1061. local g = h[3]
  1062. local b = h[4]
  1063. for v = 0, 6 do
  1064. local val = unifieddyes.VALS_EXTENDED[v+1]
  1065. local factor = 40
  1066. if v > 3 then factor = 75 end
  1067. local r2 = math.max(math.min(r + (4-v)*factor, 255), 0)
  1068. local g2 = math.max(math.min(g + (4-v)*factor, 255), 0)
  1069. local b2 = math.max(math.min(b + (4-v)*factor, 255), 0)
  1070. -- full-sat color
  1071. local desc = hue:gsub("%a", string.upper, 1).." Dye"
  1072. if val ~= "" then
  1073. desc = val:sub(1, -2):gsub("%a", string.upper, 1) .." "..desc
  1074. end
  1075. local color = string.format("%02x", r2)..string.format("%02x", g2)..string.format("%02x", b2)
  1076. if minetest.registered_items["dye:"..val..hue] then
  1077. minetest.override_item("dye:"..val..hue, {
  1078. inventory_image = "unifieddyes_dye.png^[colorize:#"..color..":200",
  1079. })
  1080. else
  1081. if (val..hue) ~= "medium_orange"
  1082. and (val..hue) ~= "light_red" then
  1083. minetest.register_craftitem(":dye:"..val..hue, {
  1084. description = S(desc),
  1085. inventory_image = "unifieddyes_dye.png^[colorize:#"..color..":200",
  1086. groups = { dye=1, not_in_creative_inventory=1 },
  1087. })
  1088. end
  1089. end
  1090. minetest.register_alias("unifieddyes:"..val..hue, "dye:"..val..hue)
  1091. if v > 3 then -- also register the low-sat version
  1092. local pr = 0.299
  1093. local pg = 0.587
  1094. local pb = 0.114
  1095. local p = math.sqrt(r2*r2*pr + g2*g2*pg + b2*b2*pb)
  1096. local r3 = math.floor(p+(r2-p)*0.5)
  1097. local g3 = math.floor(p+(g2-p)*0.5)
  1098. local b3 = math.floor(p+(b2-p)*0.5)
  1099. local color = string.format("%02x", r3)..string.format("%02x", g3)..string.format("%02x", b3)
  1100. minetest.register_craftitem(":dye:"..val..hue.."_s50", {
  1101. description = S(desc.." (low saturation)"),
  1102. inventory_image = "unifieddyes_dye.png^[colorize:#"..color..":200",
  1103. groups = { dye=1, not_in_creative_inventory=1 },
  1104. })
  1105. minetest.register_alias("unifieddyes:"..val..hue.."_s50", "dye:"..val..hue.."_s50")
  1106. end
  1107. end
  1108. end
  1109. -- register the greyscales too :P
  1110. for y = 1, 14 do -- colors 0 and 15 are black and white, default dyes
  1111. if y ~= 4 and y ~= 8 and y~= 11 then -- don't register the three greys, they're done separately.
  1112. local rgb = string.format("%02x", y*17)..string.format("%02x", y*17)..string.format("%02x", y*17)
  1113. local name = "grey_"..y
  1114. local desc = "Grey Dye #"..y
  1115. minetest.register_craftitem(":dye:"..name, {
  1116. description = S(desc),
  1117. inventory_image = "unifieddyes_dye.png^[colorize:#"..rgb..":200",
  1118. groups = { dye=1, not_in_creative_inventory=1 },
  1119. })
  1120. minetest.register_alias("unifieddyes:"..name, "dye:"..name)
  1121. end
  1122. end
  1123. minetest.override_item("dye:grey", {
  1124. inventory_image = "unifieddyes_dye.png^[colorize:#888888:200",
  1125. })
  1126. minetest.override_item("dye:dark_grey", {
  1127. inventory_image = "unifieddyes_dye.png^[colorize:#444444:200",
  1128. })
  1129. minetest.register_craftitem(":dye:light_grey", {
  1130. description = S("Light grey Dye"),
  1131. inventory_image = "unifieddyes_dye.png^[colorize:#cccccc:200",
  1132. groups = { dye=1, not_in_creative_inventory=1 },
  1133. })
  1134. -- build a table of color <-> palette associations to reduce the need for
  1135. -- realtime lookups with getpaletteidx()
  1136. for _, palette in ipairs({"extended", "split", "wallmounted"}) do
  1137. local palette2 = palette
  1138. for i in ipairs(unifieddyes.SATS) do
  1139. local sat = (palette == "wallmounted") and "" or unifieddyes.SATS[i]
  1140. for _, hue in ipairs(unifieddyes.HUES_EXTENDED) do
  1141. for _, val in ipairs(unifieddyes.VALS_EXTENDED) do
  1142. local color = val..hue[1]..sat
  1143. if unifieddyes.getpaletteidx("dye:"..color, palette2) then
  1144. unifieddyes.palette_has_color[palette.."_"..color] = true
  1145. end
  1146. end
  1147. end
  1148. end
  1149. for y = 0, 15 do
  1150. local grey = "grey_"..y
  1151. if y == 0 then grey = "black"
  1152. elseif y == 4 then grey = "dark_grey"
  1153. elseif y == 8 then grey = "grey"
  1154. elseif y == 11 then grey = "light_grey"
  1155. elseif y == 15 then grey = "white"
  1156. end
  1157. if unifieddyes.getpaletteidx("dye:"..grey, palette2) then
  1158. unifieddyes.palette_has_color[palette.."_"..grey] = true
  1159. end
  1160. end
  1161. end
  1162. unifieddyes.palette_has_color["wallmounted_light_red"] = true
  1163. -- crafting!
  1164. unifieddyes.base_color_crafts = {
  1165. { "red", "flowers:rose", nil, nil, nil, nil, 4 },
  1166. { "vermilion", "dye:red", "dye:orange", nil, nil, nil, 3 },
  1167. { "orange", "flowers:tulip", nil, nil, nil, nil, 4 },
  1168. { "orange", "dye:red", "dye:yellow", nil, nil, nil, 2 },
  1169. { "amber", "dye:orange", "dye:yellow", nil, nil, nil, 2 },
  1170. { "yellow", "flowers:dandelion_yellow", nil, nil, nil, nil, 4 },
  1171. { "lime", "dye:yellow", "dye:chartreuse", nil, nil, nil, 2 },
  1172. { "lime", "dye:yellow", "dye:yellow", "dye:green", nil, nil, 3 },
  1173. { "chartreuse", "dye:yellow", "dye:green", nil, nil, nil, 2 },
  1174. { "harlequin", "dye:chartreuse", "dye:green", nil, nil, nil, 2 },
  1175. { "harlequin", "dye:yellow", "dye:green", "dye:green", nil, nil, 3 },
  1176. { "green", "default:cactus", nil, nil, nil, nil, 4 },
  1177. { "green", "dye:yellow", "dye:blue", nil, nil, nil, 2 },
  1178. { "malachite", "dye:green", "dye:spring", nil, nil, nil, 2 },
  1179. { "malachite", "dye:green", "dye:green", "dye:cyan", nil, nil, 3 },
  1180. { "malachite", "dye:green", "dye:green", "dye:green", "dye:blue", nil, 4 },
  1181. { "spring", "dye:green", "dye:cyan", nil, nil, nil, 2 },
  1182. { "spring", "dye:green", "dye:green", "dye:blue", nil, nil, 3 },
  1183. { "turquoise", "dye:spring", "dye:cyan", nil, nil, nil, 2 },
  1184. { "turquoise", "dye:green", "dye:cyan", "dye:cyan", nil, nil, 3 },
  1185. { "turquoise", "dye:green", "dye:green", "dye:green", "dye:blue", "dye:blue", 5 },
  1186. { "cyan", "dye:green", "dye:blue", nil, nil, nil, 2 },
  1187. { "cerulean", "dye:cyan", "dye:azure", nil, nil, nil, 2 },
  1188. { "cerulean", "dye:cyan", "dye:cyan", "dye:blue", nil, nil, 3 },
  1189. { "cerulean", "dye:green", "dye:green", "dye:blue", "dye:blue", "dye:blue", 5 },
  1190. { "azure", "dye:cyan", "dye:blue", nil, nil, nil, 2 },
  1191. { "azure", "dye:green", "dye:blue", "dye:blue", nil, nil, 3 },
  1192. { "sapphire", "dye:azure", "dye:blue", nil, nil, nil, 2 },
  1193. { "sapphire", "dye:cyan", "dye:blue", "dye:blue", nil, nil, 3 },
  1194. { "sapphire", "dye:green", "dye:blue", "dye:blue", "dye:blue", nil, 4 },
  1195. { "blue", "flowers:geranium", nil, nil, nil, nil, 4 },
  1196. { "indigo", "dye:blue", "dye:violet", nil, nil, nil, 2 },
  1197. { "violet", "flowers:viola", nil, nil, nil, nil, 4 },
  1198. { "violet", "dye:blue", "dye:magenta", nil, nil, nil, 2 },
  1199. { "mulberry", "dye:violet", "dye:magenta", nil, nil, nil, 2 },
  1200. { "mulberry", "dye:violet", "dye:blue", "dye:red", nil, nil, 3 },
  1201. { "magenta", "dye:blue", "dye:red", nil, nil, nil, 2 },
  1202. { "fuchsia", "dye:magenta", "dye:rose", nil, nil, nil, 2 },
  1203. { "fuchsia", "dye:blue", "dye:red", "dye:rose", nil, nil, 3 },
  1204. { "fuchsia", "dye:red", "dye:violet", nil, nil, nil, 2 },
  1205. { "rose", "dye:magenta", "dye:red", nil, nil, nil, 2 },
  1206. { "rose", "dye:red", "dye:red", "dye:blue", nil, nil, 3 },
  1207. { "crimson", "dye:rose", "dye:red", nil, nil, nil, 2 },
  1208. { "crimson", "dye:magenta", "dye:red", "dye:red", nil, nil, 3 },
  1209. { "crimson", "dye:red", "dye:red", "dye:red", "dye:blue", nil, 4 },
  1210. { "black", "default:coal_lump", nil, nil, nil, nil, 4 },
  1211. { "white", "flowers:dandelion_white", nil, nil, nil, nil, 4 },
  1212. }
  1213. unifieddyes.shade_crafts = {
  1214. { "faint_", "", "dye:white", "dye:white", "dye:white", 4 },
  1215. { "pastel_", "", "dye:white", "dye:white", nil, 3 },
  1216. { "light_", "", "dye:white", nil, nil, 2 },
  1217. { "bright_", "", "color", "dye:white", nil, 3 },
  1218. { "", "_s50", "dye:light_grey", nil, nil, 2 },
  1219. { "", "_s50", "dye:black", "dye:white", "dye:white", 3 },
  1220. { "medium_", "", "dye:black", nil, nil, 2 },
  1221. { "medium_", "_s50", "dye:grey", nil, nil, 2 },
  1222. { "medium_", "_s50", "dye:black", "dye:white", nil, 3 },
  1223. { "dark_", "", "dye:black", "dye:black", nil, 3 },
  1224. { "dark_", "_s50", "dye:dark_grey", nil, nil, 2 },
  1225. { "dark_", "_s50", "dye:black", "dye:black", "dye:white", 4 },
  1226. }
  1227. for _,i in ipairs(unifieddyes.base_color_crafts) do
  1228. local color = i[1]
  1229. local yield = i[7]
  1230. minetest.register_craft( {
  1231. type = "shapeless",
  1232. output = "dye:"..color.." "..yield,
  1233. recipe = {
  1234. i[2],
  1235. i[3],
  1236. i[4],
  1237. i[5],
  1238. i[6],
  1239. },
  1240. })
  1241. for _,j in ipairs(unifieddyes.shade_crafts) do
  1242. local firstdye = j[3]
  1243. if firstdye == "color" then firstdye = "dye:"..color end
  1244. -- ignore black, white, anything containing the word "grey"
  1245. if color ~= "black" and color ~= "white" and not string.find(color, "grey") then
  1246. minetest.register_craft( {
  1247. type = "shapeless",
  1248. output = "dye:"..j[1]..color..j[2].." "..j[6],
  1249. recipe = {
  1250. "dye:"..color,
  1251. firstdye,
  1252. j[4],
  1253. j[5]
  1254. },
  1255. })
  1256. end
  1257. end
  1258. end
  1259. -- greys
  1260. unifieddyes.greymixes = {
  1261. { 1, "dye:black", "dye:black", "dye:black", "dye:dark_grey", 4 },
  1262. { 2, "dye:black", "dye:black", "dye:dark_grey", nil, 3 },
  1263. { 3, "dye:black", "dye:dark_grey", nil, nil, 2 },
  1264. { 4, "dye:white", "dye:black", "dye:black", nil, 3 },
  1265. { 5, "dye:dark_grey", "dye:dark_grey", "dye:grey", nil, 3 },
  1266. { 6, "dye:dark_grey", "dye:grey", nil, nil, 2 },
  1267. { 7, "dye:dark_grey", "dye:grey", "dye:grey", nil, 3 },
  1268. { 8, "dye:white", "dye:black", nil, nil, 2 },
  1269. { 9, "dye:grey", "dye:grey", "dye:light_grey", nil, 3 },
  1270. { 10, "dye:grey", "dye:light_grey", "dye:light_grey", nil, 3 },
  1271. { 11, "dye:white", "dye:white", "dye:black", nil, 3 },
  1272. { 12, "dye:light_grey", "dye:light_grey", "dye:white", nil, 3 },
  1273. { 13, "dye:light_grey", "dye:white", nil, nil, 2 },
  1274. { 14, "dye:white", "dye:white", "dye:light_grey", nil, 3 },
  1275. }
  1276. for _, i in ipairs(unifieddyes.greymixes) do
  1277. local shade = i[1]
  1278. local dye1 = i[2]
  1279. local dye2 = i[3]
  1280. local dye3 = i[4]
  1281. local dye4 = i[5]
  1282. local yield = i[6]
  1283. local color = "grey_"..shade
  1284. if shade == 4 then
  1285. color = "dark_grey"
  1286. elseif shade == 8 then
  1287. color = "grey"
  1288. elseif shade == 11 then
  1289. color = "light_grey"
  1290. end
  1291. minetest.register_craft( {
  1292. type = "shapeless",
  1293. output = "dye:"..color.." "..yield,
  1294. recipe = {
  1295. dye1,
  1296. dye2,
  1297. dye3,
  1298. dye4,
  1299. },
  1300. })
  1301. end
  1302. -- we can't make dark orange anymore because brown/medium orange conflicts
  1303. minetest.register_craft( {
  1304. type = "shapeless",
  1305. output = "dye:dark_orange",
  1306. recipe = {
  1307. "dye:brown",
  1308. "dye:brown"
  1309. },
  1310. })
  1311. -- aliases
  1312. minetest.register_alias("dye:light_red", "dye:pink")
  1313. minetest.register_alias("dye:medium_orange", "dye:brown")
  1314. minetest.register_alias("unifieddyes:black", "dye:black")
  1315. minetest.register_alias("unifieddyes:dark_grey", "dye:dark_grey")
  1316. minetest.register_alias("unifieddyes:grey", "dye:grey")
  1317. minetest.register_alias("unifieddyes:light_grey", "dye:light_grey")
  1318. minetest.register_alias("unifieddyes:white", "dye:white")
  1319. minetest.register_alias("unifieddyes:grey_0", "dye:black")
  1320. minetest.register_alias("unifieddyes:grey_4", "dye:dark_grey")
  1321. minetest.register_alias("unifieddyes:grey_8", "dye:grey")
  1322. minetest.register_alias("unifieddyes:grey_11", "dye:light_grey")
  1323. minetest.register_alias("unifieddyes:grey_15", "dye:white")
  1324. minetest.register_alias("unifieddyes:white_paint", "dye:white")
  1325. minetest.register_alias("unifieddyes:titanium_dioxide", "dye:white")
  1326. minetest.register_alias("unifieddyes:lightgrey_paint", "dye:light_grey")
  1327. minetest.register_alias("unifieddyes:grey_paint", "dye:grey")
  1328. minetest.register_alias("unifieddyes:darkgrey_paint", "dye:dark_grey")
  1329. minetest.register_alias("unifieddyes:carbon_black", "dye:black")
  1330. minetest.register_alias("unifieddyes:brown", "dye:brown")
  1331. print(S("[UnifiedDyes] Loaded!"))