/*
		       ShipWars Universe & Objects

	See reality.h for ShipWars Reality Constants & Defaults.
 */

#ifndef OBJECTS_H
#define OBJECTS_H

#include <limits.h>
#include <sys/types.h>
#include "eco.h"

typedef struct _sw_vector_compoent_struct	sw_vector_compoent_struct;
#define SW_VECTOR_COMPOENT(p)		((sw_vector_compoent_struct *)(p))
typedef struct _sw_legend_struct	sw_legend_struct;
#define SW_LEGEND(p)			((sw_legend_struct *)(p))
typedef struct _xsw_object_struct	xsw_object_struct;
#define SW_OBJECT(p)			((xsw_object_struct *)(p))
typedef struct _xsw_ecoproduct_struct	xsw_ecoproduct_struct;
#define SW_ECO_PRODUCT(p)		((xsw_ecoproduct_struct *)(p))
typedef struct _xsw_ecodata_struct	xsw_ecodata_struct;
#define SW_ECO_DATA(p)			((xsw_ecodata_struct *)(p))
typedef struct _xsw_score_struct	xsw_score_struct;
#define SW_SCORE(p)			((xsw_score_struct *)(p))
typedef struct _sw_permission_struct	sw_permission_struct;
#define SW_PERMISSION(p)		((sw_permission_struct *)(p))
typedef struct _xsw_weapon_struct	xsw_weapon_struct;
#define SW_WEAPON(p)			((xsw_weapon_struct *)(p))
typedef struct _xsw_animation_struct	xsw_animation_struct;
#define SW_ANIMATION(p)			((xsw_animation_struct *)(p))


/*
 *	Garbage Object Name String:
 */
#define SW_OBJ_NAME_GARBAGE		"*Garbage*"

/*
 *	Default Object Permissions:
 *
 *	Valid values are from 5 to 0, where 0 is the highest permission
 *	level.
 */
#define SW_DEF_UID			5
#define SW_DEF_GID			5


/*
 *	String Maximums:
 *
 *      Includes the null terminating byte.
 */
#define SW_OBJ_NAME_MAX			33
#define SW_OBJ_PASSWORD_MAX		33
#define SW_OBJ_EMPIRE_MAX		9
#define SW_UNV_TITLE_MAX		128


/*
 *	Invalid Name Characters:
 *
 *	A string containing a list of characters (excluding the null
 *	terminating byte) not permitted in an object's name.
 */
#define SW_INVALID_NAME_CHARACTERS	";,=\n\r\t#"


/*
 *	Maximums:
 */
#define SW_OBJECTS_MAX			100000		/* Per universe */
#define SW_OBJ_WEAPONS_MAX		9		/* Per object */
#define SW_CREDITS_MAX			2147483647


/*
 *	Default Empire:
 */
#define SW_DEF_EMPIRE			"IND"


/*
 *	Default Shield Frequency:
 *
 *	Must be between SW_FREQ_MIN and SW_FREQ_MAX.
 */
#define SW_DEF_SHIELD_FREQ		180.20


/*
 *	Default Com Channel:
 *
 *	This value is an (int) that needs to be * 100 of the actual
 *	value between SW_FREQ_MIN and SW_FREQ_MAX.
 */
#define SW_DEF_COM_CHANNEL		31000


/*
 *	Tractor Beam Maximums:
 */
#define SW_TRACTOR_BEAM_MAX_OBJECTS	3
#define SW_TRACTOR_BEAM_MAX_RANGE	2.0	/* In real units */


/*
 *	Object Types & Type Names:
 */
#define SW_OBJ_TYPE_ERROR		-1
#define SW_OBJ_TYPE_NAME_ERROR		"Error"

#define SW_OBJ_TYPE_GARBAGE		0
#define SW_OBJ_TYPE_NAME_GARBAGE	"Garbage"

#define SW_OBJ_TYPE_STATIC		1	/* Non moving */
#define SW_OBJ_TYPE_NAME_STATIC		"Static"

#define SW_OBJ_TYPE_DYNAMIC		2	/* Moves */
#define SW_OBJ_TYPE_NAME_DYNAMIC	"Dynamic"

