#pragma once
/*
 * Globals and Fields
 */

float navigation_bestrating;
float bot_navigation_movemode;
float navigation_testtracewalk;

vector jumpstepheightvec;
vector stepheightvec;
vector jumpheight_vec;
float jumpheight_time;

entity navigation_bestgoal;

// stack of current goals (the last one of which may be an item or other
// desirable object, the rest are typically waypoints to reach it)
.entity goalcurrent, goalstack01, goalstack02, goalstack03;
.entity goalstack04, goalstack05, goalstack06, goalstack07;
.entity goalstack08, goalstack09, goalstack10, goalstack11;
.entity goalstack12, goalstack13, goalstack14, goalstack15;
.entity goalstack16, goalstack17, goalstack18, goalstack19;
.entity goalstack20, goalstack21, goalstack22, goalstack23;
.entity goalstack24, goalstack25, goalstack26, goalstack27;
.entity goalstack28, goalstack29, goalstack30, goalstack31;

.entity goalcurrent_prev;
.float goalcurrent_distance_z;
.float goalcurrent_distance_2d;
.float goalcurrent_distance_time;

// final goal (item, object or player) is also saved in this field
.entity goalentity;
.float goalentity_lock_timeout;
.bool goalentity_shouldbefrozen;

.entity nearestwaypoint;
.float nearestwaypointtimeout;

/*
// item it is linked from waypoint it.wpXX (INCOMING link)
// links are sorted by their cost (wpXXmincost)
// one of these links is added in game every time a bot heads to an item
// even links that are not walkable are added (marked with a high cost)
// so that bots next time know if they can walk it or not saving a tracewalk call
.entity wp00, wp01, wp02, wp03, wp04, wp05, wp06, wp07, wp08, wp09, wp10, wp11, wp12, wp13, wp14, wp15;
.entity wp16, wp17, wp18, wp19, wp20, wp21, wp22, wp23, wp24, wp25, wp26, wp27, wp28, wp29, wp30, wp31;

.float wp00mincost, wp01mincost, wp02mincost, wp03mincost, wp04mincost, wp05mincost, wp06mincost, wp07mincost;
.float wp08mincost, wp09mincost, wp10mincost, wp11mincost, wp12mincost, wp13mincost, wp14mincost, wp15mincost;
.float wp16mincost, wp17mincost, wp18mincost, wp19mincost, wp20mincost, wp21mincost, wp22mincost, wp23mincost;
.float wp24mincost, wp25mincost, wp26mincost, wp27mincost, wp28mincost, wp29mincost, wp30mincost, wp31mincost;
*/

#define navigation_item_initlinks_ifneeded(e) MACRO_BEGIN if (!e.wp00) waypoint_clearlinks(e); MACRO_END // initialize wpXXmincost fields
#define navigation_item_getlinknum(to_item, from_wp) waypoint_getlinknum(to_item, from_wp)
#define navigation_item_iswalkablelink(to_item, from_wp) (waypoint_get_assigned_link_cost(to_item, from_wp) < 999)

#define navigation_item_add_link(from_wp, to_item, walkable) \
	waypoint_addlink_customcost(to_item, from_wp, (walkable ? waypoint_getlinkcost(from_wp, to_item) : 999))

#define TELEPORT_USED(pl, tele_wp) \
	boxesoverlap(tele_wp.absmin, tele_wp.absmax, pl.lastteleport_origin + STAT(PL_MIN, pl), pl.lastteleport_origin + STAT(PL_MAX, pl))

vector tracewalk_dest;
float tracewalk_dest_height;

.entity wp_goal_prev0;
.entity wp_goal_prev1;

.float lastteleporttime;
.vector lastteleport_origin;

.float blacklisted;

.entity navigation_jetpack_goal;
.vector navigation_jetpack_point;

const float DEBUG_NODE_SUCCESS        = 1;
const float DEBUG_NODE_WARNING        = 2;
const float DEBUG_NODE_FAIL           = 3;
vector debuglastnode;

entity bot_waypoint_queue_owner; 	// Owner of the temporary list of goals
entity bot_waypoint_queue_goal;		// Head of the temporary list of goals
.entity bot_waypoint_queue_nextgoal;
entity bot_waypoint_queue_bestgoal;
float bot_waypoint_queue_bestgoalrating;

const float BOT_BUNNYHOP_WP_DETECTION_RANGE = 100;

.entity bot_basewaypoint;
.bool navigation_dynamicgoal;
void navigation_dynamicgoal_init(entity this, bool initially_static);
void navigation_dynamicgoal_set(entity this, entity dropper);
void navigation_dynamicgoal_unset(entity this);

.int nav_submerged_state;
#define SUBMERGED_UNDEFINED 0
#define SUBMERGED_NO 1
#define SUBMERGED_YES 2
bool navigation_check_submerged_state(entity ent, vector pos);


/*
 * Functions
 */

void debugresetnodes();
void debugnode(entity this, vector node);
void debugnodestatus(vector position, float status);

void debuggoalstack(entity this);

float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode);

float navigation_markroutes_nearestwaypoints(entity this, float maxdist);
float navigation_routetogoal(entity this, entity e, vector startposition);

void navigation_clearroute(entity this);
void navigation_pushroute(entity this, entity e);
void navigation_poproute(entity this);
void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p);
void navigation_markroutes(entity this, entity fixed_source_waypoint);
void navigation_markroutes_inverted(entity fixed_source_waypoint);
void navigation_routerating(entity this, entity e, float f, float rangebias);
bool navigation_shortenpath(entity this);
int navigation_poptouchedgoals(entity this);
void navigation_goalrating_start(entity this);
void navigation_goalrating_end(entity this);
void navigation_goalrating_timeout_set(entity this);
void navigation_goalrating_timeout_force(entity this);
bool navigation_goalrating_timeout(entity this);
void navigation_unstuck(entity this);

void botframe_updatedangerousobjects(float maxupdate);

entity navigation_findnearestwaypoint(entity ent, float walkfromwp);
float navigation_waypoint_will_link(vector v, vector org, entity ent, vector v2, float v2_height, vector o2, float o2_height, float walkfromwp, float bestdist);
