
local S = minetest.get_translator("chemistry")

local chemical_boom = {
	name = "chemical_boom",
	--description = "explosion caused by chemical reactions",
	radius = 2,
	tiles = {
		side = "invisible.png",
		top = "invisible.png",
		bottom = "invisible.png",
		burning = "invisible.png"
	},
}

local c4_boom = {
	name = "c4_boom",
	--description = "explosion caused by chemical reactions",
	radius = 3,
	tiles = {
		side = "invisible.png",
		top = "invisible.png",
		bottom = "invisible.png",
		burning = "invisible.png"
	},
}

tnt.register_tnt(chemical_boom)

tnt.register_tnt(c4_boom)


minetest.register_node("chemistry:white_phosphorus", {
	description = S("White Phosphorus"),
	tiles = {"white_phosphorus.png"},
	is_ground_content = true,
	groups = {cracky = 3},
	sounds = default.node_sound_stone_defaults(),
})

if chemistry.allow_explosive_nodes then

minetest.register_abm({
		label = "chemistry: phosphorus ignition",
		nodenames = {"air"}, -- checking for ignition sources because there will be fewer than there are gas nodes
		neighbors = {"chemistry:white_phosphorus"},
		interval = 5.0,
		chance = 6,
		catch_up = true,
		action = function(pos, node)
			if minetest.find_node_near(pos, 1, "air") then
				minetest.swap_node(pos, {name="fire:basic_flame"})
				if math.random() < 0.0001 then
					minetest.set_node(pos, {name="air"})
				end
			end	
		end,
	})

minetest.register_abm({
		label = "chemistry: phosphorus ignition2",
		nodenames = {"chemistry:white_phosphorus"}, -- checking for ignition sources because there will be fewer than there are gas nodes
		neighbors = {"group:fire"},
		interval = 1.0,
		chance = 12,
		catch_up = true,
		action = function(pos, node)
			if minetest.find_node_near(pos, 1, "air") then
				tnt.boom(pos, chemical_boom)
				if math.random() < 0.5 then
					minetest.set_node(pos, {name="fire:basic_flame"})
				end
			end	
		end,
	})

local red_trans = {name ="chemistry:white_phosphorus"}
minetest.register_abm({
		label = "chemistry: phosphorus transform",
		nodenames = {"chemistry:red_phosphorus"}, -- checking for ignition sources because there will be fewer than there are gas nodes
		neighbors = {"group:fire"},
		interval = 5.0,
		chance = 6,
		catch_up = true,
		action = function(pos, node)
			if minetest.find_node_near(pos, 1, "group:fire") then
				minetest.swap_node(pos, red_trans)
			end	
		end,
	})

minetest.register_node("chemistry:will_explode", {
	description = S("WILL EXPLODE!!!!"),
	inventory_image = "no_texture_airlike.png",
	wield_image = "no_texture_airlike.png",
	drawtype = "airlike",
	paramtype = "none",
	sunlight_propagates = true,
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = false,
	air_equivalent = true,
	drop = "",
	groups = {not_in_creative_inventory=1},
	node_placement_prediction = "",
   on_timer = function(pos)
		tnt.boom(pos, chemical_boom)
   end,
   on_construct = function(pos)
	   minetest.get_node_timer(pos):start(math.random(0.1, 0.1))
   end,
	on_blast = function() end,
})

minetest.register_node("chemistry:c4_explode", {
	inventory_image = "no_texture_airlike.png",
	wield_image = "no_texture_airlike.png",
	drawtype = "airlike",
	paramtype = "none",
	sunlight_propagates = true,
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = false,
	air_equivalent = true,
	drop = "",
	groups = {not_in_creative_inventory=1},
	node_placement_prediction = "",
   on_timer = function(pos)
		tnt.boom(pos, c4_boom)
   end,
   on_construct = function(pos)
	   minetest.get_node_timer(pos):start(math.random(0.1, 0.1))
   end,
	on_blast = function() end,
})

minetest.register_node("chemistry:red_phosphorus", {
	description = S("Red Phosphorus"),
	tiles = {"red_phosphorus.png"},
	groups = {crumbly = 3, falling_node = 1},
	on_blast = function(pos)
		minetest.after(0.1, function()
      	minetest.swap_node(pos, {name="chemistry:will_explode"})
		   minetest.registered_nodes["chemistry:will_explode"].on_construct(pos)
      end)
	end,
	sounds = default.node_sound_sand_defaults(),
})