#define SW_OBJ_TYPE_CONTROLLED		3	/* AI controllable */
#define SW_OBJ_TYPE_NAME_CONTROLLED	"Controlled"

#define SW_OBJ_TYPE_PLAYER		4
#define SW_OBJ_TYPE_NAME_PLAYER		"Player"

#define SW_OBJ_TYPE_WEAPON		5	/* Pulse or projectile */
#define SW_OBJ_TYPE_NAME_WEAPON		"Weapon"

#define SW_OBJ_TYPE_STREAMWEAPON	6	/* Stream */
#define SW_OBJ_TYPE_NAME_STREAMWEAPON	"StreamWeapon"

#define SW_OBJ_TYPE_SPHEREWEAPON	7	/* Expanding sphere */
#define SW_OBJ_TYPE_NAME_SPHEREWEAPON	"SphereWeapon"

#define SW_OBJ_TYPE_HOME		10	/* Starbases, planets, etc */
#define SW_OBJ_TYPE_NAME_HOME		"Home"

#define SW_OBJ_TYPE_AREA		11	/* Nebulas, asteroid fields, pulsars, etc */
#define SW_OBJ_TYPE_NAME_AREA		"Area"

#define SW_OBJ_TYPE_ANIMATED		12	/* Explosions */
#define SW_OBJ_TYPE_NAME_ANIMATED	"Animated"

#define SW_OBJ_TYPE_WORMHOLE		13	/* Aka gates */
#define SW_OBJ_TYPE_NAME_WORMHOLE	"Wormhole"

#define SW_OBJ_TYPE_ELINK		14	/* External server link */
#define SW_OBJ_TYPE_NAME_ELINK		"Elink"


/*
 *	Location Type Codes:
 *
 *	Used by objects of type SW_OBJ_TYPE_AREA to determine it
 *	area type or by all other objects to determine what area type
 *	it is in.
 */
#define SW_LOC_TYPE_SPACE		0	/* Normal space */
#define SW_LOC_TYPE_STR_SPACE		"Space"

#define SW_LOC_TYPE_SPACE_TERRITORY	1	/* Normal space that is claimed */
#define SW_LOC_TYPE_STR_SPACE_TERRITORY	"SpaceTerritory"

#define SW_LOC_TYPE_NEBULA		2
#define SW_LOC_TYPE_STR_NEBULA		"Nebula"

#define SW_LOC_TYPE_ASTEROID_FIELD	3
#define SW_LOC_TYPE_STR_ASTEROID_FIELD	"AsteroidField"

#define SW_LOC_TYPE_PULSAR		4
#define SW_LOC_TYPE_STR_PULSAR		"Pulsar"


/*
 *	IFF:
 */
#define SW_IFF_UNKNOWN			0
#define SW_IFF_FRIENDLY			1
#define SW_IFF_HOSTILE			2

/*
 *	Engine states:
 */
#define SW_ENGINE_STATE_NONE		-1
#define SW_ENGINE_STATE_OFF		0
#define SW_ENGINE_STATE_STARTING	1
#define SW_ENGINE_STATE_ON		2

#define SW_ENGINE_STATE_STR_NONE	"None"
#define SW_ENGINE_STATE_STR_OFF		"Off"
#define SW_ENGINE_STATE_STR_STARTING	"Starting"
#define SW_ENGINE_STATE_STR_ON		"On"


/*
 *	Shield generator states:
 */
#define SW_SHIELD_STATE_NONE		-1
#define SW_SHIELD_STATE_DOWN		0
#define SW_SHIELD_STATE_UP		1

#define SW_SHIELD_STATE_STR_NONE	"None"
#define SW_SHIELD_STATE_STR_DOWN	"Down"
#define SW_SHIELD_STATE_STR_UP		"Up"


/*
 *	Cloak States:
 */
#define SW_CLOAK_STATE_NONE		-1
#define SW_CLOAK_STATE_DOWN		0
#define SW_CLOAK_STATE_UP		1

#define SW_CLOAK_STATE_STR_NONE		"None"
#define SW_CLOAK_STATE_STR_DOWN		"Down"
#define SW_CLOAK_STATE_STR_UP		"Up"


