PyuTest.make_node("pyutest_blocks:sponge", "Sponge", {
  oddly_breakable_by_hand = PyuTest.BLOCK_FAST
}, { "pyutest-sponge.png" })

PyuTest.make_node("pyutest_blocks:light", "Light", {
  light = 1,
  dig_immediate = 1,
  oddly_breakable_by_hand = PyuTest.BLOCK_FAST
}, {
  "pyutest-light.png"
}, {
  drawtype = "torchlike",
  walkable = false,
  paramtype = "light",
  sunlight_propagates = true,
  light_source = minetest.LIGHT_MAX,
  floodable = true
})

PyuTest.make_node("pyutest_blocks:torch", "Torch", {
  dig_immediate = 1,
  light = 1,
  attached_node = 1,
  oddly_breakable_by_hand = PyuTest.BLOCK_FAST
}, {
  "pyutest-torch.png",
  "pyutest-torch.png^[transform6",
  "pyutest-torch.png^[transform1"
}, {
  light_source = minetest.LIGHT_MAX,
  walkable = false,
  drawtype = "torchlike",
  paramtype = "light",
  inventory_image = "pyutest-torch.png",
  paramtype2 = "wallmounted",
  floodable = true
})

PyuTest.make_node("pyutest_blocks:glass", "Glass", {
  cracky = PyuTest.BLOCK_NORMAL
}, { "pyutest-glass.png" }, {
  drawtype = "glasslike_framed",
  paramtype = "light",
  sunlight_propagates = true
})

-- FIXME: This has been in the game for months, implement it already!
PyuTest.make_node("pyutest_blocks:trapdoor", "Trapdoor", {
  choppy = PyuTest.BLOCK_NORMAL,
  flammable = 1
}, { "pyutest-trapdoor.png" }, {
  drawtype = "nodebox",
  paramtype = "light",
  sunlight_propagates = true,
  node_box = {
    type = "fixed",
    fixed = { -0.5, -0.5, -0.5, 0.5, -0.15, 0.5 }
  }
})

PyuTest.make_node("pyutest_blocks:contagious_acid", "Contagious Acid", {
  crumbly = PyuTest.BLOCK_NORMAL,
  solid_node = 1
}, { "pyutest-acid.png" }, {})

PyuTest.make_node("pyutest_blocks:fire", "Fire", {
  dig_immediate = 1,
  oddly_breakable_by_hand = PyuTest.BLOCK_FAST
}, { "pyutest-fire.png" }, {
  drawtype = "firelike",
  walkable = false,
  buildable_to = true,
  paramtype = "light",
  sunlight_propagates = true,
  damage_per_second = 2,
  light_source = 8,
  drop = "pyutest_tools:ash 4"
})

PyuTest.make_node("pyutest_blocks:tnt", "TNT", {
  dig_immediate = 1,
  oddly_breakable_by_hand = PyuTest.BLOCK_FAST
}, {
  "pyutest-tnt-top-bottom.png",
  "pyutest-tnt-top-bottom.png",
  "pyutest-tnt-side.png" -- Affects all other sides
}, {
  on_rightclick = function(pos, node, clicker)
    minetest.after(3, function()
      if minetest.get_node(pos).name ~= "pyutest_blocks:tnt" then
        return
      end
      PyuTest.create_explosion(pos, 3, true, 12)
    end)
  end,

  __on_electricity_activated = function(pos, node, sender_pos)
    minetest.after(3, function()
      if minetest.get_node(pos).name ~= "pyutest_blocks:tnt" then
        return
      end
      PyuTest.create_explosion(pos, 3, true, 12)
    end)
  end,
})

PyuTest.make_node("pyutest_blocks:crate", "Crate", {
  choppy = PyuTest.BLOCK_NORMAL
}, { "pyutest-crate.png" }, {
  on_construct = function(pos)
    local meta = minetest.get_meta(pos)
    local inventory = meta:get_inventory()
    inventory:set_size("main", 8 * 4)
  end,

  can_dig = function(pos, player)
    local meta = minetest.get_meta(pos)
    local inventory = meta:get_inventory()
    local empty = inventory:is_empty("main")

    if not empty then
      minetest.chat_send_player(player:get_player_name(), "Cannot destroy crate, it's not empty!")
    end

    return empty
  end,

  on_rightclick = function(pos, node, clicker)
    local spos = string.format("%d,%d,%d", pos.x, pos.y, pos.z)
    local formspec =
    "size[8,9]" ..
    "list[nodemeta:" .. spos .. ";main;0,0;8,4;]" ..
    "list[current_player;main;0,5;8,4;]" ..
    "listring[nodemeta:" .. spos .. ";main]" ..
    "listring[current_player;main]"
    minetest.show_formspec(clicker:get_player_name(), string.format("pyutest_blocks:crate_%d_%d_%d", pos.x, pos.y, pos.z),
    formspec)
    minetest.sound_play({ name = "crate_open", gain = 1 }, { pos = pos })
  end
})

PyuTest.make_node("pyutest_blocks:workbench", "Workbench", {
  choppy = PyuTest.BLOCK_NORMAL
}, {
  "pyutest-workbench-top.png",
  "pyutest-workbench-bottom.png",
  "pyutest-workbench-sides.png"
}, {
  on_rightclick = function(pos, node, clicker)
    minetest.show_formspec(clicker:get_player_name(), "pyutest_blocks:workbench", table.concat({
      "size[8,9]",
      "list[current_player;craft;2.5,1;3,3;]",
      "list[current_player;main;0,5;8,4;]"
    }))
  end
})

PyuTest.make_node("pyutest_blocks:ladder", "Ladder", {
  dig_immediate = 1
}, { "pyutest-ladder.png" }, {
  drawtype = "signlike",
  paramtype = "light",
  walkable = false,
  climbable = true,
  buildable_to = true,
  sunlight_propagates = true,
  paramtype2 = "wallmounted",
  selection_box = {
    type = "wallmounted"
  },
  inventory_image = "pyutest-ladder.png"
})

PyuTest.make_node("pyutest_blocks:magma", "Magma", {
  cracky = PyuTest.BLOCK_FAST
}, { "pyutest-magma.png" }, {
  paramtype = "light",
  light_source = 11,
  damage_per_second = 3
})

minetest.register_abm({
  label = "Sponge Loop",
  nodenames = { "pyutest_blocks:sponge" },
  neighbors = { "group:liquid" },
  interval = 0,
  chance = 1,
  action = function(pos)
    local range = 4

    local function replace(npos)
      local node = minetest.get_node(npos)
      if node.name == "air" then return end
      local def = minetest.registered_nodes[node.name]

      if def.groups["liquid"] == 1 then
        minetest.remove_node(npos)
      end
    end

    PyuTest.dorange(pos, range, function(p)
      replace(p)
    end)
  end
})

minetest.register_abm({
  label = "Contagious Acid Spread",
  nodenames = { "group:acid_vulnerable" },
  neighbors = { "pyutest_blocks:contagious_acid" },
  interval = 2.4,
  chance = 2.2,
  catchup = true,
  action = function(pos)
    minetest.set_node(pos, {
      name = "pyutest_blocks:contagious_acid"
    })
  end
})

minetest.register_abm({
  label = "Fire Spread",
  nodenames = { "group:flammable" },
  neighbors = { "pyutest_blocks:fire" },
  interval = 1,
  chance = 4,
  action = function(pos)
    minetest.set_node(pos, {
      name = "pyutest_blocks:fire"
    })
  end
})