minetest.register_node("chemistry:purple_phosphorus", {
	description = S("Purple Phosphorus"),
	tiles = {"purple_phosphorus.png"},
	groups = {crumbly = 3, falling_node = 1},
	on_blast = function(pos)
		minetest.after(0.1, function()
      	minetest.swap_node(pos, {name="chemistry:will_explode"})
		   minetest.registered_nodes["chemistry:will_explode"].on_construct(pos)
      end)
	end,
	sounds = default.node_sound_sand_defaults(),
})

minetest.register_node("chemistry:nitroglycerin", {
	description = S("Nitroglycerin"),
	drawtype = "liquid",
	waving = 3,
	tiles = {
		{
			name = "oil_source_animated.png^[opacity:200",
			backface_culling = false,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 2.0,
			},
		},
		{
			name = "oil_source_animated.png^[opacity:200",
			backface_culling = true,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 2.0,
			},
		},
	},
	use_texture_alpha = "blend",
	paramtype = "light",
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	drop = "",
	on_blast = function(pos, intensity)
		minetest.after(0.1, function()
      	minetest.swap_node(pos, {name="chemistry:will_explode"})
		   minetest.registered_nodes["chemistry:will_explode"].on_construct(pos)
      end)
	end,
	on_burn = function(pos)
		tnt.boom(pos, chemical_boom)
	end,
	drowning = 1,
	liquidtype = "source",
	liquid_alternative_flowing = "chemistry:nitroglycerin_flowing",
	liquid_alternative_source = "chemistry:nitroglycerin",
	liquid_viscosity = 1,
	post_effect_color ={a = 100, r = 250, g = 250, b = 250},
	groups = {liquid = 3, cools_lava = 1, flammable = 10},
	sounds = chemistry.node_sound_oil(),
})

minetest.register_node("chemistry:nitroglycerin_flowing", {
	description = S("Flowing Nitroglycerin"),
	drawtype = "flowingliquid",
	waving = 3,
	tiles = {"oil.png"},
	special_tiles = {
		{
			name = "oil_flowing_animated.png^[opacity:200",
			backface_culling = false,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 0.5,
			},
		},
		{
			name = "oil_flowing_animated.png^[opacity:200",
			backface_culling = true,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 0.5,
			},
		},
	},
	use_texture_alpha = "blend",
	paramtype = "light",
	paramtype2 = "flowingliquid",
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	drop = "",
	on_blast = function(pos, intensity)
		minetest.after(0.1, function()
      	minetest.swap_node(pos, {name="chemistry:will_explode"})
		   minetest.registered_nodes["chemistry:will_explode"].on_construct(pos)
      end)
	end,
	on_burn = function(pos)
		tnt.boom(pos, chemical_boom)
	end,
	drowning = 1,
	liquidtype = "flowing",
	liquid_alternative_flowing = "chemistry:nitroglycerin_flowing",
	liquid_alternative_source = "chemistry:nitroglycerin",
	liquid_viscosity = 1,
	liquid_range = 5,
	post_effect_color = {a = 100, r = 250, g = 250, b = 250},
	groups = {liquid = 3, not_in_creative_inventory = 1,
		cools_lava = 1, flammable = 10},
	sounds = chemistry.node_sound_oil(),
})

	bucket.register_liquid(
		"chemistry:nitroglycerin",
		"chemistry:nitroglycerin_flowing",
		"chemistry:nitroglycerin_bucket",
		"oil_bucket.png",
		S("Nitroglycerin Bucket"),
		{tool = 1, explosive = 1}
	)