/*
 *	Damage Control States:
 */
#define SW_DMGCTL_STATE_OFF		0
#define SW_DMGCTL_STATE_ON		1

#define SW_DMGCTL_STATE_STR_OFF		"Off"
#define SW_DMGCTL_STATE_STR_ON		"On"


/*
 *	Weapon Emission Types:
 *
 *      These determine the firing style of the weapon.
 */
#define SW_WEP_EMISSION_STREAM		0	/* Supply = power left */
#define SW_WEP_EMISSION_PROJECTILE	1	/* Limited supply */
#define SW_WEP_EMISSION_PULSE		2	/* Supply = power left */

#define SW_WEP_EMISSION_STR_STREAM	"Stream"
#define SW_WEP_EMISSION_STR_PROJECTILE	"Projectile"
#define SW_WEP_EMISSION_STR_PULSE	"Pulse"


/*
 *	Weapon Flags:
 */
#define SW_WEP_FLAG_NO_LOCK		(1 << 0)	/* Does not lock on to
							 * anything */
#define SW_WEP_FLAG_FIRE_CLOAKED	(1 << 1)	/* Can be fired while
							 * cloaked */
#define SW_WEP_FLAG_NO_FIRE_SOUND	(1 << 2)	/* Quiet */
#define SW_WEP_FLAG_FIXED		(1 << 3)	/* No rotate */
#define SW_WEP_FLAG_PENETRATE_SHIELD	(1 << 4)	/* Penetrates the
							 * target's shields and
							 * damages its hull */
#define SW_WEP_FLAG_DESTROYABLE		(1 << 5)	/* Can be destroyed by
							 * other weapons */
#define SW_WEP_FLAG_SMOKE_TRAIL		(1 << 8)	/* Leaves a smoke trail */
#define SW_WEP_FLAG_SMOKE_PUFF_FIRED	(1 << 9)	/* Produces a smoke puff when fired */
#define SW_WEP_FLAG_SHOCKWAVE_HIT	(1 << 10)	/* Produces a shockwave when it hits anything */


#define SW_WEP_FLAG_STR_NO_LOCK		"NoLock"
#define SW_WEP_FLAG_STR_FIRE_CLOAKED	"FireCloaked"
#define SW_WEP_FLAG_STR_NO_FIRE_SOUND	"NoFireSound"
#define SW_WEP_FLAG_STR_FIXED		"Fixed"
#define SW_WEP_FLAG_STR_PENETRATE_SHIELD	"PenetrateShield"
#define SW_WEP_FLAG_STR_DESTROYABLE	"Destroyable"
#define SW_WEP_FLAG_STR_SMOKE_TRAIL	"SmokeTrail"
#define SW_WEP_FLAG_STR_SMOKE_PUFF_FIRED	"SmokePuffFired"
#define SW_WEP_FLAG_STR_SHOCKWAVE_HIT	"ShockwaveHit"

/*
 *	Lighting flags:
 *
 *	Vector lights, strobes, etc.
 *
 *	Set in member lighting of each object.
 */
#define SW_OBJ_LIGHT_VECTOR		(1 << 0)
#define SW_OBJ_LIGHT_STROBE		(1 << 1)
#define SW_OBJ_LIGHT_LUMINATION		(1 << 2)	/* Head lights */

#define SW_OBJ_LIGHT_STR_VECTOR		"Vector"
#define SW_OBJ_LIGHT_STR_STROBE		"Strobe"
#define SW_OBJ_LIGHT_STR_LUMINATION	"Lumination"

/*
 *	AI (controllable objects) AI flags:
 *
 *	Set in member ai_flags of each object.
 */
#define SW_AI_FOLLOW_FRIEND		(1 << 1)
#define SW_AI_FOLLOW_UNKNOWN		(1 << 2)
#define SW_AI_FOLLOW_HOSTILE		(1 << 3)
#define SW_AI_FOLLOW_ANY		(SW_AI_FOLLOW_FRIEND | \
					 SW_AI_FOLLOW_UNKNOWN | \
					 SW_AI_FOLLOW_HOSTILE)
