#pragma once

// Client/server mappings

#ifdef CSQC
	#include <client/csqcmodel_hooks.qh> // for .isplayermodel, .csqcmodel_isdead
	#include <common/resources/cl_resources.qh> // for GetResource
	#include <lib/csqcmodel/common.qh> // for ISPLAYER_CLIENT, ISPLAYER_PLAYER
#endif

#ifdef SVQC
// TODO: get rid of this random dumb include!
	#include <common/state.qh>

float autocvar_sv_airaccel_qw;
float autocvar_sv_airstrafeaccel_qw;
float autocvar_sv_airspeedlimit_nonqw;
float autocvar_sv_airaccel_qw_stretchfactor;
float autocvar_sv_maxairstrafespeed;
float autocvar_sv_airstrafeaccelerate;
float autocvar_sv_warsowbunny_turnaccel;
float autocvar_sv_airaccel_sideways_friction;
float autocvar_sv_aircontrol;
int autocvar_sv_aircontrol_flags;
float autocvar_sv_aircontrol_power;
float autocvar_sv_aircontrol_penalty;
float autocvar_sv_warsowbunny_airforwardaccel;
float autocvar_sv_warsowbunny_topspeed;
float autocvar_sv_warsowbunny_accel;
float autocvar_sv_warsowbunny_backtosideratio;
float autocvar_sv_friction;
float autocvar_sv_accelerate;
float autocvar_sv_stopspeed;
float autocvar_sv_airaccelerate;
float autocvar_sv_slickaccelerate;
float autocvar_sv_airstopaccelerate;
bool autocvar_sv_airstopaccelerate_full;
float autocvar_sv_track_canjump;
string autocvar_g_physics_clientselect_options;
string autocvar_g_physics_clientselect_default;
bool autocvar_g_jump_grunt;
bool autocvar_g_physics_clientselect;
float autocvar_g_maxspeed;
float autocvar_g_movement_highspeed = 1;
bool autocvar_g_movement_highspeed_q3_compat = 0;
bool autocvar_speedmeter;
float autocvar_sv_jumpspeedcap_max;
bool autocvar_sv_jumpspeedcap_max_disable_on_ramps;
float autocvar_sv_jumpspeedcap_min;
float autocvar_sv_jumpvelocity;
float autocvar_sv_jumpvelocity_crouch;
float autocvar_sv_maxairspeed;
float autocvar_sv_maxspeed;
bool autocvar_g_footsteps;
#endif

.entity conveyor;

.float race_penalty;

.float gravity;
.float swamp_slowdown;
.int lastflags;
.float lastground;
.bool wasFlying;

.int buttons_old;
.vector movement_old;
.vector v_angle_old;
.string lastclassname;

.float(entity,float) PlayerPhysplug;
float AdjustAirAccelQW(float accelqw, float factor);

bool IsFlying(entity a);

#ifdef GAMEQC
REPLICATE_INIT(string, cvar_cl_physics);
REPLICATE_INIT(bool, cvar_cl_jetpack_jump);
REPLICATE_INIT(bool, cvar_cl_movement_track_canjump);
#endif

#define PHYS_PL_MAX(s)                      STAT(PL_MAX, s)
#define PHYS_PL_MIN(s)                      STAT(PL_MIN, s)
#define PHYS_PL_CROUCH_MAX(s)               STAT(PL_CROUCH_MAX, s)
#define PHYS_PL_CROUCH_MIN(s)               STAT(PL_CROUCH_MIN, s)

#define PHYS_PL_VIEWOFS(s)                  STAT(PL_VIEW_OFS, s)
#define PHYS_PL_CROUCH_VIEWOFS(s)           STAT(PL_CROUCH_VIEW_OFS, s)

#define PHYS_VIEWHEIGHT(s)                  STAT(VIEWHEIGHT, s)
#define PHYS_HEALTH(s)                      STAT(HEALTH, s)