if minetest.get_modpath("mesecons") then
		minetest.register_node("chemistry:c4", {
			description = S("C4 Bomb"),
			tiles = {"c4_base.png", "c4_base.png", "c4_side.png"},
			is_ground_content = false,
			groups = {dig_immediate = 2, mesecon = 2, flammable = 5},
			sounds = default.node_sound_wood_defaults(),
			after_place_node = function(pos, placer)
				if placer and placer:is_player() then
					local meta = minetest.get_meta(pos)
					meta:set_string("owner", placer:get_player_name())
				end
			end,
			on_blast = function(pos)
		      minetest.after(0.1, function()
      	      minetest.swap_node(pos, {name="chemistry:c4_explode"})
		         minetest.registered_nodes["chemistry:c4_explode"].on_construct(pos)
            end)
			end,
			mesecons = {effector =
				{action_on =
					function(pos)
				      minetest.sound_play(
					      "c4_beep",
					      {pos = pos, max_hear_distance = 18, gain = 0.5}
                   )
      	          minetest.swap_node(pos, {name="chemistry:c4_lit"})
		             minetest.registered_nodes["chemistry:c4_lit"].on_construct(pos)
					end
				}
			},
			on_burn = function(pos)
				minetest.after(0.5, function()
					tnt.boom(pos, c4_boom)
				end)
			end,
		})

		minetest.register_node("chemistry:c4_lit", {
			tiles = {"c4_base.png", "c4_base.png", "c4_side.png"},
			is_ground_content = false,
		   groups = {not_in_creative_inventory = 1},
			sounds = default.node_sound_wood_defaults(),
         on_timer = function(pos)
	      	tnt.boom(pos, c4_boom)
         end,
         on_construct = function(pos)
	         minetest.get_node_timer(pos):start(5)
         end,
	      on_blast = function() end,
       })

minetest.register_craft({
	output = 'chemistry:c4 5',
	recipe = {
		{'chemistry:plastic_block', 'default:mese_crystal', 'chemistry:plastic_block'},
		{'default:mese_crystal', 'chemistry:nitroglycerin_bucket', 'default:mese_crystal'},
		{'chemistry:plastic_block', 'default:mese_crystal', 'chemistry:plastic_block'},
	},
   replacements = {{'chemistry:nitroglycerin_bucket', 'bucket:bucket_empty'}}
})
end

minetest.register_craft({
	output = 'chemistry:nitroglycerin_bucket',
	recipe = {
		{'chemistry:hno_acid_bucket', 'chemistry:glycerin_bucket'},
	},
   replacements = {{'chemistry:hno_acid_bucket', 'bucket:bucket_empty'}}
})

elseif not chemistry.allow_explosive_nodes then

minetest.register_node("chemistry:red_phosphorus", {
	description = S("Red Phosphorus"),
	tiles = {"red_phosphorus.png"},
	groups = {crumbly = 3, falling_node = 1},
	sounds = default.node_sound_sand_defaults(),
})

minetest.register_node("chemistry:purple_phosphorus", {
	description = S("Purple Phosphorus"),
	tiles = {"purple_phosphorus.png"},
	groups = {crumbly = 3, falling_node = 1},
	sounds = default.node_sound_sand_defaults(),
})

end


minetest.register_node("chemistry:glycerin", {
	description = S("Glycerin"),
	drawtype = "liquid",
	waving = 3,
	tiles = {
		{
			name = "liquid_source_animated.png^[opacity:190",
			backface_culling = false,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 2.0,
			},
		},
		{
			name = "liquid_source_animated.png^[opacity:190",
			backface_culling = true,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 2.0,
			},
		},
	},
	use_texture_alpha = "blend",
	paramtype = "light",
	walkable = false,
	pointable = false,
	diggable = false,
   damage_per_second = 0,
	buildable_to = true,
	is_ground_content = false,
	drop = "",
	drowning = 1,
	liquidtype = "source",
	liquid_alternative_flowing = "chemistry:glycerin_flowing",
	liquid_alternative_source = "chemistry:glycerin",
	liquid_viscosity = 2,
	post_effect_color = {a = 122, r = 122, g = 122, b = 122},
	groups = {liquid = 3, cools_lava = 1, glycerin = 1},
	sounds = chemistry.node_sound_oil(),
})

minetest.register_node("chemistry:glycerin_flowing", {
	description = S("Flowing Glycerin"),
	drawtype = "flowingliquid",
	waving = 3,
	tiles = {"liquid.png"},
	special_tiles = {
		{
			name = "liquid_flowing_animated.png^[opacity:190",
			backface_culling = false,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 2,
			},
		},
		{
			name = "liquid_flowing_animated.png^[opacity:190",
			backface_culling = true,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 2,
			},
		},
	},
	use_texture_alpha = "blend",
	paramtype = "light",
	paramtype2 = "flowingliquid",
	walkable = false,
	pointable = false,
	diggable = false,
   damage_per_second = 0,
	buildable_to = true,
	is_ground_content = false,
	drop = "",
	drowning = 1,
	liquidtype = "flowing",
	liquid_alternative_flowing = "chemistry:glycerin_flowing",
	liquid_alternative_source = "chemistry:glycerin",
	liquid_viscosity = 2,
	liquid_range = 5,
	post_effect_color = {a = 122, r = 122, g = 122, b = 122},
	groups = {liquid = 3, not_in_creative_inventory = 1,
		cools_lava = 1, glycerin = 1},
	sounds = chemistry.node_sound_oil(),
})

	bucket.register_liquid(
		"chemistry:glycerin",
		"chemistry:glycerin_flowing",
		"chemistry:glycerin_bucket",
		"liquid_bucket.png",
		S("Glycerin Bucket"),
		{tool = 1}
	)