#define SW_AI_FIRE_FRIEND		(1 << 8)
#define SW_AI_FIRE_UNKNOWN		(1 << 9)
#define SW_AI_FIRE_HOSTILE		(1 << 10)
#define SW_AI_FIRE_ANY			(SW_AI_FIRE_FRIEND | \
					 SW_AI_FIRE_UNKNOWN | \
					 SW_AI_FIRE_HOSTILE)

#define SW_AI_STR_FOLLOW_FRIEND		"FOLLOW_FRIEND"
#define SW_AI_STR_FOLLOW_UNKNOWN	"FOLLOW_UNKNOWN"
#define SW_AI_STR_FOLLOW_HOSTILE	"FOLLOW_HOSTILE"

#define SW_AI_STR_FIRE_FRIEND		"FIRE_FRIEND"
#define SW_AI_STR_FIRE_UNKNOWN		"FIRE_UNKNOWN"
#define SW_AI_STR_FIRE_HOSTILE		"FIRE_HOSTILE"


/*
 *	Universe Legend:
 *
 *	See reality.h for defaults.
 */
struct _sw_legend_struct {

	char		title[SW_UNV_TITLE_MAX];
	char		version[SW_UNV_TITLE_MAX];

	char		ocsn[PATH_MAX + NAME_MAX];	/* OCS Names */
	char		imgrefs[PATH_MAX + NAME_MAX];	/* Image references */
	char		sndrefs[PATH_MAX + NAME_MAX];	/* Sound references */

	/* Unit conversions */
	float		ru_to_au;

	/* Sector geometry (in real units) */
	float		sect_x_len, sect_y_len, sect_z_len,
			sect_x_min, sect_x_max,
			sect_y_min, sect_y_max,
			sect_z_min, sect_z_max;

	/* Lost and found owner (must be a player) */
	int		lost_found_owner;

	/* New player start position */
	long		player_start_sect_x,
			player_start_sect_y,
			player_start_sect_z;

	float		player_start_x,
			player_start_y,
			player_start_z;

	float		player_start_heading,
			player_start_pitch,
			player_start_bank;

	/* Guest start position */
	long		guest_start_sect_x,
			guest_start_sect_y,
			guest_start_sect_z;

	float		guest_start_x,
			guest_start_y,
			guest_start_z;

	float		guest_start_heading,
			guest_start_pitch,
			guest_start_bank;


	/* Warn when antimatter drops below this value */
	float		antimatter_low_warning;

	/* Interval of damage being applied when flying through asteroid
	 * fields (in ms)
	 */
	time_t		asteroid_field_damage_int;

	/* Damage incured while flying through asteroid fields,
	 * calculated by:
	 *
	 *	hp -= asteroid_field_damage_rate * size_ru *
	 *		velocity_coeff / cycle
	 */
	float		asteroid_field_damage_rate;

	/* Autodestruct damage range (in real units) */
	float		auto_destruct_damage_range;

	/* Cloak antimatter consumption rate, calculated by:
	 *
	 *	antimatter -= size_ru * cloak_antimatter_rate / cycle
	 */
	float		cloak_antimatter_rate;

	/* Com Message & Hail Range (in real units) */
	float		com_range;

	/* Damage control repair rate, calculated by:
	 *
	 *	hp += damage_control_rate * power * power_purity /
	 *		cycle
	 */
	float		damage_control_rate;

	/* Disarmed weapon lifespan (in ms)
	 *
	 * This value can also be:
	 *
	 *	0 = infinate
	 *	1 = no change (use weapon's current life span)
	 */
	time_t		disarmed_weapon_lifespan;

	/* Engine antimatter consumption rate, calculated by:
	 *
	 *	antimatter -= engine_antimatter_rate * throttle / cycle
	 */
	float		engine_antimatter_rate;

	/* Thrust output is multiplied by this coefficient when out of
	 * antimatter
	 */
	float		no_antimatter_thrust_coeff;

	/* Hit points coefficient in which a controlled or player object
	 * is set on fire if it drops below this coefficient
	 * (hp / hp_max)
	 */
	float		on_fire_hp_coeff;               