#define PHYS_ACCELERATE(s)                  STAT(MOVEVARS_ACCELERATE, s)
#define PHYS_AIRACCELERATE(s)               STAT(MOVEVARS_AIRACCELERATE, s)
#define PHYS_SLICKACCELERATE(s)             STAT(MOVEVARS_SLICKACCELERATE, s)
#define PHYS_AIRACCEL_QW(s)                 STAT(MOVEVARS_AIRACCEL_QW, s)
#define PHYS_AIRACCEL_QW_STRETCHFACTOR(s)   STAT(MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, s)
#define PHYS_AIRACCEL_SIDEWAYS_FRICTION(s)  STAT(MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, s)
#define PHYS_AIRCONTROL(s)                  STAT(MOVEVARS_AIRCONTROL, s)
#define PHYS_AIRCONTROL_FLAGS(s)            STAT(MOVEVARS_AIRCONTROL_FLAGS, s)
#define PHYS_AIRCONTROL_PENALTY(s)          STAT(MOVEVARS_AIRCONTROL_PENALTY, s)
#define PHYS_AIRCONTROL_POWER(s)            STAT(MOVEVARS_AIRCONTROL_POWER, s)
#define PHYS_AIRSPEEDLIMIT_NONQW(s)         STAT(MOVEVARS_AIRSPEEDLIMIT_NONQW, s)
#define PHYS_AIRSTOPACCELERATE(s)           STAT(MOVEVARS_AIRSTOPACCELERATE, s)
#define PHYS_AIRSTOPACCELERATE_FULL(s)      STAT(MOVEVARS_AIRSTOPACCELERATE_FULL, s)
#define PHYS_AIRSTRAFEACCELERATE(s)         STAT(MOVEVARS_AIRSTRAFEACCELERATE, s)
#define PHYS_AIRSTRAFEACCEL_QW(s)           STAT(MOVEVARS_AIRSTRAFEACCEL_QW, s)

#define PHYS_AMMO_FUEL(s)                   STAT(FUEL, s)

#define PHYS_DODGING_FROZEN(s)              STAT(DODGING_FROZEN, s)

#define PHYS_FRICTION(s)                    STAT(MOVEVARS_FRICTION, s)
#define PHYS_FRICTION_ONLAND(s)             STAT(MOVEVARS_FRICTION_ONLAND, s)
#define PHYS_FRICTION_SLICK(s)              STAT(MOVEVARS_FRICTION_SLICK, s)

#define PHYS_FROZEN(s)                      (STAT(FROZEN, s) || StatusEffects_active(STATUSEFFECT_Frozen, s))

#define PHYS_HIGHSPEED(s)                   STAT(MOVEVARS_HIGHSPEED, s)

#define PHYS_JETPACK_ACCEL_SIDE(s)          STAT(JETPACK_ACCEL_SIDE, s)
#define PHYS_JETPACK_ACCEL_UP(s)            STAT(JETPACK_ACCEL_UP, s)
#define PHYS_JETPACK_ANTIGRAVITY(s)         STAT(JETPACK_ANTIGRAVITY, s)
#define PHYS_JETPACK_FUEL(s)                STAT(JETPACK_FUEL, s)
#define PHYS_JETPACK_MAXSPEED_SIDE(s)       STAT(JETPACK_MAXSPEED_SIDE, s)
#define PHYS_JETPACK_MAXSPEED_UP(s)         STAT(JETPACK_MAXSPEED_UP, s)
#define PHYS_JETPACK_REVERSE_THRUST(s)      STAT(JETPACK_REVERSE_THRUST, s)

