game-source/fbxa/bot_way.qc

509 lines
11 KiB
C++
Raw Normal View History

/***********************************************
* *
* FrikBot Waypoints *
* "The better than roaming AI" *
* *
***********************************************/
/*
This program is in the Public Domain. My crack legal
team would like to add:
RYAN "FRIKAC" SMITH IS PROVIDING THIS SOFTWARE "AS IS"
AND MAKES NO WARRANTY, EXPRESS OR IMPLIED, AS TO THE
ACCURACY, CAPABILITY, EFFICIENCY, MERCHANTABILITY, OR
FUNCTIONING OF THIS SOFTWARE AND/OR DOCUMENTATION. IN
NO EVENT WILL RYAN "FRIKAC" SMITH BE LIABLE FOR ANY
GENERAL, CONSEQUENTIAL, INDIRECT, INCIDENTAL,
EXEMPLARY, OR SPECIAL DAMAGES, EVEN IF RYAN "FRIKAC"
SMITH HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES, IRRESPECTIVE OF THE CAUSE OF SUCH DAMAGES.
You accept this software on the condition that you
indemnify and hold harmless Ryan "FrikaC" Smith from
any and all liability or damages to third parties,
including attorney fees, court costs, and other
related costs and expenses, arising out of your use
of this software irrespective of the cause of said
liability.
The export from the United States or the subsequent
reexport of this software is subject to compliance
with United States export control and munitions
control restrictions. You agree that in the event you
seek to export this software, you assume full
responsibility for obtaining all necessary export
licenses and approvals and for assuring compliance
with applicable reexport restrictions.
Any reproduction of this software must contain
this notice in its entirety.
*/
#include "libfrikbot.h"
#include "Array.h"
#include "List.h"
integer bot_way_linker;
2003-07-24 08:25:04 +00:00
@implementation Bot (Way)
2003-07-24 18:25:46 +00:00
-(void)deleteWaypoint:(Waypoint)what
{
2003-07-25 22:38:18 +00:00
if (current_way == what)
current_way = NIL;
[waypoint_array removeItem:what];
[what release];
2003-07-24 08:25:04 +00:00
}
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
FindRoute & FindThing used by the pathing code
in bot_ai.qc
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
2003-07-24 08:25:04 +00:00
-(entity)findThing:(string)s
{
local entity t;
local float tdst, dst;
local entity best;
dst = 100000;
best = NIL;
t = find (NIL, classname, s);
2003-03-04 18:26:17 +00:00
while (t != NIL) {
2003-07-23 22:44:15 +00:00
tdst = vlen(((t.absmin + t.absmax) * 0.5) - ent.origin);
2003-03-04 18:26:17 +00:00
if (tdst < dst) {
dst = tdst;
best = t;
}
t = find(t, classname, s);
}
return best;
2003-07-24 08:25:04 +00:00
}
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
FindRoute, this is a key function in the
pathing. The name is a bit misleading, this
code finds the closest waypoint that is part
of a route calculated by the beginRoute and
end_route routines This is a definite path to
an object.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
2003-07-24 18:25:46 +00:00
-(Waypoint)findRoute:(Waypoint)lastone
{
// kinda like FindWaypoint, only of this bots route though
2003-07-24 18:25:46 +00:00
local Waypoint t, best;
2003-07-24 08:25:04 +00:00
local float dst, tdst;
local integer flag;
local integer count, i;
2003-07-24 08:25:04 +00:00
flag = b_clientflag;
dst = 100000;
best = NIL;
count = [waypoint_array count];
for (i = 0; i < count; i++) {
t = [waypoint_array getItemAt:i];
2003-07-24 08:25:04 +00:00
tdst = vlen(t.origin - ent.origin);
if ((tdst < dst) && (t.bot_bits & flag)) {
2003-07-24 08:25:04 +00:00
if ((lastone == NIL) || ([lastone isLinkedTo:t])) {
dst = tdst;
best = t;
}
}
}
return best;
2003-07-24 08:25:04 +00:00
}
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Mark_path
After the route has been found, mark it with
bitflags so the table can be used for a
different bot.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
-(void)markPath:(Target)this
{
2003-07-24 18:25:46 +00:00
local Waypoint t;
2003-07-24 08:25:04 +00:00
local integer flag;
2003-07-24 18:25:46 +00:00
[Waypoint clearMyRoute:self];
t = [this findWaypoint:this.current_way];
// FIXME
// ugh, better way to find players please!!!
if ([this classname] != "player")
this.current_way = t;
2003-03-04 18:26:17 +00:00
if (t.enemy == NIL) {
2003-07-24 08:25:04 +00:00
[self lost:this :FALSE];
if (waypoint_mode == WM_DYNAMIC)
2003-07-24 08:25:04 +00:00
route_failed = TRUE;
return;
}
flag = b_clientflag;
2003-03-04 18:26:17 +00:00
while(t) {
if (t.bot_bits & flag)
return;
2003-07-24 08:25:04 +00:00
if (t == last_way)
return;
t.bot_bits |= flag;
t = t.enemy;
}
2003-07-24 08:25:04 +00:00
}
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
beginRoute and getPath
PLEASE NOTE: getPath replaces the old
calls to beginRoute.
Routing isn't done all at once now, but in two
stages, the bot will calc a route *THEN*
choose a target, *THEN* mark a path.
Boy it's confusing.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
-(integer)beginRoute
{
if (busy_waypoints > 0)
return FALSE;
2003-07-24 08:25:04 +00:00
if (route_table) {
2003-03-04 18:26:17 +00:00
if (!route_table.ishuman) {
if (route_table.b_clientno != -1)
return FALSE;
}
}
route_table = self;
2003-07-24 18:25:46 +00:00
[Waypoint clearRouteTable];
last_way = [self findWaypoint:current_way];
2003-07-24 08:25:04 +00:00
if (last_way != NIL) {
last_way.distance = vlen(last_way.origin - ent.origin);
[last_way queueForThink];
last_way.busy = TRUE;
busy_waypoints = 1;
return TRUE;
2003-03-04 18:26:17 +00:00
} else {
route_table = NIL;
busy_waypoints = 0;
return FALSE;
}
2003-07-24 08:25:04 +00:00
}
-(void)getPath:(Target)this :(integer)direct
{
if (this == NIL)
return;
2003-03-04 18:26:17 +00:00
if (route_table == self) {
if (busy_waypoints <= 0) {
route_table = NIL;
[self markPath:this];
}
return;
}
2003-03-04 18:26:17 +00:00
if (direct) {
if([self beginRoute])
direct_route = TRUE;
else
2003-07-24 08:25:04 +00:00
[self lost:this :FALSE];
return;
}
2003-07-24 08:25:04 +00:00
}
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Temporary Marker code
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
2003-07-24 08:25:04 +00:00
-(void)spawnTempWaypoint:(vector)org
{
2003-08-01 05:25:07 +00:00
local Waypoint tep;
2003-07-25 22:38:18 +00:00
if (!temp_way)
2003-08-01 05:25:07 +00:00
temp_way = tep = [[Waypoint alloc] init];
else
2003-07-25 22:38:18 +00:00
tep = temp_way;
2003-08-01 05:25:07 +00:00
[tep setOrigin:org];
tep.search_time = 0;
tep.is_temp = 1;
2003-08-01 05:25:07 +00:00
[self targetAdd:tep];
2003-07-24 08:25:04 +00:00
}
/*
Dynamic Waypoint spawning and linking. Not
very good all things considered.
*/
-(void)dynamicWaypoint
{
2003-07-24 18:25:46 +00:00
local Waypoint t;
2003-07-24 08:25:04 +00:00
local float dist, dynlink = 0, dynpoint = 0, editor = 0;
if (teleport_time > portal_time) {
if (!(ent.flags & FL_WATERJUMP)) {
dyn_flags = 2;
if (!ishuman) {
[self lost:targets[0] :TRUE];
ent.enemy = NIL;
}
}
portal_time = teleport_time;
}
// stacking everything on waypoint_mode might've been good for the editor,
// but it sucks to beat hell for this code.
// convert waypoint_mode to something more usable..
if (waypoint_mode > WM_LOADED) {
2003-07-25 22:38:18 +00:00
if (ishuman) {
2003-07-24 08:25:04 +00:00
if (waypoint_mode == WM_EDITOR_DYNLINK)
dynlink = 1;
else if (waypoint_mode == WM_EDITOR_DYNAMIC)
dynlink = dynpoint = 1;
editor = 1;
}
} else if (waypoint_mode == WM_DYNAMIC)
dynlink = dynpoint = 1;
// if there's nothing for dynamic to do..
if (!dynpoint) {
if (!editor)
return;
}
// for speed sake, I won't have bots dynamic waypoint in coop
2003-07-25 22:38:18 +00:00
if (!ishuman)
2003-07-24 08:25:04 +00:00
if (coop)
return;
// don't waypoint in single player
2003-07-24 23:16:43 +00:00
//XXX if (max_clients < 2)
//XXX return;
2003-07-24 08:25:04 +00:00
// if you're dead
else if (ent.health <= 0) {
if (dynpoint) {
if (current_way) {
if (pointcontents(ent.origin) < -4) {
2003-07-25 22:38:18 +00:00
if (current_way.flags & AI_BLIND)
current_way.flags |= AI_PRECISION;
2003-07-24 08:25:04 +00:00
else
2003-07-25 22:38:18 +00:00
current_way.flags |= AI_BLIND;
2003-07-24 08:25:04 +00:00
}
}
}
2003-07-25 22:38:18 +00:00
dyn_dest = '0 0 0';
current_way = NIL;
dyn_flags = 0;
2003-07-24 08:25:04 +00:00
return;
}
// you shouldn't be making waypoints mid air
if (dynpoint) {
if (!((ent.flags & FL_ONGROUND) || ent.waterlevel == 3)) {
if (dyn_flags != 2) {
dyn_flags = 1;
}
return;
}
}
// keep from doing the rest of this every frame
2003-07-25 22:38:18 +00:00
if (dyn_time > time)
2003-07-24 08:25:04 +00:00
return;
2003-07-25 22:38:18 +00:00
dyn_time = time + 0.2;
2003-07-24 08:25:04 +00:00
// display the links for editor mode
if (editor) {
2003-07-25 22:38:18 +00:00
if (current_way) {
local Waypoint way = current_way;
2003-07-24 08:25:04 +00:00
local integer i;
for (i = 0; i < 4; i++) {
if (way.links[i]) {
DeveloperLightning (way, way.links[i],
2003-07-24 08:25:04 +00:00
way.flags & (AI_TELELINK_1 << i));
}
}
}
2003-07-25 22:38:18 +00:00
if (b_aiflags & AI_HOLD_SELECT)
2003-07-24 08:25:04 +00:00
return;
}
2003-07-24 18:25:46 +00:00
t = [self findWaypoint:current_way];
2003-07-24 08:25:04 +00:00
if (t) {
dist = vlen (ent.origin - t.origin);
if (dist < 192) {
if (dist < 64) {
2003-07-25 22:38:18 +00:00
if (t != current_way) {
2003-07-24 08:25:04 +00:00
if (dynlink) {
2003-07-25 22:38:18 +00:00
if (!dyn_flags) {
2003-07-24 08:25:04 +00:00
if ([t canSee:current_way ignoring:ent])
[t linkWay:current_way];
}
if (dyn_flags == 2)
[current_way teleLinkWay:t];
else if ([t canSee:current_way ignoring:ent])
[current_way linkWay:t];
}
if (editor) {
setmodel(t.ent, "progs/s_light.spr");
if (current_way)
setmodel(current_way.ent, "progs/s_bubble.spr");
}
}
2003-07-25 22:38:18 +00:00
current_way = t;
dyn_flags = 0;
2003-07-24 08:25:04 +00:00
}
dyn_dest = ent.origin + ent.view_ofs;
return;
}
}
if ([self recognizePlat:FALSE]) {
2003-07-24 08:25:04 +00:00
if (vlen(trace_ent.velocity) > 0) {
2003-07-25 22:38:18 +00:00
if (dyn_plat)
2003-07-24 08:25:04 +00:00
return;
2003-07-25 22:38:18 +00:00
dyn_plat = TRUE;
if (!dyn_flags)
dyn_flags = 1;
2003-07-24 08:25:04 +00:00
//bprint("on a plat!!!!!\n");
} else
2003-07-25 22:38:18 +00:00
dyn_plat = FALSE;
2003-07-24 08:25:04 +00:00
} else
dyn_plat = FALSE;
if (dyn_flags == 2)
dyn_dest = ent.origin + ent.view_ofs;
2003-07-25 22:38:18 +00:00
else if (dyn_dest == '0 0 0')
2003-07-24 08:25:04 +00:00
dyn_dest = ent.origin + ent.view_ofs;
if (!dynpoint)
return;
2003-07-24 18:25:46 +00:00
t = [[Waypoint alloc] initAt:dyn_dest];
2003-07-24 08:25:04 +00:00
if (!dyn_flags) {
if ([t canSee:current_way ignoring:ent])
[t linkWay:current_way];
}
if (dyn_flags == 2)
[current_way teleLinkWay: t];
else if ([t canSee:current_way ignoring:ent])
[current_way linkWay:t];
if (editor) {
setmodel(t.ent, "progs/s_light.spr");
if (current_way)
setmodel(current_way.ent, "progs/s_bubble.spr");
}
current_way = t;
dyn_flags = 0;
dyn_dest = ent.origin + ent.view_ofs;
if ([self recognizePlat:FALSE]) {
2003-07-24 08:25:04 +00:00
if (trace_ent.classname == "door")
t.flags |= AI_DOORFLAG;
}
}
-(integer)canSee:(Target)targ
{
local float thruwater = 0, pc1 = 0, pc2 = 0;
local vector spot1, org;
org = [targ realorigin];
spot1 = ent.origin + ent.view_ofs;
if (targ.ent.solid == SOLID_BSP) {
traceline (spot1, org, TRUE, ent);
if (trace_ent == targ.ent)
return TRUE;
else if (trace_fraction == 1)
return TRUE;
return FALSE;
} else {
pc1 = pointcontents (org);
pc2 = pointcontents (spot1);
if (targ.ent.classname == "player")
thruwater = FALSE;
else if (pc1 == CONTENT_LAVA)
return FALSE;
else
thruwater = TRUE;
}
if (pc1 < -1) {
// targ's origin is in water or other liquid
if (pc2 != pc1) {
// look for their head
traceline (spot1, org + targ.ent.mins, TRUE, ent);
// cross the water check
if (trace_inopen)
if (trace_inwater)
if (!thruwater)
return FALSE;
if (trace_ent == targ.ent)
return TRUE;
else if (trace_fraction == 1)
return TRUE;
return FALSE;
}
} else {
if (pc2 != pc1) {
traceline (spot1, org + targ.ent.maxs, TRUE, ent);
if (trace_inopen)
if (trace_inwater)
if (!thruwater)
return FALSE;
if (trace_ent == targ.ent)
return TRUE;
else if (trace_fraction == 1)
return TRUE;
return FALSE;
}
}
traceline (spot1, org, TRUE, ent);
if (trace_ent == targ.ent)
return TRUE;
else if (trace_fraction == 1)
return TRUE;
traceline (spot1, org + targ.ent.maxs, TRUE, ent);
if (trace_ent == targ.ent)
return TRUE;
else if (trace_fraction == 1)
return TRUE;
traceline (spot1, org + targ.ent.mins, TRUE, ent);
if (trace_ent == targ.ent)
return TRUE;
else if (trace_fraction == 1)
return TRUE;
return FALSE;
}
2003-07-24 08:25:04 +00:00
@end