	/* Tractor beam antimatter consumption rate, calculated by:
	 *
	 *	 antimatter -= size_ru * tractor_beam_antimatter_rate /
	 *		cycle
	 */
	float		tractor_beam_antimatter_rate;

	/* Tractor beam range (in real units) */
	float		tractor_beam_range;

	/* Transporter range (in real units) for economy
	 * transactions
	 */
	float		transporter_range;

};


/*
 *	Data Types:
 */
typedef u_int64_t	sw_flags_t;	/* All purpose flags type */
typedef float		sw_credits_t;	/* Credits */
typedef float		sw_rmu_t;	/* Raw Materials */


/*
 *	Vector Compoent:
 */
struct _sw_vector_compoent_struct {

	float		i, j, k;

};

/*
 *	Economy Product:
 */
struct _xsw_ecoproduct_struct {

	char		name[ECO_PRODUCT_NAME_MAX];

	int		ocs_code;	/* OCS code for this product or 0
					 * if not applicable (used to identify
					 * this product) */

	/* Cost of product per 1 unit */
	sw_credits_t	sell_price,	/* Price if being bought from customer */
			buy_price;	/* Price if being sold to customer */

	float 		amount,		/* How much in sellable stock */
			amount_max;	/* Maximum possible in stock */

};

/*
 *	Economy:
 */
struct _xsw_ecodata_struct {

	eco_flags_t	flags;		/* General flags */

	/* Tax coefficient value applied to unit buy or sell price
	 * For example, 1.2 would mean 20%, so if the unit buy price
	 * is 10 credits then with tax applied it would be 12 credits
	 */
	float		tax_general,
			tax_friend,
			tax_hostile;

	xsw_ecoproduct_struct	**product;
	int			total_products;

};


/*
 *	Score:
 */
struct _xsw_score_struct {

	sw_credits_t	credits;	/* Universal credits (money) */
	sw_rmu_t	rmu,		/* Raw material units */
			rmu_max;	/* Maximum raw material units */
	float		damage_given;
	float		damage_received;
	int		kills;

};


/*
 *	Permission:
 */
struct _sw_permission_struct {

	int		uid,		/* User ID, 0 is highest */
			gid;		/* Group ID, 0 is highest */

};


/*
 *	Weapon:
 */
struct _xsw_weapon_struct {

	sw_flags_t	flags;		/* Any SW_WEP_FLAG_* */

	int		ocs_code;	/* Object create script to use to
					 * fire this weapon */
	int		emission_type;	/* One of SW_WEP_EMISSION_* */

	/* Quantity remaining and max, for emission_type =
	 * SW_WEP_EMISSION_PROJECTILE only */
	int		amount,
			max;

	float		damage_hull,	/* Damage to hull at 100% yield */
			damage_shields;	/* Damage to shields at 100% yield */

	long		range;		/* Length of stream weapons (in
					 * screen units), for emission_type =
					 * SW_WEP_EMISSION_PROJECTILE only */

	float		create_power;	/* Power needed for a unit fire */ 
	time_t		delay,          /* Interval between firings (in ms) */
			last_used;      /* Last fired (in ms) */

	/* Sound references */
	int		use_sndref,		/* Just before firing */
			fire_sndref,		/* Fire sound */
			hit_sndref,		/* When an object is hit */
			reclaim_sndref;		/* When this weapon is picked up */

	char		name[SW_OBJ_NAME_MAX];

};


/*
 *	Animation:
 */
struct _xsw_animation_struct {

	time_t		interval;	/* Milliseconds between frames */
	time_t		last_interval;	/* Last millitime frame was inc */

	int		current_frame;	/* Current frame number */
	int		total_frames;	/* Total number of frames */

	int		cycle_count;	/* Times repeated */
	int 		cycle_times;	/* Total times to repeat */

};


/*
 *	ShipWars Object:
 */
struct _xsw_object_struct {

	int		type;		/* Type of object */

	sw_flags_t	ext_flags;	/* Proprietery flags used solely by
					 * the client, server, user, etc */

	char		name[SW_OBJ_NAME_MAX];
	char		*password;
	char		empire[SW_OBJ_EMPIRE_MAX];
	char		*description;
	char		*elink;		/* The elink value for ELINK objects */

