2014-03-15 16:59:03 +00:00
|
|
|
// SONIC ROBO BLAST 2
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-18 00:42:11 +00:00
|
|
|
// Copyright (C) 2007-2016 by John "JTE" Muniz.
|
2023-03-31 12:53:31 +00:00
|
|
|
// Copyright (C) 2011-2023 by Sonic Team Junior.
|
2014-03-15 16:59:03 +00:00
|
|
|
//
|
|
|
|
// This program is free software distributed under the
|
|
|
|
// terms of the GNU General Public License, version 2.
|
|
|
|
// See the 'LICENSE' file for more details.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/// \file b_bot.c
|
|
|
|
/// \brief Basic bot handling
|
|
|
|
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "d_player.h"
|
|
|
|
#include "g_game.h"
|
|
|
|
#include "r_main.h"
|
|
|
|
#include "p_local.h"
|
|
|
|
#include "b_bot.h"
|
|
|
|
#include "lua_hook.h"
|
2022-01-18 13:01:26 +00:00
|
|
|
#include "i_system.h" // I_BaseTiccmd
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
void B_UpdateBotleader(player_t *player)
|
2019-11-18 05:30:07 +00:00
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
UINT32 i;
|
|
|
|
fixed_t dist;
|
|
|
|
fixed_t neardist = INT32_MAX;
|
|
|
|
player_t *nearplayer = NULL;
|
|
|
|
//Find new botleader
|
|
|
|
for (i = 0; i < MAXPLAYERS; i++)
|
|
|
|
{
|
|
|
|
if (players[i].bot || players[i].playerstate != PST_LIVE || players[i].spectator || !players[i].mo)
|
|
|
|
continue;
|
2022-11-12 01:38:52 +00:00
|
|
|
|
2021-12-26 12:41:49 +00:00
|
|
|
if (!player->botleader)
|
2021-01-23 15:25:35 +00:00
|
|
|
{
|
2021-12-26 12:41:49 +00:00
|
|
|
player->botleader = &players[i]; // set default
|
2021-01-23 15:25:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-26 12:41:49 +00:00
|
|
|
|
|
|
|
if (!player->mo)
|
|
|
|
return;
|
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
//Update best candidate based on nearest distance
|
|
|
|
dist = R_PointToDist2(player->mo->x, player->mo->y, players[i].mo->x, players[i].mo->y);
|
|
|
|
if (neardist > dist)
|
|
|
|
{
|
|
|
|
neardist = dist;
|
|
|
|
nearplayer = &players[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Set botleader to best candidate (or null if none available)
|
|
|
|
player->botleader = nearplayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void B_ResetAI(botmem_t *mem)
|
|
|
|
{
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
|
|
|
mem->catchup_tics = 0;
|
2019-11-18 05:30:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 17:12:26 +00:00
|
|
|
static void B_BuildTailsTiccmd(mobj_t *sonic, mobj_t *tails, ticcmd_t *cmd)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
|
|
|
boolean forward=false, backward=false, left=false, right=false, jump=false, spin=false;
|
2019-11-06 05:59:53 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
player_t *player = sonic->player, *bot = tails->player;
|
|
|
|
ticcmd_t *pcmd = &player->cmd;
|
2021-01-23 15:25:35 +00:00
|
|
|
botmem_t *mem = &bot->botmem;
|
|
|
|
boolean water = (tails->eflags & MFE_UNDERWATER);
|
2019-11-18 23:55:21 +00:00
|
|
|
SINT8 flip = P_MobjFlip(tails);
|
2019-10-03 04:49:46 +00:00
|
|
|
boolean _2d = (tails->flags2 & MF2_TWOD) || twodlevel;
|
|
|
|
fixed_t scale = tails->scale;
|
2021-01-23 15:25:35 +00:00
|
|
|
boolean jump_last = (bot->lastbuttons & BT_JUMP);
|
|
|
|
boolean spin_last = (bot->lastbuttons & BT_SPIN);
|
2019-11-06 05:59:53 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
fixed_t dist = P_AproxDistance(sonic->x - tails->x, sonic->y - tails->y);
|
|
|
|
fixed_t zdist = flip * (sonic->z - tails->z);
|
2019-11-30 16:54:51 +00:00
|
|
|
angle_t ang = sonic->angle;
|
2019-10-03 04:49:46 +00:00
|
|
|
fixed_t pmom = P_AproxDistance(sonic->momx, sonic->momy);
|
|
|
|
fixed_t bmom = P_AproxDistance(tails->momx, tails->momy);
|
2021-01-23 15:25:35 +00:00
|
|
|
fixed_t followmax = 128 * 8 * scale; // Max follow distance before AI begins to enter catchup state
|
2019-11-06 05:59:53 +00:00
|
|
|
fixed_t followthres = 92 * scale; // Distance that AI will try to reach
|
2019-10-03 04:49:46 +00:00
|
|
|
fixed_t followmin = 32 * scale;
|
|
|
|
fixed_t comfortheight = 96 * scale;
|
|
|
|
fixed_t touchdist = 24 * scale;
|
2019-11-06 05:59:53 +00:00
|
|
|
boolean stalled = (bmom < scale >> 1) && dist > followthres; // Helps to see if the AI is having trouble catching up
|
2019-11-30 16:54:51 +00:00
|
|
|
boolean samepos = (sonic->x == tails->x && sonic->y == tails->y);
|
2021-01-23 15:25:35 +00:00
|
|
|
boolean blocked = bot->blocked;
|
2022-11-12 01:38:52 +00:00
|
|
|
|
2019-11-30 16:54:51 +00:00
|
|
|
if (!samepos)
|
|
|
|
ang = R_PointToAngle2(tails->x, tails->y, sonic->x, sonic->y);
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
// Lua can handle it!
|
2020-11-17 12:14:45 +00:00
|
|
|
if (LUA_HookBotAI(sonic, tails, cmd))
|
2014-03-15 16:59:03 +00:00
|
|
|
return;
|
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
// We can't follow Sonic if he's not around!
|
|
|
|
if (!sonic || sonic->health <= 0)
|
|
|
|
{
|
|
|
|
mem->thinkstate = AI_STANDBY;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (mem->thinkstate == AI_STANDBY)
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
|
|
|
|
2016-09-23 22:48:48 +00:00
|
|
|
if (tails->player->powers[pw_carry] == CR_MACESPIN || tails->player->powers[pw_carry] == CR_GENERIC)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2016-09-23 22:48:48 +00:00
|
|
|
boolean isrelevant = (sonic->player->powers[pw_carry] == CR_MACESPIN || sonic->player->powers[pw_carry] == CR_GENERIC);
|
|
|
|
if (sonic->player->cmd.buttons & BT_JUMP && (sonic->player->pflags & PF_JUMPED) && isrelevant)
|
2014-03-15 16:59:03 +00:00
|
|
|
cmd->buttons |= BT_JUMP;
|
2016-09-23 22:48:48 +00:00
|
|
|
if (isrelevant)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
|
|
|
cmd->forwardmove = sonic->player->cmd.forwardmove;
|
2016-03-30 15:47:27 +00:00
|
|
|
cmd->angleturn = abs((signed)(tails->angle - sonic->angle))>>16;
|
2014-03-15 16:59:03 +00:00
|
|
|
if (sonic->angle < tails->angle)
|
|
|
|
cmd->angleturn = -cmd->angleturn;
|
|
|
|
} else if (dist > FixedMul(512*FRACUNIT, tails->scale))
|
|
|
|
cmd->buttons |= BT_JUMP;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-31 12:53:31 +00:00
|
|
|
// Adapted from clairebun's tails_AI.wad
|
2019-11-06 05:59:53 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// Check water
|
|
|
|
if (water)
|
|
|
|
{
|
|
|
|
followmin = 0;
|
|
|
|
followthres = 16*scale;
|
|
|
|
followmax >>= 1;
|
2021-01-23 15:25:35 +00:00
|
|
|
if (mem->thinkstate == AI_THINKFLY)
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2019-11-06 05:59:53 +00:00
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
// Update catchup_tics
|
|
|
|
if (mem->thinkstate == AI_SPINFOLLOW)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
2021-12-12 07:52:27 +00:00
|
|
|
mem->catchup_tics = 0;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2019-11-06 05:59:53 +00:00
|
|
|
else if (dist > followmax || zdist > comfortheight || stalled)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
2021-12-12 07:52:27 +00:00
|
|
|
mem->catchup_tics = min(mem->catchup_tics + 2, 70);
|
|
|
|
if (mem->catchup_tics >= 70)
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_CATCHUP;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
else
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
2021-12-12 07:52:27 +00:00
|
|
|
mem->catchup_tics = max(mem->catchup_tics - 1, 0);
|
2021-01-23 15:25:35 +00:00
|
|
|
if (mem->thinkstate == AI_CATCHUP)
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
2019-11-06 05:59:53 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// Orientation
|
2021-01-23 15:25:35 +00:00
|
|
|
// cmd->angleturn won't be relative to player angle, since we're not going through G_BuildTiccmd.
|
2019-12-31 05:58:58 +00:00
|
|
|
if (bot->pflags & (PF_SPINNING|PF_STARTDASH))
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
cmd->angleturn = (sonic->angle) >> 16; // NOT FRACBITS DAMNIT
|
2019-12-31 05:58:58 +00:00
|
|
|
}
|
2021-01-23 15:25:35 +00:00
|
|
|
else if (mem->thinkstate == AI_FLYCARRY)
|
2019-12-31 05:58:58 +00:00
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
cmd->angleturn = sonic->player->cmd.angleturn;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
cmd->angleturn = (ang) >> 16; // NOT FRACBITS DAMNIT
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2019-11-06 05:59:53 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// ********
|
|
|
|
// FLY MODE
|
2021-01-23 15:25:35 +00:00
|
|
|
// exiting check
|
|
|
|
if (player->exiting && mem->thinkstate == AI_THINKFLY)
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Activate co-op flight
|
2021-01-23 15:25:35 +00:00
|
|
|
if (mem->thinkstate == AI_THINKFLY && player->pflags & PF_JUMPED)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
if (!jump_last)
|
|
|
|
{
|
|
|
|
jump = true;
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_FLYSTANDBY;
|
2022-01-18 13:01:26 +00:00
|
|
|
bot->pflags |= PF_CANCARRY;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 05:59:53 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// Check positioning
|
|
|
|
// Thinker for co-op flight
|
|
|
|
if (!(water || pmom || bmom)
|
2019-11-30 16:54:51 +00:00
|
|
|
&& (dist < touchdist && !samepos)
|
2019-10-03 04:49:46 +00:00
|
|
|
&& !(pcmd->forwardmove || pcmd->sidemove || player->dashspeed)
|
|
|
|
&& P_IsObjectOnGround(sonic) && P_IsObjectOnGround(tails)
|
2019-11-18 05:37:09 +00:00
|
|
|
&& !(player->pflags & PF_STASIS)
|
2023-01-07 00:41:35 +00:00
|
|
|
&& !(player->exiting)
|
2019-11-18 05:37:09 +00:00
|
|
|
&& bot->charability == CA_FLY)
|
2022-01-18 13:01:26 +00:00
|
|
|
mem->thinkstate = AI_THINKFLY;
|
2021-01-23 15:25:35 +00:00
|
|
|
else if (mem->thinkstate == AI_THINKFLY)
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-11-18 03:22:27 +00:00
|
|
|
|
2019-12-01 13:31:33 +00:00
|
|
|
// Set carried state
|
|
|
|
if (player->powers[pw_carry] == CR_PLAYER && sonic->tracer == tails)
|
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_FLYCARRY;
|
2019-12-01 13:31:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// Ready for takeoff
|
2021-01-23 15:25:35 +00:00
|
|
|
if (mem->thinkstate == AI_FLYSTANDBY)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
if (zdist < -64*scale || (flip * tails->momz) > scale) // Make sure we're not too high up
|
|
|
|
spin = true;
|
|
|
|
else if (!jump_last)
|
|
|
|
jump = true;
|
2019-12-06 18:49:42 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// Abort if the player moves away or spins
|
|
|
|
if (dist > followthres || player->dashspeed)
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2019-11-18 03:22:27 +00:00
|
|
|
// Read player inputs while carrying
|
2021-01-23 15:25:35 +00:00
|
|
|
else if (mem->thinkstate == AI_FLYCARRY)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
cmd->forwardmove = pcmd->forwardmove;
|
|
|
|
cmd->sidemove = pcmd->sidemove;
|
2020-06-06 09:36:34 +00:00
|
|
|
if (pcmd->buttons & BT_SPIN)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
spin = true;
|
|
|
|
jump = false;
|
|
|
|
}
|
|
|
|
else if (!jump_last)
|
|
|
|
jump = true;
|
|
|
|
// End flymode
|
2019-11-18 07:55:55 +00:00
|
|
|
if (player->powers[pw_carry] != CR_PLAYER)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 03:22:27 +00:00
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
if (P_IsObjectOnGround(tails) && !(pcmd->buttons & BT_JUMP) && (mem->thinkstate == AI_FLYSTANDBY || mem->thinkstate == AI_FLYCARRY))
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-11-18 03:22:27 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// ********
|
|
|
|
// SPINNING
|
2021-01-23 15:25:35 +00:00
|
|
|
if (!(player->pflags & (PF_SPINNING|PF_STARTDASH)) && mem->thinkstate == AI_SPINFOLLOW)
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
|
|
|
else if (mem->thinkstate == AI_FOLLOW || mem->thinkstate == AI_SPINFOLLOW)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
if (!_2d)
|
|
|
|
{
|
|
|
|
// Spindash
|
|
|
|
if (player->dashspeed)
|
|
|
|
{
|
|
|
|
if (dist < followthres && dist > touchdist) // Do positioning
|
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
cmd->angleturn = (ang) >> 16; // NOT FRACBITS DAMNIT
|
2019-10-03 04:49:46 +00:00
|
|
|
cmd->forwardmove = 50;
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_SPINFOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2019-10-04 19:02:27 +00:00
|
|
|
else if (dist < touchdist)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
2019-10-04 19:02:27 +00:00
|
|
|
if (!bmom && (!(bot->pflags & PF_SPINNING) || (bot->dashspeed && bot->pflags & PF_SPINNING)))
|
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
cmd->angleturn = (sonic->angle) >> 16; // NOT FRACBITS DAMNIT
|
2019-10-04 19:02:27 +00:00
|
|
|
spin = true;
|
|
|
|
}
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_SPINFOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
|
|
|
else
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
|
|
|
// Spin
|
|
|
|
else if (player->dashspeed == bot->dashspeed && player->pflags & PF_SPINNING)
|
|
|
|
{
|
|
|
|
if (bot->pflags & PF_SPINNING || !spin_last)
|
|
|
|
{
|
|
|
|
spin = true;
|
2021-01-23 15:25:35 +00:00
|
|
|
cmd->angleturn = (sonic->angle) >> 16; // NOT FRACBITS DAMNIT
|
2019-10-03 04:49:46 +00:00
|
|
|
cmd->forwardmove = MAXPLMOVE;
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_SPINFOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
|
|
|
else
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 2D mode
|
|
|
|
else
|
|
|
|
{
|
2019-11-18 03:22:27 +00:00
|
|
|
if (((player->dashspeed && !bmom) || (player->dashspeed == bot->dashspeed && (player->pflags & PF_SPINNING)))
|
|
|
|
&& ((bot->pflags & PF_SPINNING) || !spin_last))
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
spin = true;
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->thinkstate = AI_SPINFOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2021-01-23 15:25:35 +00:00
|
|
|
else
|
|
|
|
mem->thinkstate = AI_FOLLOW;
|
2019-10-03 04:49:46 +00:00
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
2019-11-18 03:22:27 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// ********
|
|
|
|
// FOLLOW
|
2021-01-23 15:25:35 +00:00
|
|
|
if (mem->thinkstate == AI_FOLLOW || mem->thinkstate == AI_CATCHUP)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
// Too far
|
2021-01-23 15:25:35 +00:00
|
|
|
if (mem->thinkstate == AI_CATCHUP || dist > followthres)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
if (!_2d)
|
|
|
|
cmd->forwardmove = MAXPLMOVE;
|
|
|
|
else if (sonic->x > tails->x)
|
|
|
|
cmd->sidemove = MAXPLMOVE;
|
|
|
|
else
|
|
|
|
cmd->sidemove = -MAXPLMOVE;
|
|
|
|
}
|
|
|
|
// Within threshold
|
2021-01-23 15:25:35 +00:00
|
|
|
else if (dist > followmin && abs(zdist) < 192*scale)
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
if (!_2d)
|
|
|
|
cmd->forwardmove = FixedHypot(pcmd->forwardmove, pcmd->sidemove);
|
|
|
|
else
|
|
|
|
cmd->sidemove = pcmd->sidemove;
|
|
|
|
}
|
|
|
|
// Below min
|
|
|
|
else if (dist < followmin)
|
|
|
|
{
|
|
|
|
// Copy inputs
|
2021-01-23 15:25:35 +00:00
|
|
|
cmd->angleturn = (sonic->angle) >> 16; // NOT FRACBITS DAMNIT
|
2019-10-03 04:49:46 +00:00
|
|
|
cmd->forwardmove = 8 * pcmd->forwardmove / 10;
|
|
|
|
cmd->sidemove = 8 * pcmd->sidemove / 10;
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 18:49:42 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// ********
|
|
|
|
// JUMP
|
2021-01-23 15:25:35 +00:00
|
|
|
if (mem->thinkstate == AI_FOLLOW || mem->thinkstate == AI_CATCHUP || (mem->thinkstate == AI_SPINFOLLOW && player->pflags & PF_JUMPED))
|
2019-10-03 04:49:46 +00:00
|
|
|
{
|
|
|
|
// Flying catch-up
|
|
|
|
if (bot->pflags & PF_THOKKED)
|
|
|
|
{
|
2019-11-18 03:22:27 +00:00
|
|
|
cmd->forwardmove = min(MAXPLMOVE, (dist/scale)>>3);
|
2019-10-03 04:49:46 +00:00
|
|
|
if (zdist < -64*scale)
|
|
|
|
spin = true;
|
|
|
|
else if (zdist > 0 && !jump_last)
|
|
|
|
jump = true;
|
|
|
|
}
|
2019-11-18 03:22:27 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// Just landed
|
|
|
|
if (tails->eflags & MFE_JUSTHITFLOOR)
|
|
|
|
jump = false;
|
|
|
|
// Start jump
|
2019-11-18 03:22:27 +00:00
|
|
|
else if (!jump_last && !(bot->pflags & PF_JUMPED) //&& !(player->pflags & PF_SPINNING)
|
2019-10-03 04:49:46 +00:00
|
|
|
&& ((zdist > 32*scale && player->pflags & PF_JUMPED) // Following
|
2021-01-23 15:25:35 +00:00
|
|
|
|| (zdist > 64*scale && mem->thinkstate == AI_CATCHUP) // Vertical catch-up
|
2021-12-12 07:52:27 +00:00
|
|
|
|| (stalled && mem->catchup_tics > 20 && bot->powers[pw_carry] == CR_NONE)
|
2019-11-18 03:22:27 +00:00
|
|
|
//|| (bmom < scale>>3 && dist > followthres && !(bot->powers[pw_carry])) // Stopped & not in carry state
|
2019-10-03 04:49:46 +00:00
|
|
|
|| (bot->pflags & PF_SPINNING && !(bot->pflags & PF_JUMPED)))) // Spinning
|
|
|
|
jump = true;
|
|
|
|
// Hold jump
|
2021-01-23 15:25:35 +00:00
|
|
|
else if (bot->pflags & PF_JUMPED && jump_last && tails->momz*flip > 0 && (zdist > 0 || mem->thinkstate == AI_CATCHUP))
|
2014-03-15 16:59:03 +00:00
|
|
|
jump = true;
|
2019-10-03 04:49:46 +00:00
|
|
|
// Start flying
|
2021-01-23 15:25:35 +00:00
|
|
|
else if (bot->pflags & PF_JUMPED && mem->thinkstate == AI_CATCHUP && !jump_last && bot->charability == CA_FLY)
|
2014-03-15 16:59:03 +00:00
|
|
|
jump = true;
|
|
|
|
}
|
2019-11-18 03:22:27 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// ********
|
|
|
|
// HISTORY
|
2021-01-23 15:25:35 +00:00
|
|
|
//jump_last = jump;
|
|
|
|
//spin_last = spin;
|
2019-11-18 03:22:27 +00:00
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
// Turn the virtual keypresses into ticcmd_t.
|
|
|
|
B_KeysToTiccmd(tails, cmd, forward, backward, left, right, false, false, jump, spin);
|
|
|
|
|
|
|
|
// Update our status
|
2021-01-23 15:25:35 +00:00
|
|
|
mem->lastForward = forward;
|
|
|
|
mem->lastBlocked = blocked;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void B_BuildTiccmd(player_t *player, ticcmd_t *cmd)
|
|
|
|
{
|
2022-01-18 13:01:26 +00:00
|
|
|
G_CopyTiccmd(cmd, I_BaseTiccmd(), 1); // empty, or external driver
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
// Can't build a ticcmd if we aren't spawned...
|
|
|
|
if (!player->mo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (player->playerstate == PST_DEAD)
|
|
|
|
{
|
|
|
|
if (B_CheckRespawn(player))
|
|
|
|
cmd->buttons |= BT_JUMP;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bot AI isn't programmed in analog.
|
2019-12-30 20:01:14 +00:00
|
|
|
CV_SetValue(&cv_analog[1], false);
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
// Let Lua scripts build ticcmds
|
2020-12-12 11:06:57 +00:00
|
|
|
if (LUA_HookTiccmd(player, cmd, HOOK(BotTiccmd)))
|
2014-03-15 16:59:03 +00:00
|
|
|
return;
|
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
// Make sure we have a valid main character to follow
|
2022-01-18 13:01:26 +00:00
|
|
|
B_UpdateBotleader(player);
|
2021-01-23 15:25:35 +00:00
|
|
|
if (!player->botleader)
|
2014-03-15 16:59:03 +00:00
|
|
|
return;
|
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
// Single Player Tails AI
|
|
|
|
//B_BuildTailsTiccmd(players[consoleplayer].mo, player->mo, cmd);
|
|
|
|
B_BuildTailsTiccmd(player->botleader->mo, player->mo, cmd);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void B_KeysToTiccmd(mobj_t *mo, ticcmd_t *cmd, boolean forward, boolean backward, boolean left, boolean right, boolean strafeleft, boolean straferight, boolean jump, boolean spin)
|
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
player_t *player = mo->player;
|
2019-10-06 11:13:13 +00:00
|
|
|
// don't try to do stuff if your sonic is in a minecart or something
|
2021-12-12 07:52:27 +00:00
|
|
|
if (player->botleader && player->botleader->powers[pw_carry] && player->botleader->powers[pw_carry] != CR_PLAYER)
|
2019-10-06 11:13:13 +00:00
|
|
|
return;
|
2014-03-15 16:59:03 +00:00
|
|
|
// Turn the virtual keypresses into ticcmd_t.
|
|
|
|
if (twodlevel || mo->flags2 & MF2_TWOD) {
|
2021-01-23 15:25:35 +00:00
|
|
|
if (player->botleader->climbing
|
2014-03-15 16:59:03 +00:00
|
|
|
|| mo->player->pflags & PF_GLIDING) {
|
|
|
|
// Don't mess with bot inputs during these unhandled movement conditions.
|
|
|
|
// The normal AI doesn't use abilities, so custom AI should be sending us exactly what it wants anyway.
|
|
|
|
if (forward)
|
|
|
|
cmd->forwardmove += MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
if (backward)
|
|
|
|
cmd->forwardmove -= MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
if (left || strafeleft)
|
|
|
|
cmd->sidemove -= MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
if (right || straferight)
|
|
|
|
cmd->sidemove += MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
} else {
|
|
|
|
// In standard 2D mode, interpret "forward" as "the way you're facing" and everything else as "the way you're not facing"
|
|
|
|
if (left || right)
|
|
|
|
backward = true;
|
|
|
|
left = right = false;
|
|
|
|
if (forward) {
|
|
|
|
if (mo->angle < ANGLE_90 || mo->angle > ANGLE_270)
|
|
|
|
right = true;
|
|
|
|
else
|
|
|
|
left = true;
|
|
|
|
} else if (backward) {
|
|
|
|
if (mo->angle < ANGLE_90 || mo->angle > ANGLE_270)
|
|
|
|
left = true;
|
|
|
|
else
|
|
|
|
right = true;
|
|
|
|
}
|
|
|
|
if (left || strafeleft)
|
|
|
|
cmd->sidemove -= MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
if (right || straferight)
|
|
|
|
cmd->sidemove += MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-18 19:22:51 +00:00
|
|
|
angle_t angle;
|
2014-03-15 16:59:03 +00:00
|
|
|
if (forward)
|
|
|
|
cmd->forwardmove += MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
if (backward)
|
|
|
|
cmd->forwardmove -= MAXPLMOVE<<FRACBITS>>16;
|
2022-11-12 01:38:52 +00:00
|
|
|
if (left)
|
2014-03-15 16:59:03 +00:00
|
|
|
cmd->angleturn += 1280;
|
|
|
|
if (right)
|
2022-11-12 01:38:52 +00:00
|
|
|
cmd->angleturn -= 1280;
|
2014-03-15 16:59:03 +00:00
|
|
|
if (strafeleft)
|
|
|
|
cmd->sidemove -= MAXPLMOVE<<FRACBITS>>16;
|
|
|
|
if (straferight)
|
|
|
|
cmd->sidemove += MAXPLMOVE<<FRACBITS>>16;
|
2019-12-06 18:49:42 +00:00
|
|
|
|
2019-10-03 04:49:46 +00:00
|
|
|
// cap inputs so the bot can't accelerate faster diagonally
|
2019-11-18 19:22:51 +00:00
|
|
|
angle = R_PointToAngle2(0, 0, cmd->sidemove << FRACBITS, cmd->forwardmove << FRACBITS);
|
2019-11-18 23:52:14 +00:00
|
|
|
{
|
|
|
|
INT32 maxforward = abs(P_ReturnThrustY(NULL, angle, MAXPLMOVE));
|
|
|
|
INT32 maxside = abs(P_ReturnThrustX(NULL, angle, MAXPLMOVE));
|
|
|
|
cmd->forwardmove = max(min(cmd->forwardmove, maxforward), -maxforward);
|
|
|
|
cmd->sidemove = max(min(cmd->sidemove, maxside), -maxside);
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
if (jump)
|
|
|
|
cmd->buttons |= BT_JUMP;
|
|
|
|
if (spin)
|
2020-06-06 09:36:34 +00:00
|
|
|
cmd->buttons |= BT_SPIN;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void B_MoveBlocked(player_t *player)
|
|
|
|
{
|
|
|
|
(void)player;
|
2021-01-23 15:25:35 +00:00
|
|
|
player->blocked = true;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean B_CheckRespawn(player_t *player)
|
|
|
|
{
|
2021-01-23 15:25:35 +00:00
|
|
|
mobj_t *sonic;
|
2014-03-15 16:59:03 +00:00
|
|
|
mobj_t *tails = player->mo;
|
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
//We don't have a main player to spawn to!
|
|
|
|
if (!player->botleader)
|
|
|
|
return false;
|
2022-11-12 01:38:52 +00:00
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
sonic = player->botleader->mo;
|
2014-03-15 16:59:03 +00:00
|
|
|
// We can't follow Sonic if he's not around!
|
|
|
|
if (!sonic || sonic->health <= 0)
|
|
|
|
return false;
|
|
|
|
|
2020-02-12 19:16:23 +00:00
|
|
|
// B_RespawnBot doesn't do anything if the condition above this isn't met
|
|
|
|
{
|
2020-12-12 11:06:57 +00:00
|
|
|
UINT8 shouldForce = LUA_Hook2Mobj(sonic, tails, MOBJ_HOOK(BotRespawn));
|
2020-02-12 19:16:23 +00:00
|
|
|
|
|
|
|
if (P_MobjWasRemoved(sonic) || P_MobjWasRemoved(tails))
|
|
|
|
return (shouldForce == 1); // mobj was removed
|
|
|
|
|
|
|
|
if (shouldForce == 1)
|
|
|
|
return true;
|
|
|
|
else if (shouldForce == 2)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
// Check if Sonic is busy first.
|
|
|
|
// If he's doing any of these things, he probably doesn't want to see us.
|
2017-03-14 18:11:17 +00:00
|
|
|
if (sonic->player->pflags & (PF_GLIDING|PF_SLIDING|PF_BOUNCING)
|
2016-09-23 22:48:48 +00:00
|
|
|
|| (sonic->player->panim != PA_IDLE && sonic->player->panim != PA_WALK)
|
2019-11-18 05:30:07 +00:00
|
|
|
|| (sonic->player->powers[pw_carry] && sonic->player->powers[pw_carry] != CR_PLAYER))
|
2014-03-15 16:59:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Low ceiling, do not want!
|
2019-10-06 14:11:00 +00:00
|
|
|
if (sonic->eflags & MFE_VERTICALFLIP)
|
|
|
|
{
|
|
|
|
if (sonic->z - sonic->floorz < (sonic->player->exiting ? 5 : 2)*sonic->height)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (sonic->ceilingz - sonic->z < (sonic->player->exiting ? 6 : 3)*sonic->height)
|
2014-03-15 16:59:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If you're dead, wait a few seconds to respawn.
|
|
|
|
if (player->playerstate == PST_DEAD) {
|
|
|
|
if (player->deadtimer > 4*TICRATE)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you can't see Sonic, I guess we should?
|
|
|
|
if (!P_CheckSight(sonic, tails) && P_AproxDistance(P_AproxDistance(tails->x-sonic->x, tails->y-sonic->y), tails->z-sonic->z) > FixedMul(1024*FRACUNIT, tails->scale))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void B_RespawnBot(INT32 playernum)
|
|
|
|
{
|
|
|
|
player_t *player = &players[playernum];
|
|
|
|
fixed_t x,y,z;
|
2021-01-23 15:25:35 +00:00
|
|
|
mobj_t *sonic;
|
2014-03-15 16:59:03 +00:00
|
|
|
mobj_t *tails;
|
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
if (!player->botleader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sonic = player->botleader->mo;
|
2014-03-15 16:59:03 +00:00
|
|
|
if (!sonic || sonic->health <= 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
B_ResetAI(&player->botmem);
|
2019-11-18 05:30:07 +00:00
|
|
|
|
2021-01-23 15:25:35 +00:00
|
|
|
player->bot = BOT_2PAI;
|
2014-03-15 16:59:03 +00:00
|
|
|
P_SpawnPlayer(playernum);
|
|
|
|
tails = player->mo;
|
|
|
|
|
|
|
|
x = sonic->x;
|
|
|
|
y = sonic->y;
|
|
|
|
if (sonic->eflags & MFE_VERTICALFLIP) {
|
|
|
|
tails->eflags |= MFE_VERTICALFLIP;
|
2019-10-06 14:11:00 +00:00
|
|
|
z = sonic->z - (512*sonic->scale);
|
2014-03-15 16:59:03 +00:00
|
|
|
if (z < sonic->floorz)
|
|
|
|
z = sonic->floorz;
|
|
|
|
} else {
|
2019-10-06 14:11:00 +00:00
|
|
|
z = sonic->z + sonic->height + (512*sonic->scale);
|
2014-03-15 16:59:03 +00:00
|
|
|
if (z > sonic->ceilingz - sonic->height)
|
|
|
|
z = sonic->ceilingz - sonic->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sonic->flags2 & MF2_OBJECTFLIP)
|
|
|
|
tails->flags2 |= MF2_OBJECTFLIP;
|
|
|
|
if (sonic->flags2 & MF2_TWOD)
|
|
|
|
tails->flags2 |= MF2_TWOD;
|
|
|
|
if (sonic->eflags & MFE_UNDERWATER)
|
|
|
|
tails->eflags |= MFE_UNDERWATER;
|
|
|
|
player->powers[pw_underwater] = sonic->player->powers[pw_underwater];
|
|
|
|
player->powers[pw_spacetime] = sonic->player->powers[pw_spacetime];
|
|
|
|
player->powers[pw_gravityboots] = sonic->player->powers[pw_gravityboots];
|
|
|
|
player->powers[pw_nocontrol] = sonic->player->powers[pw_nocontrol];
|
2018-06-09 17:06:14 +00:00
|
|
|
player->pflags |= PF_AUTOBRAKE|(sonic->player->pflags & PF_DIRECTIONCHAR);
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2022-04-25 19:36:54 +00:00
|
|
|
P_SetOrigin(tails, x, y, z);
|
2014-03-15 16:59:03 +00:00
|
|
|
if (player->charability == CA_FLY)
|
|
|
|
{
|
2015-01-22 15:23:45 +00:00
|
|
|
P_SetPlayerMobjState(tails, S_PLAY_FLY);
|
2014-03-15 16:59:03 +00:00
|
|
|
tails->player->powers[pw_tailsfly] = (UINT16)-1;
|
|
|
|
}
|
|
|
|
else
|
2015-01-22 15:23:45 +00:00
|
|
|
P_SetPlayerMobjState(tails, S_PLAY_FALL);
|
2014-03-15 16:59:03 +00:00
|
|
|
P_SetScale(tails, sonic->scale);
|
|
|
|
tails->destscale = sonic->destscale;
|
|
|
|
}
|
2019-12-31 08:48:29 +00:00
|
|
|
|
|
|
|
void B_HandleFlightIndicator(player_t *player)
|
|
|
|
{
|
2019-12-31 08:55:36 +00:00
|
|
|
mobj_t *tails = player->mo;
|
2022-01-18 13:01:26 +00:00
|
|
|
botmem_t *mem = &player->botmem;
|
2021-12-12 07:52:27 +00:00
|
|
|
boolean shouldExist;
|
|
|
|
|
2019-12-31 08:48:29 +00:00
|
|
|
if (!tails)
|
|
|
|
return;
|
|
|
|
|
2022-01-18 13:01:26 +00:00
|
|
|
shouldExist = (mem->thinkstate == AI_THINKFLY) && player->botleader
|
2021-12-12 07:52:27 +00:00
|
|
|
&& player->bot == BOT_2PAI && player->playerstate == PST_LIVE;
|
|
|
|
|
|
|
|
// check whether the indicator doesn't exist
|
|
|
|
if (P_MobjWasRemoved(tails->hnext))
|
2019-12-31 08:48:29 +00:00
|
|
|
{
|
2021-12-12 07:52:27 +00:00
|
|
|
// if it shouldn't exist, everything is fine
|
|
|
|
if (!shouldExist)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// otherwise, spawn it
|
|
|
|
P_SetTarget(&tails->hnext, P_SpawnMobjFromMobj(tails, 0, 0, 0, MT_OVERLAY));
|
|
|
|
P_SetTarget(&tails->hnext->target, tails);
|
|
|
|
P_SetTarget(&tails->hnext->hprev, tails);
|
|
|
|
P_SetMobjState(tails->hnext, S_FLIGHTINDICATOR);
|
2019-12-31 08:48:29 +00:00
|
|
|
}
|
2021-12-12 07:52:27 +00:00
|
|
|
|
|
|
|
// if the mobj isn't a flight indicator, let's not mess with it
|
|
|
|
if (tails->hnext->type != MT_OVERLAY || (tails->hnext->state != states+S_FLIGHTINDICATOR))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// if it shouldn't exist, remove it
|
|
|
|
if (!shouldExist)
|
2019-12-31 08:48:29 +00:00
|
|
|
{
|
|
|
|
P_RemoveMobj(tails->hnext);
|
|
|
|
P_SetTarget(&tails->hnext, NULL);
|
2021-12-12 07:52:27 +00:00
|
|
|
return;
|
2019-12-31 08:48:29 +00:00
|
|
|
}
|
2021-12-12 07:52:27 +00:00
|
|
|
|
|
|
|
// otherwise, update its visibility
|
|
|
|
if (P_IsLocalPlayer(player->botleader))
|
|
|
|
tails->hnext->flags2 &= ~MF2_DONTDRAW;
|
|
|
|
else
|
|
|
|
tails->hnext->flags2 |= MF2_DONTDRAW;
|
2019-12-31 08:48:29 +00:00
|
|
|
}
|