#pragma once
#include "defs.qh"

#ifdef SVQC
bool autocvar_g_telefrags;
bool autocvar_g_telefrags_avoid;
bool autocvar_g_telefrags_teamplay;
#endif

IntrusiveList g_teleporters;
STATIC_INIT(g_teleporters) { g_teleporters = IL_NEW(); }

.entity pusher;

const int TELEPORT_FLAG_SOUND = BIT(0);
const int TELEPORT_FLAG_PARTICLES = BIT(1);
const int TELEPORT_FLAG_TDEATH = BIT(2);
const int TELEPORT_FLAG_FORCE_TDEATH = BIT(3);

#define TELEPORT_FLAGS_WARPZONE   0
#define TELEPORT_FLAGS_PORTAL     (TELEPORT_FLAG_SOUND | TELEPORT_FLAG_PARTICLES | TELEPORT_FLAG_TDEATH | TELEPORT_FLAG_FORCE_TDEATH)
#define TELEPORT_FLAGS_TELEPORTER (TELEPORT_FLAG_SOUND | TELEPORT_FLAG_PARTICLES | TELEPORT_FLAG_TDEATH)

.float teleportable;
// types for .teleportable entity setting
const int TELEPORT_NORMAL = 1; // play sounds/effects etc
const int TELEPORT_SIMPLE = 2; // only do teleport, nothing special

entity Simple_TeleportPlayer(entity teleporter, entity player);

void Teleport_Touch(entity this, entity toucher);

void teleport_findtarget(entity this);

entity Teleport_Find(vector mi, vector ma);

void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax, float tflags);

#ifdef SVQC

void trigger_teleport_use(entity this, entity actor, entity trigger);

#define TDEATHLOOP(org) \
	vector deathmin = (org) + player.mins; \
	vector deathmax = (org) + player.maxs; \
	if(telefragmin != telefragmax) \
	{ \
		if(deathmin.x > telefragmin.x) deathmin.x = telefragmin.x; \
		if(deathmin.y > telefragmin.y) deathmin.y = telefragmin.y; \
		if(deathmin.z > telefragmin.z) deathmin.z = telefragmin.z; \
		if(deathmax.x < telefragmax.x) deathmax.x = telefragmax.x; \
		if(deathmax.y < telefragmax.y) deathmax.y = telefragmax.y; \
		if(deathmax.z < telefragmax.z) deathmax.z = telefragmax.z; \
	} \
	for (entity head = findbox_OrFallback(deathmin, deathmax); head; head = head.chain) \
		if (head != player && head.takedamage)

float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax);
float tdeath_hit;
void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax);

void spawn_tdeath(vector v0, entity e, vector v);

void Reset_ArcBeam(entity player, vector forward);

#endif

void WarpZone_PostTeleportPlayer_Callback(entity pl);

#ifdef CSQC
.entity realowner;
#endif