	time_t		last_updated;	/* Last updated by client or
					 * server (in ms) */
	time_t		last_connected;	/* In seconds */

	int		loc_type;	/* Type of location object is in */
	int		loc_obj_num;	/* Area object number that this object
					 * is in or -1 for none */
	int		imgref;		/* Image reference number */
	int		owner;		/* Object that owns this object or
					 * -1 for none */
	long		size;		/* Radius of object size in screen units */

	int		locked_on;	/* Object number that this object is
					 * locked on or -1 for none */

	int		intercepting_object;	/* Object number that this
						 * object is intercepting or
						 * -1 for none */

	float		scanner_range;		/* In real units */

	long		sect_x,		/* Current sector */
			sect_y,
			sect_z;

	float		x,		/* Coordinate position in sector */
			y,
			z;

	float		heading,	/* In radians */
			pitch,		/* In radians (not implmented yet) */
			bank;		/* In radians (not implmented yet) */
	sw_vector_compoent_struct attitude_vector_compoent;

	float		velocity;
	float		velocity_max;
	float		velocity_heading,	/* In radians */
			velocity_pitch,		/* In radians (not implmented yet) */
			velocity_bank;		/* In radians (not implmented yet) */
	sw_vector_compoent_struct momentum_vector_compoent;

	float		thrust_dir;	/* In radians (for omni directional thrust) */
	float		thrust;		/* throttle * thrust_power */
	float		thrust_power;	/* Full power of thrust */
	float		throttle;	/* 0.0 to 1.0 */
	char		engine_state;	/* One of SW_ENGINE_STATE_* */

	float		turnrate;	/* In radians per cycle */

	float		odometer;	/* Total cumulative distance
					 * traveled in real units */

	sw_flags_t	lighting;	/* Vector lights, strobes, etc switches */

	float		hp,		/* Hull or density */
			hp_max;

	float		power,
			power_max;
	float		power_purity;	/* 0.0 to 1.0 */

	float		core_efficency;	/* In power units per cycle */

	float		antimatter,
			antimatter_max;

	int		shield_state;	/* One of SHIELD_STATE_* */
	float		shield_frequency;	/* In KHz */

	int		selected_weapon,
			total_weapons;	/* May not be greater than SW_OBJ_WEAPONS_MAX */

	time_t		birth_time;	/* In ms */
	time_t		lifespan;	/* In ms, 0 means never dies */

	int		creation_ocs;	/* The OCS used to create this
					 * object, 0 for none (used for
					 * weapons reclaiming) */

	int		cloak_state;	/* One of CLOAK_STATE_* */
	float		cloak_strength;	/* 0.0 to 1.0, when cloak is on,
					 * cloak_strength reduces visibility */
	time_t		last_cloak;	/* Last cloak raised or lower, in ms */
	time_t		cloak_refractory_int;	/* Delay before you can cloak
						 * again or fire a weapon,
						 * in ms */

	float		visibility;	/* Normal visibility: 0.0 to 1.0 */
	float		cur_visibility;	/* Actual current visibility */

	float		shield_visibility;	/* When greater than 0.0,
						 * shields are visibable */

	int		damage_control;	/* One of DMGCTL_STATE_* */

	int		com_channel;	/* In * 100 units */

	time_t		auto_destruct_next;	/* Time to when auto destruct
						 * should occure in systime
						 * seconds (0 for none) */

	/* AI flags (for type XSW_OBJ_TYPE_CONTROLLED only) */
	sw_flags_t	ai_flags;


	/* Objects being tractored */
	int		*tractored_object;
	int		total_tractored_objects;


	/* Permissions */
	sw_permission_struct	permission;

	/* Animation */
	xsw_animation_struct	animation;

	/* Score (dynamically allocated) */
	xsw_score_struct	*score;

	/* Weapons (dynamically allocated, limited to SW_OBJ_WEAPONS_MAX) */
	xsw_weapon_struct	**weapons;

	/* Economy (dynamically allocated) */
	xsw_ecodata_struct	*eco;

};


#endif /* OBJECTS_H */