#define PHYS_JUMPSPEEDCAP_DISABLE_ONRAMPS(s) STAT(MOVEVARS_JUMPSPEEDCAP_DISABLE_ONRAMPS, s)
#define PHYS_JUMPSPEEDCAP_MIN(s)            STAT(MOVEVARS_JUMPSPEEDCAP_MIN, s)
#define PHYS_JUMPSPEEDCAP_MAX(s)            STAT(MOVEVARS_JUMPSPEEDCAP_MAX, s)
#define PHYS_JUMPVELOCITY(s)                STAT(MOVEVARS_JUMPVELOCITY, s)
#define PHYS_JUMPVELOCITY_CROUCH(s)         STAT(MOVEVARS_JUMPVELOCITY_CROUCH, s)

#define PHYS_MAXAIRSPEED(s)                 STAT(MOVEVARS_MAXAIRSPEED, s)
#define PHYS_MAXAIRSTRAFESPEED(s)           STAT(MOVEVARS_MAXAIRSTRAFESPEED, s)
#define PHYS_MAXSPEED(s)                    STAT(MOVEVARS_MAXSPEED, s)

#define PHYS_STOPSPEED(s)                   STAT(MOVEVARS_STOPSPEED, s)

#define PHYS_TRACK_CANJUMP(s)               STAT(MOVEVARS_TRACK_CANJUMP, s)

#define PHYS_WARSOWBUNNY_ACCEL(s)           STAT(MOVEVARS_WARSOWBUNNY_ACCEL, s)
#define PHYS_WARSOWBUNNY_AIRFORWARDACCEL(s) STAT(MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, s)
#define PHYS_WARSOWBUNNY_BACKTOSIDERATIO(s) STAT(MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, s)
#define PHYS_WARSOWBUNNY_TOPSPEED(s)        STAT(MOVEVARS_WARSOWBUNNY_TOPSPEED, s)
#define PHYS_WARSOWBUNNY_TURNACCEL(s)       STAT(MOVEVARS_WARSOWBUNNY_TURNACCEL, s)

#define PHYS_SLICK_APPLYGRAVITY(s)          STAT(SLICK_APPLYGRAVITY)

#define PHYS_INPUT_BUTTON_ATCK(s)           PHYS_INPUT_BUTTON_BUTTON1(s)
#define PHYS_INPUT_BUTTON_JUMP(s)           PHYS_INPUT_BUTTON_BUTTON2(s)
#define PHYS_INPUT_BUTTON_ATCK2(s)          PHYS_INPUT_BUTTON_BUTTON3(s)
#define PHYS_INPUT_BUTTON_ZOOM(s)           PHYS_INPUT_BUTTON_BUTTON4(s)
#define PHYS_INPUT_BUTTON_CROUCH(s)         PHYS_INPUT_BUTTON_BUTTON5(s)
#define PHYS_INPUT_BUTTON_HOOK(s)           PHYS_INPUT_BUTTON_BUTTON6(s)
#define PHYS_INPUT_BUTTON_INFO(s)           PHYS_INPUT_BUTTON_BUTTON7(s) // button7 is FREE
#define PHYS_INPUT_BUTTON_DRAG(s)           PHYS_INPUT_BUTTON_BUTTON8(s)
#define PHYS_INPUT_BUTTON_USE(s)            PHYS_INPUT_BUTTON_BUTTON_USE(s)
#define PHYS_INPUT_BUTTON_CHAT(s)           PHYS_INPUT_BUTTON_BUTTON_CHAT(s)
#define PHYS_INPUT_BUTTON_PRYDON(s)         PHYS_INPUT_BUTTON_BUTTON_PRYDON(s)
#define PHYS_INPUT_BUTTON_ZOOMSCRIPT(s)     PHYS_INPUT_BUTTON_BUTTON9(s)
#define PHYS_INPUT_BUTTON_JETPACK(s)        PHYS_INPUT_BUTTON_BUTTON10(s)
#define PHYS_INPUT_BUTTON_DODGE(s)          PHYS_INPUT_BUTTON_BUTTON11(s)
#define PHYS_INPUT_BUTTON_MINIGAME(s)       PHYS_INPUT_BUTTON_BUTTON12(s)