minetest.register_craftitem("chemistry:potassium_permanganate", {
	description = S("Potassium Permanganate"),
	inventory_image = "potassium_permanganate.png",
})

minetest.register_node("chemistry:potassium_permanganate_block", {
	description = S("Potassium Permanganate Block"),
	tiles = {"potassium_permanganate_block.png"},
	is_ground_content = true,
	groups = {crumbly = 2, falling_node = 1},
   on_timer = function(pos)
	if minetest.find_node_near(pos, 1, {"group:glycerin"}) then
		minetest.swap_node(pos, {name="chemistry:kmno4_with_glycerin"})
		minetest.registered_nodes["chemistry:kmno4_with_glycerin"].on_construct(pos)
	end
end,
   on_construct = function(pos)
	minetest.get_node_timer(pos):start(math.random(20, 30))
end,
	sounds = default.node_sound_sand_defaults(),
})

minetest.register_node("chemistry:kmno4_with_glycerin", {
	description = S("Potassium Permanganate with glycerin"),
	tiles = {"potassium_permanganate_block.png^[colorize:black:100"},
	is_ground_content = true,
	groups = {crumbly = 2, falling_node = 1, not_in_creative_inventory = 1},
   on_timer = function(pos)
	minetest.swap_node(pos, {name="chemistry:hot_kmno4"})
	minetest.registered_nodes["chemistry:hot_kmno4"].on_construct(pos)
end,
   on_construct = function(pos)
	minetest.get_node_timer(pos):start(math.random(20, 20))
end,
	drop = {
		max_items = 1,
		items = {
			{
				items = {'chemistry:potassium_permanganate_block'},  --The first and second drops ever
			},
      }
   },
	sounds = default.node_sound_sand_defaults(),
})

minetest.register_node("chemistry:hot_kmno4", {
	description = S("Hot Potassium Permanganate"),
	tiles = {"potassium_permanganate_block.png^[colorize:red:50"},
	is_ground_content = true,
	groups = {crumbly = 2, falling_node = 1, not_in_creative_inventory = 1},
   on_timer = function(pos)
	minetest.swap_node(pos, {name="air"})
end,
   on_construct = function(pos)
	minetest.get_node_timer(pos):start(math.random(10, 20))
end,
	drop = {
		max_items = 1,
		items = {
			{
				items = {'chemistry:potassium_permanganate_block'},  --The first and second drops ever
			},
      }
   },
	sounds = default.node_sound_sand_defaults(),
})

minetest.register_craft({
	output = 'chemistry:potassium_permanganate_block',
	recipe = {
		{'chemistry:potassium_permanganate', 'chemistry:potassium_permanganate', 'chemistry:potassium_permanganate'},
		{'chemistry:potassium_permanganate', 'chemistry:potassium_permanganate', 'chemistry:potassium_permanganate'},
		{'chemistry:potassium_permanganate', 'chemistry:potassium_permanganate', 'chemistry:potassium_permanganate'},
	}
})

-- KMnO4 Items from KMnO4 Block Crafting
minetest.register_craft({
	output = 'chemistry:potassium_permanganate 9',
	recipe = {
		{'chemistry:potassium_permanganate_block'},
	}
})

	minetest.register_abm({
		label = "chemistry: kmno4 ignition",
		nodenames = {"air", "group:glycerin"}, 
		neighbors = {"chemistry:hot_kmno4"},
		interval = 1.0,
		chance = 8,
		catch_up = true,
		action = function(pos, node)
			if minetest.find_node_near(pos, 1, "air") then
				minetest.swap_node(pos, {name="fire:basic_flame"})
			end	
		end,
	})

minetest.register_craft({
	output = 'chemistry:potassium_permanganate',
	recipe = {
		{'fire:flint_and_steel', 'chemistry:potassium_hydroxide', 'chemistry:manganese_oxide'},
	}
})

