local lib = starlit.mod.lib

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

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

function starlit.world.tier.tech(name, tech)
	local t = starlit.world.tier.db[name]
	if t.techs and t.techs[tech] ~= nil then return t.techs[tech] end
	if t.super then return starlit.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';
		fabclass = {
			basis = fab {
				element = {aluminum=4};
			};
		};
	};
	greater = {
		name = 'Greater', adj = 'Greater';
		super = 'base';
		fabclass = {
			basis = fab {
				element = {vanadium=2};
			};
		};
	};
	starlit = {
		name = 'Starlit', adj = 'Starlit';
		super = 'base';
		fabclass = {
			basis = fab {
				element = {iridium=1};
			};
		};
	};
	forevanished = {
		name = 'Forevanished One', adj = 'Forevanished';
		super = 'base';
		fabclass = {
			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;
		fabclass = { -- characteristic materials
			basis = fab { -- fallback
				element = {aluminum=4};
			};
		};
	};

	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;
		fabclass = {
			basis = fab {
				element = {iron=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;
		fabclass = {
			basis = fab {
				element = {titanium=1};
				time = {print = 1.2}; -- commune stuff is intricate
			};
		};
	};

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


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

	suIkuri = { --super-tier
		name = 'Su\'ikuri', adj = "Su'ikuruk";
		super = 'starlit';
		techs = {psi = true, prim = true, bioSuit = true, psiSuit = true};
		power = 1.5;
		efficiency = 1.0;
		reliability = 3.0;
		cost = 2.0;
		fabclass = {
			psi = fab {
				element = {numinium = 2.0};
			};
			bio = fab {
				element = {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, those members who need stronger
		-- defenses than their native psionics can provide. 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 = 'starlit';
		techs = lib.tbl.set('tool', 'electric', 'electronic', 'suit', 'gravitic');
		power = 2.0;
		efficiency = 2.0;
		reliability = 2.0;
		cost = 0.5;
		fabclass = {
			basis = fab {
				element = {cobalt=2};
			};
		};
	};

	eluthrai = { --super-tier
		name = 'Eluthrai', adj = 'Eluthran';
		super = 'starlit';
		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;
		fabclass = {
			basis = fab {
				element = {scandium = 1};
			};
			weapon = {
				metal = {cinderstone = 1};
			};
			special	= fab {
				element = {technetium=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;
		fabclass = {
			basis = fab {
				element = {technetium=2, neodymium=3};
-- 				metal = {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;
		fabclass = {
			basis = fab {
				metal = {};
			};
		};
	};

}