#ifdef CSQC
STATIC_INIT(PHYS_INPUT_BUTTON)
{
	localcmd("alias +hook +button6\n");
	localcmd("alias -hook -button6\n");

	localcmd("alias +jetpack +button10\n");
	localcmd("alias -jetpack -button10\n");

	localcmd("alias +dodge +button11\n");
	localcmd("alias -dodge -button11\n");
}
#endif

// if more buttons are needed, start using impulse bits as buttons

#define PHYS_INPUT_BUTTON_BACKWARD(s)       (PHYS_INPUT_MOVEVALUES(s).x < 0)
#define PHYS_INPUT_BUTTON_FORWARD(s)        (PHYS_INPUT_MOVEVALUES(s).x > 0)
#define PHYS_INPUT_BUTTON_LEFT(s)           (PHYS_INPUT_MOVEVALUES(s).y < 0)
#define PHYS_INPUT_BUTTON_RIGHT(s)          (PHYS_INPUT_MOVEVALUES(s).y > 0)

// used for special commands and idle checking, not from the engine
// TODO: cache
#define PHYS_INPUT_BUTTON_MASK(s) ( \
	  (BIT(0) * PHYS_INPUT_BUTTON_ATCK(s)) \
	| (BIT(1) * PHYS_INPUT_BUTTON_JUMP(s)) \
	| (BIT(2) * PHYS_INPUT_BUTTON_ATCK2(s)) \
	| (BIT(3) * PHYS_INPUT_BUTTON_ZOOM(s)) \
	| (BIT(4) * PHYS_INPUT_BUTTON_CROUCH(s)) \
	| (BIT(5) * PHYS_INPUT_BUTTON_HOOK(s)) \
	| (BIT(6) * PHYS_INPUT_BUTTON_USE(s)) \
	| (BIT(7) * PHYS_INPUT_BUTTON_BACKWARD(s)) \
	| (BIT(8) * PHYS_INPUT_BUTTON_FORWARD(s)) \
	| (BIT(9) * PHYS_INPUT_BUTTON_LEFT(s)) \
	| (BIT(10) * PHYS_INPUT_BUTTON_RIGHT(s)) \
	)

#define IS_JUMP_HELD(s)                     (!((s).flags & FL_JUMPRELEASED))
#define SET_JUMP_HELD(s)                    ((s).flags &= ~FL_JUMPRELEASED)
#define UNSET_JUMP_HELD(s)                  ((s).flags |= FL_JUMPRELEASED)

#define WAS_ONGROUND(s)                     boolean((s).lastflags & FL_ONGROUND)
#define WAS_ONSLICK(s)                      boolean((s).lastflags & FL_ONSLICK)

#define IS_DUCKED(s)                        (boolean((s).flags & FL_DUCKED))
#define SET_DUCKED(s)                       ((s).flags |= FL_DUCKED)
#define UNSET_DUCKED(s)                     ((s).flags &= ~FL_DUCKED)

#define ITEMS_STAT(s)                       ((s).items)

.float teleport_time;

#ifdef CSQC

	#define PHYS_FIXANGLE(s) ('0 0 0')

	.float watertype;
	.float waterlevel;
	.int items;

	.vector movement;

	// angles of the player's view (as opposed to their model which uses `.vector angles;`) in degrees
	// x is pitch: positive means down (unlike .angles)
	// y is yaw: between -180 and 180, increases when turning left
	// z is roll: positive means tilted clockwise, usually is 0
	// when .fixangle is set, the player's view will change to the direction where the model is facing
	// more info: https://gitlab.com/xonotic/xonotic-data.pk3dir/merge_requests/447#note_32816794
	.vector v_angle;

	.entity hook;

