
local S = minetest.get_translator("chemistry")


minetest.register_craftitem("chemistry:radium", {
	description = S("Radium"),
	inventory_image = "radium.png",
})

minetest.register_node("chemistry:radium_block", {
	description = S("Radium Block"),
	tiles = {"radium_block.png"},
	is_ground_content = true,
	groups = {cracky = 1, radioactive = 10, level = 3},
	sounds = default.node_sound_metal_defaults(),
})

minetest.register_craft({
	output = 'chemistry:radium_block',
	recipe = {
		{'chemistry:radium', 'chemistry:radium', 'chemistry:radium'},
		{'chemistry:radium', 'chemistry:radium', 'chemistry:radium'},
		{'chemistry:radium', 'chemistry:radium', 'chemistry:radium'},
	}
})

minetest.register_craft({
	output = 'chemistry:radium 9',
	recipe = {
		{'chemistry:radium_block'},
	}
})

minetest.register_craftitem("chemistry:radium_dust", {
	description = S("Radium Dust"),
	inventory_image = "radium_dust.png",
})

-- Ore generation
-- -128 <-> -255

minetest.register_node("chemistry:radon", {
	description = S("Radon Gas"),
	walkable = false,
	pointable = false,
	diggable = false,
	is_ground_content = false,
	buildable_to = true,
	drawtype = "glasslike",
	drowning = 1,
   damage_per_second = 0,
	post_effect_color = {a = 122, r = 122, g = 122, b = 122},
	tiles = {"incolored_gas.png^[opacity:15"},
	wield_image = "radon_wl.png",
	inventory_image = "radon_wl.png",
	use_texture_alpha = "blend",
	groups = {not_in_creative_inventory=0, ropes_can_extend_into=1, not_solid=1, not_opaque=1, radioactive = 1, gas=1},
	paramtype = "light",
	drop = {},
	sunlight_propagates = true,
	--on_blast = function() end, -- unaffected by explosions
})

local directions = {
	{x=1, y=0, z=0},
	{x=-1, y=0, z=0},
	{x=0, y=0, z=1},
	{x=0, y=0, z=-1},
}

local gas_node = {name="chemistry:radon"}
minetest.register_abm({
    label = "chemistry:gases_movement",
    nodenames = {"chemistry:radon"},
    neighbors = {"group:liquid", "air", "group:falling_node"},
    interval = 1,
    chance = 1,
    catch_up = true,
    action = function(pos, node)
		local next_pos = {x=pos.x, y=pos.y+1, z=pos.z}
		local next_node = minetest.get_node(next_pos)
		if minetest.get_item_group(next_node.name, "liquid") > 0 then
			minetest.swap_node(next_pos, gas_node)
			minetest.swap_node(pos, next_node)
		elseif minetest.get_item_group(next_node.name, "falling_node") > 0 then
			minetest.swap_node(next_pos, gas_node)
			minetest.swap_node(pos, next_node)
		else
			next_pos.y = pos.y-1
			next_node = minetest.get_node(next_pos)
			if next_node.name == "air" then
				minetest.swap_node(next_pos, gas_node)
				minetest.swap_node(pos, next_node)			
			else
				local dir = directions[math.random(1,4)]
				local next_pos = vector.add(pos, dir)
				local next_node = minetest.get_node(next_pos)
				if next_node.name == "air" or  minetest.get_item_group(next_node.name, "liquid") > 0 then
					if next_node.name == "air" or math.random() < 0.5 then -- gas never "climbs" above air.
						minetest.swap_node(next_pos, gas_node)
						minetest.swap_node(pos, next_node)
					else
						-- this can get gas to rise up out of the surface of liquid, preventing it from forming a permanent hole.
						next_pos.y = next_pos.y + 1
						next_node = minetest.get_node(next_pos)
						if next_node.name == "air" then
							minetest.swap_node(next_pos, gas_node)
							minetest.swap_node(pos, next_node)
						end
					end
            end
			end
		end
	end,
})

minetest.register_craftitem("chemistry:polonium", {
	description = S("Polonium"),
	inventory_image = "polonium.png",
})

minetest.register_node("chemistry:polonium_block", {
	description = S("Polonium Block"),
	tiles = {"polonium_block.png"},
	is_ground_content = true,
	groups = {cracky = 2, radioactive = 7, toxic=1},
	sounds = default.node_sound_stone_defaults(),
})

