-- 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 ,
	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);

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;


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

CREATE INDEX idx_buildablereqs_technology
	ON tech.buildable_requirements( technology_id );

ALTER TABLE tech.buildable_requirements
	ADD CONSTRAINT fk_buildablereqs_buildable
		FOREIGN KEY (buildable_id) REFERENCES tech.buildables ,
	ADD CONSTRAINT fk_buildablereqs_technology
		FOREIGN KEY (technology_id) REFERENCES tech.technologies;


--
-- 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;