summaryrefslogtreecommitdiff
path: root/factory/init.lua
blob: 4c976625fe80c8167a0995b4a071d1653c7cb258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
factory={
  crafts={},
  empty={item=ItemStack(nil),time=0}
}

-- Settings
dofile(minetest.get_modpath("factory").."/settings.txt")

-- This below is the Crafter mod by the legend MasterGollum

function factory.register_craft(craft)
  assert(craft.type ~= nil and craft.recipe ~= nil and craft.output ~= nil,
    "Invalid craft definition, it must have type, recipe and output")
  assert(type(craft.recipe)=="table" and type(craft.recipe[1])=="table","'recipe' must be a bidimensional table")
  minetest.log("verbose","registerCraft ("..craft.type..", output="..craft.output.." recipe="..dump(craft.recipe))
  craft._h=#craft.recipe
  craft._w=#craft.recipe[1]
  -- TODO check that all the arrays have the same length...
  factory.crafts[#factory.crafts+1]=craft
end

function factory.get_craft_result(data)
  assert(data.method ~= nil and data.items ~= nil, "Invalid call, method and items must be provided")
  local w = 1
  if data.width ~= nil and data.width>0 then
    w=data.width
  end
  local r=nil
  for zz,craft in ipairs(factory.crafts) do
    r=factory._check_craft(data,w,craft)
    if r ~= nil then
        if factory.debug then
          print("Craft found, returning "..dump(r.item))
        end
      return r
    end
  end
  return factory.empty
end

function factory._check_craft(data,w,c)
  if c.type == data.method then
    -- Here we go..
    for i=1,w-c._h+1 do
      for j=1,w-c._w+1 do
        local p=(i-1)*w+j
        if factory.debug then
          print("Checking data.items["..dump(i).."]["..dump(j).."]("..dump(p)..")="..dump(data.items[p]:get_name()).." vs craft.recipe[1][1]="..dump(c.recipe[1][1]))
        end
        if data.items[p]:get_name() == c.recipe[1][1] then
          for m=1,c._h do
            for n=1,c._w do
              local q=(i+m-1-1)*w+j+n-1
              if factory.debug then
                print("  Checking data.items["..dump(i+m-1).."]["..dump(j+n-1).."]("..dump(q)..")="..dump(data.items[q]:get_name())..
                " vs craft.recipe["..dump(m).."]["..dump(n).."]="..dump(c.recipe[m][n]))
              end
              if c.recipe[m][n] ~= data.items[q]:get_name() then
                return nil
              end
            end
          end
          -- found! we still must check that is not any other stuff outside the limits of the recipe sizes...
          -- Checking at right of the matching square
          for m=i-c._h+1+1,w do
            for n=j+c._w,w do
              local q=(m-1)*w+n
              if factory.debug then
                print("  Checking right data.items["..dump(m).."]["..dump(n).."]("..dump(q)..")="..dump(data.items[q]:get_name()))
              end
              if data.items[q]:get_name() ~= "" then
                return nil
              end
            end
          end
          -- Checking at left of the matching square (the first row has been already scanned)
          for m=i-c._h+1+1+1,w do
            for n=1,j-1 do
              local q=(m-1)*w+n
              if factory.debug then
                print("  Checking left data.items["..dump(m).."]["..dump(n).."]("..dump(q)..")="..dump(data.items[q]:get_name()))
              end
              if data.items[q]:get_name() ~= "" then
                return nil
              end
            end
          end
          -- Checking at bottom of the matching square
          for m=i+c._h,w do
            for n=j,j+c._w do
              local q=(m-1)*w+n
              if factory.debug then
                print("  Checking bottom data.items["..dump(m).."]["..dump(n).."]("..dump(q)..")="..dump(data.items[q]:get_name()))
              end
              if data.items[q]:get_name() ~= "" then
                return nil
              end
            end
          end
          if factory.debug then
            print("Craft found! "..c.output)
          end
          return {item=ItemStack(c.output),time=1}
        elseif data.items[p] ~= nil and data.items[p]:get_name() ~= "" then
          if factory.debug then
            print("Invalid data item "..dump(data.items[p]:get_name()))
          end
          return nil
        end
      end
    end
  end
end

-- GUI related stuff
factory_gui_bg = "bgcolor[#080808BB;true]"
factory_gui_bg_img = "background[5,5;1,1;gui_factoryformbg.png;true]"
factory_gui_slots = "listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]"

function factory.get_hotbar_bg(x,y)
	local out = ""
	for i=0,7,1 do
		out = out .."image["..x+i..","..y..";1,1;gui_hb_bg.png]"
	end
	return out
end

function factory.swap_node(pos,name)
	local node = minetest.get_node(pos)
	if node.name == name then
		return
	end
	node.name = name
	minetest.swap_node(pos,node)
end

dofile(minetest.get_modpath("factory").."/nodes.lua")
dofile(minetest.get_modpath("factory").."/craftitems.lua")
dofile(minetest.get_modpath("factory").."/crafting.lua")

dofile(minetest.get_modpath("factory").."/ind_furnace.lua")
dofile(minetest.get_modpath("factory").."/ind_squeezer.lua")