// TODO
	#define IS_CLIENT(s)                        (((s).isplayermodel & ISPLAYER_CLIENT) || (s) == csqcplayer)
	#define IS_PLAYER(s)                        ((s).isplayermodel & ISPLAYER_PLAYER)
	#define IS_NOT_A_CLIENT(s)                  (!(s).isplayermodel && (s) != csqcplayer)
	#define IS_DEAD(s)                          (((s).classname == "ENT_CLIENT_MODEL") ? (s).csqcmodel_isdead : (GetResource((s), RES_HEALTH) <= 0))

	//float player_multijump;
	//float player_jumpheight;

	#define PHYS_INPUT_ANGLES(s)                input_angles
// TODO
	#define PHYS_WORLD_ANGLES(s)                input_angles

	#define PHYS_INPUT_TIMELENGTH               input_timelength
	#define PHYS_INPUT_FRAMETIME                serverdeltatime

	#define PHYS_INPUT_MOVEVALUES(s)            input_movevalues
	#define PHYS_CS(s)                          (s)

	#define PHYS_INPUT_BUTTON_BUTTON1(s)        boolean(input_buttons & BIT(0))
	#define PHYS_INPUT_BUTTON_BUTTON2(s)        boolean(input_buttons & BIT(1))
	#define PHYS_INPUT_BUTTON_BUTTON3(s)        boolean(input_buttons & BIT(2))
	#define PHYS_INPUT_BUTTON_BUTTON4(s)        boolean(input_buttons & BIT(3))
	#define PHYS_INPUT_BUTTON_BUTTON5(s)        boolean(input_buttons & BIT(4))
	#define PHYS_INPUT_BUTTON_BUTTON6(s)        boolean(input_buttons & BIT(5))
	#define PHYS_INPUT_BUTTON_BUTTON7(s)        boolean(input_buttons & BIT(6))
	#define PHYS_INPUT_BUTTON_BUTTON8(s)        boolean(input_buttons & BIT(7))
	#define PHYS_INPUT_BUTTON_BUTTON_USE(s)     boolean(input_buttons & BIT(8))
	#define PHYS_INPUT_BUTTON_BUTTON_CHAT(s)    boolean(input_buttons & BIT(9))
	#define PHYS_INPUT_BUTTON_BUTTON_PRYDON(s)  boolean(input_buttons & BIT(10))
	#define PHYS_INPUT_BUTTON_BUTTON9(s)        boolean(input_buttons & BIT(11))
	#define PHYS_INPUT_BUTTON_BUTTON10(s)       boolean(input_buttons & BIT(12))
	#define PHYS_INPUT_BUTTON_BUTTON11(s)       boolean(input_buttons & BIT(13))
	#define PHYS_INPUT_BUTTON_BUTTON12(s)       boolean(input_buttons & BIT(14))
	#define PHYS_INPUT_BUTTON_BUTTON13(s)       boolean(input_buttons & BIT(15))
	#define PHYS_INPUT_BUTTON_BUTTON14(s)       boolean(input_buttons & BIT(16))
	#define PHYS_INPUT_BUTTON_BUTTON15(s)       boolean(input_buttons & BIT(17))
	#define PHYS_INPUT_BUTTON_BUTTON16(s)       boolean(input_buttons & BIT(18))

	#define PHYS_INVEHICLE(s)                   (boolean(hud != HUD_NORMAL))

	#define PHYS_CL_TRACK_CANJUMP(s)            STAT(MOVEVARS_CL_TRACK_CANJUMP, s)

