#pragma once

// define this if svqc code wants to use .frame2 and .lerpfrac
//#define CSQCMODEL_HAVE_TWO_FRAMES

// don't define this ever
//#define CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW

// server decides crouching, this lags, but so be it
//#define CSQCMODEL_SERVERSIDE_CROUCH

// a hack for Xonotic
#ifdef CSQC
# define TAG_ENTITY_NAME tag_networkentity
# define TAG_ENTITY_TYPE float
.float tag_networkentity;

# define TAG_VIEWLOC_NAME tag_networkviewloc
# define TAG_VIEWLOC_TYPE int
.float tag_networkviewloc;

# define ALPHA m_alpha
.float m_alpha;

# define GROUNDENTITY_NAME ground_networkentity
# define GROUNDENTITY_TYPE float
.float ground_networkentity;
#else
# define TAG_ENTITY_NAME tag_entity
# define TAG_ENTITY_TYPE entity

# define TAG_VIEWLOC_NAME viewloc
# define TAG_VIEWLOC_TYPE entity

# define ALPHA alpha

# define GROUNDENTITY_NAME groundentity
# define GROUNDENTITY_TYPE entity
#endif

// add properties you want networked to CSQC here
// NOTE: some properties share the same flag due to the limited number of bits (24) we can use
//   generally the grouped ones are cheap (1 byte and less frequently networked)
//   bits above 14 are defined in lib/csqcmodel/common.qh
#define CSQCMODEL_EXTRAPROPERTIES \
	CSQCMODEL_PROPERTY(BIT(0), int, ReadShort, WriteShort, colormap) \
	CSQCMODEL_IF(!isplayer) \
		CSQCMODEL_PROPERTY(BIT(0), int, ReadByte, WriteByte, sv_entnum) \
	CSQCMODEL_ENDIF \
	CSQCMODEL_PROPERTY(BIT(1), int, ReadInt24_t, WriteInt24_t, effects) \
	CSQCMODEL_PROPERTY(BIT(2), int, ReadByte, WriteByte, modelflags) \
	CSQCMODEL_PROPERTY(BIT(2), int, ReadByte, WriteByte, skin) \
	CSQCMODEL_PROPERTY(BIT(2), int, ReadByte, WriteByte, traileffect) \
	CSQCMODEL_PROPERTY(BIT(3), int, ReadByte, WriteByte, move_movetype) \
	CSQCMODEL_PROPERTY_SCALED(BIT(3), float, ReadByte, WriteByte, ALPHA, 254, -1, 254) /* NOTE: update the use of this in cl_model.qc if you change the bitflag number! */ \
	CSQCMODEL_PROPERTY(BIT(3), float, ReadByte, WriteByte, solid) \
	CSQCMODEL_PROPERTY(BIT(4), int, ReadByte, WriteByte, clipgroup) \
	CSQCMODEL_PROPERTY(BIT(5), float, ReadApproxPastTime, WriteApproxPastTime, death_time) \
	CSQCMODEL_IF(isplayer) \
		CSQCMODEL_PROPERTY(BIT(6), GROUNDENTITY_TYPE, ReadShort, WriteEntity, GROUNDENTITY_NAME) \
	CSQCMODEL_ENDIF \
	CSQCMODEL_IF(!isplayer) \
		CSQCMODEL_PROPERTY(BIT(7), TAG_ENTITY_TYPE, ReadShort, WriteEntity, TAG_ENTITY_NAME) \
		CSQCMODEL_PROPERTY_SCALED(BIT(8), float, ReadByte, WriteByte, glowmod_x, 254, -1, 254) \
		CSQCMODEL_PROPERTY_SCALED(BIT(8), float, ReadByte, WriteByte, glowmod_y, 254, -1, 254) \
		CSQCMODEL_PROPERTY_SCALED(BIT(8), float, ReadByte, WriteByte, glowmod_z, 254, -1, 254) \
	CSQCMODEL_ENDIF \
	CSQCMODEL_PROPERTY_SCALED(BIT(8), float, ReadByte, WriteByte, colormod_x, 16, 0, 255) \
	CSQCMODEL_PROPERTY_SCALED(BIT(8), float, ReadByte, WriteByte, colormod_y, 16, 0, 255) \
	CSQCMODEL_PROPERTY_SCALED(BIT(8), float, ReadByte, WriteByte, colormod_z, 16, 0, 255) \
	CSQCMODEL_IF(isplayer) \
		CSQCMODEL_PROPERTY(BIT(7), int, ReadByte, WriteByte, anim_state) \
		CSQCMODEL_PROPERTY(BIT(7), float, ReadApproxPastTime, WriteApproxPastTime, anim_time) \
		CSQCMODEL_IF(!islocalplayer) \
			CSQCMODEL_PROPERTY(BIT(8), float, ReadChar, WriteChar, anim_lower_action) \
			CSQCMODEL_PROPERTY(BIT(8), float, ReadApproxPastTime, WriteApproxPastTime, anim_lower_time) \
		CSQCMODEL_ENDIF \
		CSQCMODEL_PROPERTY(BIT(9), float, ReadChar, WriteChar, anim_upper_action) \
		CSQCMODEL_PROPERTY(BIT(9), float, ReadApproxPastTime, WriteApproxPastTime, anim_upper_time) \
	CSQCMODEL_ENDIF \
	CSQCMODEL_PROPERTY(BIT(10), float, ReadAngle, WriteAngle, v_angle_x) \
	CSQCMODEL_PROPERTY(BIT(11), TAG_VIEWLOC_TYPE, ReadShort, WriteEntity, TAG_VIEWLOC_NAME) /* NOTE: update the use of this in cl_model.qc if you change the bitflag number! */ \
	CSQCMODEL_PROPERTY(BIT(12), float, ReadCoord, WriteCoord, scale) \
	CSQCMODEL_PROPERTY(BIT(13), int, ReadInt24_t, WriteInt24_t, dphitcontentsmask) \
	/**/
// NOTE: bits above 14 are defined in lib/csqcmodel/common.qh, avoid reusing them if possible
//   in particular bit 15 should NOT be reused, it is large enough sending the model's hitbox vectors!
// TODO get rid of colormod/glowmod here; also get rid of some useless properties on non-players that only exist for CopyBody

// add hook function calls here
#define CSQCPLAYER_HOOK_POSTCAMERASETUP() \
	viewloc_SetViewLocation()

// force updates of player entities this frequently (per second) even if unchanged
#define CSQCPLAYER_FORCE_UPDATES 4

// mod must define:
//vector PL_MIN  = ...;
//vector PL_MAX  = ...;
//vector PL_VIEW_OFS  = ...;
//vector PL_CROUCH_MIN  = ...;
//vector PL_CROUCH_MAX  = ...;
//vector PL_CROUCH_VIEW_OFS  = ...;

#ifdef SVQC
# define CSQCMODEL_AUTOINIT(e) CSQCModel_LinkEntity(e)
# define CSQCMODEL_AUTOUPDATE(e) CSQCModel_CheckUpdate(e)
#endif

#define CSQCMODEL_EF_RESPAWNGHOST EF_SELECTABLE