minetest.register_craft({
	output = 'chemistry:polonium_block',
	recipe = {
		{'chemistry:polonium', 'chemistry:polonium', 'chemistry:polonium'},
		{'chemistry:polonium', 'chemistry:polonium', 'chemistry:polonium'},
		{'chemistry:polonium', 'chemistry:polonium', 'chemistry:polonium'},
	}
})

minetest.register_craft({
	output = 'chemistry:polonium 9',
	recipe = {
		{'chemistry:polonium_block'},
	}
})


minetest.register_craftitem("chemistry:thorium", {
	description = S("Thorium"),
	inventory_image = "thorium.png",
})

minetest.register_node("chemistry:thorium_block", {
	description = S("Thorium Block"),
	tiles = {"thorium_block.png"},
	is_ground_content = true,
	groups = {cracky = 2, radioactive = 5, level = 2},
	sounds = default.node_sound_stone_defaults(),
})

minetest.register_craft({
	output = 'chemistry:thorium_block',
	recipe = {
		{'chemistry:thorium', 'chemistry:thorium', 'chemistry:thorium'},
		{'chemistry:thorium', 'chemistry:thorium', 'chemistry:thorium'},
		{'chemistry:thorium', 'chemistry:thorium', 'chemistry:thorium'},
	}
})

minetest.register_craft({
	output = 'chemistry:thorium 9',
	recipe = {
		{'chemistry:thorium_block'},
	}
})

-- Ore generation
-- -128 <-> -255

local orthogonal = {
	{x=0,y=0,z=1},
	{x=0,y=1,z=0},
	{x=1,y=0,z=0},
	{x=0,y=0,z=-1},
	{x=0,y=-1,z=0},
	{x=-1,y=0,z=0},
}

minetest.register_abm({
	label = "chemistry:thorite decay",
	nodenames = {"chemistry:thorite"},
	neighbors = {"air"},
	interval = 1.0,
	chance = 5,
	catch_up = true,
	action = function(pos, node)
		local target_pos = vector.add(pos,orthogonal[math.random(1,6)])
		if minetest.get_node(target_pos).name == "air" then
			minetest.set_node(target_pos, {name="chemistry:radon"})
			if math.random() < 0.5 then
				minetest.sound_play(
					"",
					{pos = pos, max_hear_distance = 8, gain = 0.05}
				)
			end
		end	
	end,
})

minetest.register_node("chemistry:uranium_dioxide", {
	description = S("Uranium Dioxide"),
	tiles = {{
		name = "uranium_dioxide.png",
    	align_style = "world",
	    scale       = 2,
	}},
	groups = {crumbly = 3, falling_node = 1, radioactive = 2},
	sounds = default.node_sound_sand_defaults(),
})

minetest.register_craftitem("chemistry:uranium", {
	description = S("Uranium"),
	inventory_image = "uranium.png",
})

minetest.register_node("chemistry:uranium_block", {
	description = S("Uranium Block"),
	tiles = {"uranium_block.png"},
	is_ground_content = true,
	groups = {cracky = 1, radioactive = 6},
	sounds = default.node_sound_stone_defaults(),
})

minetest.register_craft({
	output = 'chemistry:uranium_block',
	recipe = {
		{'chemistry:uranium', 'chemistry:uranium', 'chemistry:uranium'},
		{'chemistry:uranium', 'chemistry:uranium', 'chemistry:uranium'},
		{'chemistry:uranium', 'chemistry:uranium', 'chemistry:uranium'},
	}
})

minetest.register_craft({
	output = 'chemistry:uranium 9',
	recipe = {
		{'chemistry:uranium_block'},
	}
})

