writerecipegv.lua 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- DOT (Graphviz) export
  2. local write_item_gv = function(file, item, node_lines, nodes_written)
  3. local itemtag = item:gsub(":", "_"):gsub(",","_")
  4. if nodes_written[itemtag] then
  5. return itemtag
  6. end
  7. local color
  8. local mod
  9. local colon_index = string.find(item, ":")
  10. if colon_index == nil then
  11. item = "group:" .. item
  12. colon_index = 6
  13. color = "#C0C0C0"
  14. mod = "group"
  15. else
  16. mod = string.sub(item, 1, colon_index-1)
  17. color = simplecrafting_lib.get_key_color(mod)
  18. end
  19. table.insert(node_lines, '\t'..itemtag..' [label="'..item..'",style="filled",fillcolor="'..color..'",shape="box"]')
  20. nodes_written[itemtag] = true
  21. return itemtag
  22. end
  23. local write_recipe_gv = function(file, craft_type, id, recipe, nodes_written, node_lines, recipe_lines)
  24. local recipe_id = "recipe_"..craft_type.."_"..tostring(id)
  25. local extra_data = {}
  26. local has_extra_data = false
  27. for k, v in pairs(recipe) do
  28. if type(v) == "function" then
  29. minetest.log("error", "[simplecrafting_lib] recipe write: " .. key .. "'s value is a function")
  30. elseif k ~= "output" and k ~= "input" and k ~= "returns" then
  31. extra_data[k] = v
  32. has_extra_data = true
  33. end
  34. end
  35. table.insert(recipe_lines, '\t'..recipe_id..' [label="'..craft_type..'",style="filled",fillcolor="#FFCC00",shape="diamond"]') -- recipe node
  36. if has_extra_data then
  37. --TODO
  38. --write_data_gv(file, "recipe_extra_data", minetest.serialize(extra_data))
  39. end
  40. if recipe.input then
  41. for initem, incount in pairs(recipe.input) do
  42. local itemtag = write_item_gv(file, initem, node_lines, nodes_written)
  43. table.insert(recipe_lines, '\t'..itemtag..' -> '..recipe_id..' [label="'..tostring(incount)..'"]')
  44. end
  45. end
  46. if recipe.returns then
  47. for returnitem, returncount in pairs(recipe.returns) do
  48. local itemtag = write_item_gv(file, returnitem, node_lines, nodes_written)
  49. table.insert(recipe_lines, '\t'..recipe_id..' -> '..itemtag..' [label="'..tostring(returncount)..'",arrowhead="onormal",color="#888888"]')
  50. end
  51. end
  52. if recipe.output then
  53. local output = ItemStack(recipe.output)
  54. local outitem = output:get_name()
  55. local itemtag = write_item_gv(file, outitem, node_lines, nodes_written)
  56. table.insert(recipe_lines, '\t'..recipe_id..' -> '..itemtag..' [label="'..tostring(output:get_count())..'"]')
  57. end
  58. table.insert(recipe_lines, "") -- blank line between recipes
  59. end
  60. return function(file, recipes, recipe_filter, show_unused)
  61. for craft_type, recipe_list in pairs(recipes) do
  62. local items_written = {} -- tracks which items have already been written
  63. local node_lines = {} -- gather up all the unique item node definitions
  64. local recipe_lines = {} -- recipe nodes and recipes bundled together
  65. file:write('digraph ' .. craft_type .. '{\n')
  66. for id, recipe in pairs(recipe_list.recipes) do
  67. if recipe_filter.filter(recipe) then
  68. write_recipe_gv(file, craft_type, id, recipe, items_written, node_lines, recipe_lines)
  69. end
  70. end
  71. table.sort(node_lines) -- don't sort recipe_lines, those have an actual order to them
  72. file:write(table.concat(node_lines, "\n"))
  73. file:write("\n\n")
  74. file:write(table.concat(recipe_lines, "\n"))
  75. file:write('}\n')
  76. end
  77. simplecrafting_lib.save_key_colors()
  78. file:flush()
  79. file:close()
  80. end