nuclide/src/botlib/route.qc

220 lines
5.3 KiB
Plaintext

/*
* Copyright (c) 2016-2020 Marco Cawthorne <marco@icculus.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Begin calculating a route.
* The callback function will be called once the route has finished being calculated.
* The route must be memfreed once it is no longer needed.
* The route must be followed in reverse order (ie: the first node that must be reached
* is at index numnodes-1). If no route is available then the callback will be called with no nodes.
*/
int
Route_RoundDistance(float flDist)
{
float r = fabs(flDist) % 2;
if (r == 0)
return (flDist);
if (flDist < 0)
return -(fabs(flDist) - r);
else
return (flDist + 2 - r);
}
/* returns a botinfo point that's nearest to us */
entity
Route_SelectNearest(float type, vector org)
{
entity temp;
int bestrange = COST_INFINITE;
int range;
entity dest = __NULL__;
for (temp = world; (temp = findfloat(temp, ::botinfo, type));) {
range = vlen(temp.origin - org);
if ((range < bestrange) && (temp.solid != SOLID_NOT)) {
bestrange = range;
dest = temp;
}
}
return dest;
}
/* returns a botinfo point belonging to our team */
entity
Route_SelectNearestTeam(float type, vector org, float tt)
{
entity temp;
int bestrange = COST_INFINITE;
int range;
entity dest = __NULL__;
for (temp = world; (temp = findfloat(temp, ::botinfo, type));) {
if (temp.team != tt)
continue;
range = vlen(temp.origin - org);
if ((range < bestrange) && (temp.solid != SOLID_NOT)) {
bestrange = range;
dest = temp;
}
}
return dest;
}
/* returns a botinfo point belonging to the enemy team */
entity
Route_SelectNearestEnemyTeam(float type, vector org, float tt)
{
entity temp;
int bestrange = COST_INFINITE;
int range;
entity dest = __NULL__;
for (temp = world; (temp = findfloat(temp, ::botinfo, type));) {
if (temp.team == tt)
continue;
range = vlen(temp.origin - org);
if ((range < bestrange) && (temp.solid != SOLID_NOT)) {
bestrange = range;
dest = temp;
}
}
return dest;
}
/*
================
Spawn_SelectRandom
================
*/
entity
Route_SelectRandom(string sEntname)
{
static entity eLastSpot;
eLastSpot = find(eLastSpot, classname, sEntname);
return (eLastSpot);
}
/*
================
Route_SelectRandomSpot
================
*/
entity
Route_SelectRandomSpot(void)
{
static entity eLastSpot;
eLastSpot = findfloat(eLastSpot, ::botinfo, BOTINFO_SPAWNPOINT);
return (eLastSpot);
}
void
Bot_RouteCB(entity ent, vector dest, int numnodes, nodeslist_t *nodelist)
{
bot b = (bot)ent;
b.m_iNodes = numnodes;
b.m_iCurNode = numnodes - 1;
b.m_pRoute = nodelist;
b.m_vecLastNode = dest;
//dprint("Bot: Route calculated.\n");
//dprint(sprintf("Bot: # of nodes: %i\n", bot.m_iNodes) );
//dprint(sprintf("Bot: # current node: %i\n", bot.m_iCurNode) );
//dprint(sprintf("Bot: # endpos: %v\n", dest));
}
vector
Route_SelectDestination(bot target)
{
CGameRules rules;
rules = (CGameRules)g_grMode;
entity dest = world;
if (rules.IsTeamPlay()) {
/* we have the goal item, so capture it */
if (target.flags & FL_GOALITEM) {
print(sprintf("%s going for capture\n", target.netname));
dest = Route_SelectNearestTeam(BOTINFO_TEAM_GOALCAPTURE, target.origin, target.team);
/* we may have to go to our teams' goal item then */
if (!dest) {
dest = Route_SelectNearestTeam(BOTINFO_TEAM_GOALITEM, target.origin, target.team);
}
} else {
print(sprintf("%s hunting for goal item\n", target.netname));
dest = Route_SelectNearestEnemyTeam(BOTINFO_TEAM_GOALITEM, target.origin, target.team);
}
if (dest)
return dest.origin + [0,0,32];
print(sprintf("%s can't figure out where to go for the goal\n", target.netname));
}
/* if we're low on health, look for health items */
if (target.health < 50) {
print(sprintf("%s going for health\n", target.netname));
dest = Route_SelectNearest(BOTINFO_HEALTH, target.origin);
if (dest)
return dest.origin + [0,0,32];
print(sprintf("%s can't figure out where to go for health\n", target.netname));
}
/* armor is always a good idea to have */
if (target.armor < 50) {
print(sprintf("%s going for armor\n", target.netname));
dest = Route_SelectNearest(BOTINFO_ARMOR, target.origin);
if (dest)
return dest.origin + [0,0,32];
print(sprintf("%s can't figure out where to go for armor\n", target.netname));
}
/* if all else fails... select a random spot */
print(sprintf("%s found nothing, going for random PoI\n", target.netname));
dest = Route_SelectRandomSpot();
target.m_eDestination = dest;
return (dest.origin);
}
int
Route_GetNodeFlags(nodeslist_t *node)
{
int fl = node.m_iFlags;
/* to avoid random buttons being pressed */
if (fl < 0)
return LF_DESTINATION;
else
return fl;
}