minetest.register_node("chemistry:uranium_water", {
	description = S("Uranium Water"),
	drawtype = "liquid",
	waving = 3,
	tiles = {
		{
			name = "yliquid_source_animated.png^[opacity:150",
			backface_culling = false,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 2.0,
			},
		},
		{
			name = "yliquid_source_animated.png^[opacity:150",
			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 = 5,
	buildable_to = true,
	is_ground_content = false,
	drop = "",
	drowning = 1,
	liquidtype = "source",
	liquid_alternative_flowing = "chemistry:uranium_water_flowing",
	liquid_alternative_source = "chemistry:uranium_water",
	liquid_viscosity = 1,
	post_effect_color = {a = 122, r = 122, g = 122, b = 0},
	groups = {liquid = 3, cools_lava = 1, radioactive = 2, water = 1},
	sounds = default.node_sound_water_defaults(),
})

minetest.register_node("chemistry:uranium_water_flowing", {
	description = S("Flowing Uranium Water"),
	drawtype = "flowingliquid",
	waving = 3,
	tiles = {"yliquid.png"},
	special_tiles = {
		{
			name = "yliquid_flowing_animated.png^[opacity:150",
			backface_culling = false,
			animation = {
				type = "vertical_frames",
				aspect_w = 16,
				aspect_h = 16,
				length = 0.5,
			},
		},
		{
			name = "yliquid_flowing_animated.png^[opacity:150",
			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,
   damage_per_second = 5,
	buildable_to = true,
	is_ground_content = false,
	drop = "",
	drowning = 1,
	liquidtype = "flowing",
	liquid_alternative_flowing = "chemistry:uranium_water_flowing",
	liquid_alternative_source = "chemistry:uranium_water",
	liquid_viscosity = 1,
	post_effect_color = {a = 122, r = 122, g = 122, b = 0},
	groups = {liquid = 3, not_in_creative_inventory = 1,
		cools_lava = 1, radioactive = 2, water = 1},
	sounds = default.node_sound_water_defaults(),
})

	bucket.register_liquid(
		"chemistry:uranium_water",
		"chemistry:uranium_water_flowing",
		"chemistry:uranium_water_bucket",
		"yellow_wt_bucket.png",
		("Uranium Water Bucket"),
		{tool = 1}
	)

minetest.register_craft({
   type = "shapeless",
	output = 'chemistry:uranyl_acetate',
	recipe = {
		'chemistry:uranium_dioxide', 'chemistry:ch3cooh_acid_bucket',
	},
	replacements = {{'chemistry:ch3cooh_acid_bucket', 'bucket:bucket_empty'}},
})

minetest.register_craft({
   type = "shapeless",
	output = 'chemistry:uranyl_nitrate',
	recipe = {
		'chemistry:uranyl_acetate', 'chemistry:hno_acid_bucket',
	},
	replacements = {{'chemistry:hno_acid_bucket', 'bucket:bucket_empty'}, {'chemistry:uranyl_acetate', 'chemistry:nitrogen_dioxide 5'}},
})

minetest.register_craft({
   type = "shapeless",
	output = 'chemistry:uranium_water_bucket',
	recipe = {
		'chemistry:uranyl_nitrate', 'bucket:bucket_water',
	}
})

minetest.register_craft({
   type = "shapeless",
	output = 'chemistry:sodium_diuranate',
	recipe = {
		'chemistry:uranium_water_bucket', 'chemistry:cwater_bucket', 'vessels:glass_bottle',
	},
	replacements = {{'chemistry:cwater_bucket', 'bucket:bucket_empty'}, {'chemistry:uranium_water_bucket', 'chemistry:uranium_water_bucket'}},
})

minetest.register_craft({
   type = "shapeless",
	output = 'chemistry:uranium_comp',
	recipe = {
		'chemistry:sodium_diuranate', 'vessels:glass_fragments',
	}
})

minetest.register_craftitem("chemistry:uranyl_acetate", {
	description = S("Uranyl Acetate"),
	inventory_image = "uranyl_acetate.png",
})

minetest.register_craftitem("chemistry:uranyl_nitrate", {
	description = S("Uranyl Nitrate"),
	inventory_image = "uranyl_nitrate.png",
})

minetest.register_craftitem("chemistry:sodium_diuranate", {
	description = S("Sodium Diuranate"),
	inventory_image = "sodium_diuranate.png",
})

minetest.register_craftitem("chemistry:uranium_comp", {
	description = S("Uranium Compound"),
	inventory_image = "uranium_comp.png",
})

minetest.register_node("chemistry:uranium_glass", {
	description = S("Uranium Glass"),
	drawtype = "glasslike_framed_optional",
	tiles = {"uranium_glass.png", "uranium_glass_detail.png"},
	use_texture_alpha = "clip",
	paramtype = "light",
   light_source = 7,
   glow = 7,
	sunlight_propagates = true,
	is_ground_content = false,
	groups = {cracky = 3, oddly_breakable_by_hand = 3},
	sounds = default.node_sound_glass_defaults(),
})

minetest.register_craft({
	type = "cooking",
	cooktime = 3,
	output = "chemistry:uranium_glass",
	recipe = "chemistry:uranium_comp"
})

-- Ore generation
-- -128 <-> -255

minetest.register_craftitem("chemistry:plutonium", {
	description = S("Plutonium"),
	inventory_image = "plutonium.png",
})

minetest.register_node("chemistry:plutonium_block", {
	description = S("Plutonium Block"),
	tiles = {"plutonium_block.png"},
	is_ground_content = true,
	groups = {cracky = 2, radioactive = 6},
	sounds = default.node_sound_stone_defaults(),
})

minetest.register_craft({
	output = 'chemistry:plutonium_block',
	recipe = {
		{'chemistry:plutonium', 'chemistry:plutonium', 'chemistry:plutonium'},
		{'chemistry:plutonium', 'chemistry:plutonium', 'chemistry:plutonium'},
		{'chemistry:plutonium', 'chemistry:plutonium', 'chemistry:plutonium'},
	}
})

minetest.register_craft({
	output = 'chemistry:plutonium 9',
	recipe = {
		{'chemistry:plutonium_block'},
	}
})

minetest.register_node("chemistry:americium_ore", {
	description = S("Americium Ore"),
	tiles = {"plutonium_block.png^americium_ore.png"},
	is_ground_content = true,
	groups = {cracky=1, radioactive = 7},
	drop = {
		max_items = 9,
		items = {
			{
				items = {'chemistry:americium 1'},
			},
			{
				items = {'chemistry:plutonium 8'}, 
			},
		}
	},
	sounds = default.node_sound_stone_defaults(),
})

minetest.register_craftitem("chemistry:americium", {
	description = S("Americium"),
	inventory_image = "americium.png",
})

minetest.register_node("chemistry:americium_block", {
	description = S("Americium Block"),
	tiles = {"americium_block.png"},
	is_ground_content = true,
	groups = {cracky = 2, radioactive = 7, level = 4},
	sounds = default.node_sound_metal_defaults(),
})

minetest.register_craft({
	output = 'chemistry:americium_block',
	recipe = {
		{'chemistry:americium', 'chemistry:americium', 'chemistry:americium'},
		{'chemistry:americium', 'chemistry:americium', 'chemistry:americium'},
		{'chemistry:americium', 'chemistry:americium', 'chemistry:americium'},
	}
})

minetest.register_craft({
	output = 'chemistry:americium 9',
	recipe = {
		{'chemistry:americium_block'},
	}
})

if minetest.get_modpath("uraniumstuff") then
local allow_armor_damage = minetest.settings:get_bool("uraniumstuff.allow_armor_radiation", true)
    
    local function has_item(player_name, item_name)
        local inventory = minetest.get_inventory({type="player", name=player_name})
        local main_list = inventory:get_list("main")
        for _, stack in ipairs(main_list) do
            if stack:get_name() == item_name then
                return true
            end
        end
        return false
    end

    local function alter_health(player, change)
        if player == nil then return end
        local hp_max = player:get_properties().hp_max
        local current_health = player:get_hp()
        local new_health = current_health + change

        if new_health > hp_max then new_health = hp_max end
        if new_health < 0 then new_health = 0 end

        player:set_hp(new_health)
    end

    local function pattern_count(base, pattern)
        return select(2, string.gsub(base, pattern, ""))
    end

    local function get_radioactive_armor_count(player)
        local inv_3d = player:get_meta():get_string("3d_armor_inventory")
        if not inv_3d then return 0 end
        return pattern_count(inv_3d, "cchemistry:")
    end

    local function damage_players()
        local players = minetest.get_connected_players()
        for _, player in ipairs(players) do
            local damage = 0
            if allow_armor_damage then
                damage = damage + get_radioactive_armor_count(player)
            end
            if damage > 0 then
                local player_name = player:get_player_name()
                local has_gem = has_item(player_name, "uraniumstuff:uranium_protection_gem")
                if not has_gem then
                    alter_health(player, damage*-1)
                end
            end
        end
        minetest.after(2, damage_players)
    end
    damage_players()

end