#elif defined(SVQC)

	#define PHYS_FIXANGLE(s) ((s).fixangle)

	bool Physics_Valid(string thecvar);

	void Physics_UpdateStats(entity this);

	void PM_UpdateButtons(entity this, entity store);

	// footstep interval
	.float nextstep;

	#define PHYS_INPUT_ANGLES(s)                ((s).v_angle)
	#define PHYS_WORLD_ANGLES(s)                ((s).angles)

	#define PHYS_INPUT_TIMELENGTH               frametime
	#define PHYS_INPUT_FRAMETIME                sys_frametime

	#define PHYS_INPUT_MOVEVALUES(s)            CS(s).movement
	#define PHYS_CS(s)                          CS(s)

	#define PHYS_INPUT_BUTTON_BUTTON1(s)        (CS(s).button0)
	#define PHYS_INPUT_BUTTON_BUTTON2(s)        (CS(s).button2)
	#define PHYS_INPUT_BUTTON_BUTTON3(s)        (CS(s).button3)
	#define PHYS_INPUT_BUTTON_BUTTON4(s)        (CS(s).button4)
	#define PHYS_INPUT_BUTTON_BUTTON5(s)        (CS(s).button5)
	#define PHYS_INPUT_BUTTON_BUTTON6(s)        (CS(s).button6)
	#define PHYS_INPUT_BUTTON_BUTTON7(s)        (CS(s).button7)
	#define PHYS_INPUT_BUTTON_BUTTON8(s)        (CS(s).button8)
	#define PHYS_INPUT_BUTTON_BUTTON_USE(s)     (CS(s).buttonuse)
	#define PHYS_INPUT_BUTTON_BUTTON_CHAT(s)    (CS(s).buttonchat)
	#define PHYS_INPUT_BUTTON_BUTTON_PRYDON(s)  (CS(s).cursor_active)
	#define PHYS_INPUT_BUTTON_BUTTON9(s)        (CS(s).button9)
	#define PHYS_INPUT_BUTTON_BUTTON10(s)       (CS(s).button10)
	#define PHYS_INPUT_BUTTON_BUTTON11(s)       (CS(s).button11)
	#define PHYS_INPUT_BUTTON_BUTTON12(s)       (CS(s).button12)
	#define PHYS_INPUT_BUTTON_BUTTON13(s)       (CS(s).button13)
	#define PHYS_INPUT_BUTTON_BUTTON14(s)       (CS(s).button14)
	#define PHYS_INPUT_BUTTON_BUTTON15(s)       (CS(s).button15)
	#define PHYS_INPUT_BUTTON_BUTTON16(s)       (CS(s).button16)

	#define PHYS_INVEHICLE(s)                   (boolean((s).vehicle != NULL))

	#define PHYS_CL_TRACK_CANJUMP(s)            (CS_CVAR(s).cvar_cl_movement_track_canjump)

#endif

#define PHYS_FRICTION_REPLICA_DT                  0.00390625

#ifdef SVQC
// FIXME/EXPLAINME: why? Mario: because
vector autocvar_sv_player_maxs = '16 16 45';
vector autocvar_sv_player_mins = '-16 -16 -24';
vector autocvar_sv_player_viewoffset = '0 0 35';
vector autocvar_sv_player_crouch_maxs = '16 16 25';
vector autocvar_sv_player_crouch_mins = '-16 -16 -24';
vector autocvar_sv_player_crouch_viewoffset = '0 0 20';
//vector autocvar_sv_player_headsize = '24 24 12';
#endif

REGISTER_NET_C2S(setpause)
#ifdef CSQC
void unpause_update()
{
	static bool waspaused;
	bool ispaused = PHYS_INPUT_BUTTON_CHAT(this);
	if (ispaused == waspaused) return;
	waspaused = ispaused;
	// if (!serverispaused) return; // TODO: find out somehow
	if (ispaused) return; // ignore setting pause, server will get those presses anyway, but it won't get releases
	int channel = MSG_C2S;
	WriteHeader(channel, setpause);
	WriteByte(channel, ispaused);
}
#endif
#ifdef SVQC
NET_HANDLE(setpause, bool)
{
	bool ispaused = boolean(ReadByte());
	PHYS_INPUT_BUTTON_CHAT(sender) = ispaused;
	return true;
}
#endif

// used elsewhere, like in strafehud
float GeomLerp(float, float, float);
