local lib = starsoul.mod.lib

starsoul.world.tier = lib.registry.mk 'starsoul:tier'
local T = starsoul.world.tier
local fab = starsoul.type.fab

function starsoul.world.tier.fabsum(name, ty)
	local dest = fab {}
	local t = starsoul.world.tier.db[name]
	assert(t, 'reference to nonexisting tier '..name)
	if t.super then
		dest = dest+starsoul.world.tier.fabsum(t.super, ty)*(t.cost or 1)
	end
	if t.fabclasses and t.fabclasses[ty] then
		dest = dest + t.fabclasses[ty]
	end
	return dest
end

function starsoul.world.tier.tech(name, tech)
	local t = starsoul.world.tier.db[name]
	if t.techs and t.techs[tech] ~= nil then return t.techs[tech] end
	if t.super then return starsoul.world.tier.tech(t.super, tech) end
	return false
end

T.meld {
	base = {
		fabclass = {
			electric = fab {metal={copper = 10}};
			suit = fab {element={carbon = 1e3}};
			psi = fab {metal={numinium = 1}};
			bio = fab {element={carbon = 1}};
		};

	}; -- properties that apply to all tiers
	------------------
	-- tier classes --
	------------------

	lesser = {
		name = 'Lesser', adj = 'Lesser';
		super = 'base';
		fabclasses = {
			basis = fab {
				metal = {aluminum=4};
			};
		};
	};
	greater = {
		name = 'Greater', adj = 'Greater';
		super = 'base';
		fabclasses = {
			basis = fab {
				metal = {vanadium=2};
			};
		};
	};
	starsoul = {
		name = 'Starsoul', adj = 'Starsoul';
		super = 'base';
		fabclasses = {
			basis = fab {
				metal = {osmiridium=1};
			};
		};
	};
	forevanished = {
		name = 'Forevanished One', adj = 'Forevanished';
		super = 'base';
		fabclasses = {
			basis = fab {
				metal = {elusium=1};
			};
		};
	};

	------------------
	-- Lesser Races --
	------------------

	makeshift = { -- regular trash
		name = 'Makeshift', adj = 'Makeshift';
		super = 'lesser';
		techs = {tool = true, prim = true, electric = true};
		power = 0.5;
		efficiency = 0.3;
		reliability = 0.2;
		cost = 0.3;
		fabclasses = { -- characteristic materials
			basis = fab { -- fallback
				metal = {iron=3};
			};
		};
	};

	imperial = { --powerful trash
		name = 'Imperial', adj = 'Imperial';
		super = 'lesser';
		techs = {tool = true, electric = true, electronic = true, suit = true, combatSuit = true, weapon = true, hover='ion'};
		power = 2.0;
		efficiency = 0.5;
		reliability = 0.5;
		cost = 1.0;
		fabclasses = {
			basis = fab {
				metal = {steel=2};
			};
		};
	};

	commune = { --reliability
		name = 'Commune', adj = 'Commune';
		super = 'lesser';
		techs = {tool = true, electric = true, electronic = true, suit = true, combatSuit = true, weapon = true, gravitic = true, hover='grav'};
		power = 1.0;
		efficiency = 2.0;
		reliability = 3.0;
		cost = 1.5;
		fabclasses = {
			basis = fab {
				metal = {titanium=1};
				time = {print = 1.2}; -- commune stuff is intricate
			};
		};
	};

	-------------------
	-- Greater Races --
	-------------------


	----------------
	-- Starsouled --
	----------------

	suIkuri = { --super-tier
		name = 'Su\'ikuri', adj = "Su'ikuruk";
		super = 'starsoul';
		techs = {psi = true, prim = true, bioSuit = true, psiSuit = true};
		power = 1.5;
		efficiency = 1.0;
		reliability = 3.0;
		cost = 2.0;
		fabclasses = {
			psi = fab {
				metal = {numinium = 2.0};
				crystal = {beryllium = 1.0};
			};
			bio = fab {
				crystal = {beryllium = 1.0};
			};
		};
	};

	usukwinya = { --value for 'money'; no weapons; no hovertech (they are birds)
		-- NOTA BENE: the ususkwinya *do* have weapons of their own; however,
		-- they are extremely restricted and never made available except to a
		-- very select number of that species. consequently, usuk players
		-- of a certain scenario may have usuk starting weapons, but these must
		-- be manually encoded to avoid injecting them into the overall crafting
		-- /loot system. because there are so few of these weapons in existence,
		-- all so tightly controlled, the odds of the weapons or plans winding
		-- up on Farthest Shadow are basically zero unless you bring them yourself
		name = 'Usukwinya', adj = 'Usuk';
		super = 'starsoul';
		techs = lib.tbl.set('tool', 'electric', 'electronic', 'suit', 'gravitic');
		power = 2.0;
		efficiency = 2.0;
		reliability = 2.0;
		cost = 0.5;
		fabclasses = {
			basis = fab {
				crystal = {aluminum = 5}; -- ruby
			};
		};
	};

	eluthrai = { --super-tier
		name = 'Eluthrai', adj = 'Eluthran';
		super = 'starsoul';
		techs = {tool = true, electric = true, electronic = true, weapon = true, gravitic = true, gravweapon = true, suit = true, combatSuit = true, hover = 'grav'};
		power = 4.0;
		efficiency = 4.0;
		reliability = 4.0;
		cost = 4.0;
		fabclasses = {
			basis = fab {
				crystal = {carbon = 5}; -- diamond
			};
			special	= fab {
				metal = {technetium=1, cinderstone=1}
			};
		};
	};

	-----------------------
	-- Forevanished Ones --
	-----------------------

	firstborn = { --god-tier
		name = 'Firstborn', adj = 'Firstborn';
		super = 'forevanished';
		techs = {tool = true, electric = true, electronic = true, suit = true, psi = true, combatSuit = true, weapon = true, gravitic = true, gravweapon = true};
		power = 10.0;
		efficiency = 5.0;
		reliability = 3.0;
		cost = 10.0;
		fabclasses = {
			basis = fab {
				metal = {technetium=2, neodymium=3, sunsteel=1};
				crystal = {astrite=1};
			};
		};
	};

	forevanisher = { --godslayer-tier
		name = 'Forevanisher', adj = 'Forevanisher';
		super = 'forevanished';
		techs = {tool = true, electric = true, electronic = true, suit = true, psi = true, combatSuit = true, weapon = true, gravitic = true, gravweapon = true};
		power = 20.0;
		efficiency = 1.0;
		reliability = 2.0;
		cost = 100.0;
		fabclasses = {
			basis = fab {
				metal = {};
				crystal = {};
			};
		};
	};

}
