-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Buildings/ships definitions
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------

--
-- "Buildables"
--
CREATE TABLE tech.buildables(
	name_id				INT NOT NULL PRIMARY KEY ,
	description_id		INT NOT NULL ,
	technology_name_id	INT ,
	cost				INT NOT NULL CHECK( cost > 0 ) ,
	work				INT NOT NULL CHECK( work > 0 ) ,
	upkeep				INT NOT NULL CHECK( upkeep >= 0 )
);

CREATE INDEX idx_buildables_description
	ON tech.buildables (description_id);
CREATE INDEX idx_buildables_technology
	ON tech.buildables ( technology_name_id );

ALTER TABLE tech.buildables
	ADD CONSTRAINT fk_buildables_name
		FOREIGN KEY (name_id) REFERENCES defs.strings ,
	ADD CONSTRAINT fk_buildables_description
		FOREIGN KEY (description_id) REFERENCES defs.strings ,
	ADD CONSTRAINT fk_buildables_technology
		FOREIGN KEY (technology_name_id) REFERENCES defs.technologies;


--
-- Requirements
--
CREATE TABLE tech.buildable_requirements(
	buildable_id	INT NOT NULL ,
	level_id		INT NOT NULL ,
	PRIMARY KEY( buildable_id , level_id )
);

CREATE INDEX idx_buildablereqs_level
	ON tech.buildable_requirements( level_id );

ALTER TABLE tech.buildable_requirements
	ADD CONSTRAINT fk_buildablereqs_buildable
		FOREIGN KEY (buildable_id) REFERENCES tech.buildables ,
	ADD CONSTRAINT fk_buildablereqs_level
		FOREIGN KEY (level_id) REFERENCES tech.levels;


--
-- Buildings
--
CREATE TABLE tech.buildings(
	buildable_id	INT NOT NULL PRIMARY KEY ,
	workers			INT NOT NULL CHECK( workers >= 0 ) ,
	output_type		building_output_type ,
	output			INT NOT NULL CHECK( output > 0 )
);

ALTER TABLE tech.buildings
	ADD CONSTRAINT fk_buildings_buildable
		FOREIGN KEY (buildable_id) REFERENCES tech.buildables;

--
-- Ships
--
CREATE TABLE tech.ships(
	buildable_id	INT NOT NULL PRIMARY KEY ,
	flight_time		INT NOT NULL CHECK( flight_time > 0 ) ,
	power			INT NOT NULL CHECK( power > 0 )
);


ALTER TABLE tech.ships
	ADD CONSTRAINT fk_buildings_buildable
		FOREIGN KEY (buildable_id) REFERENCES tech.buildables;