2021-02-16 10:36:08 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Copyright (C) 2021 Christoph Oelckers & Mitchell Richters
|
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "gamefuncs.h"
|
2021-02-16 10:48:59 +00:00
|
|
|
#include "gamestruct.h"
|
2021-03-20 16:08:55 +00:00
|
|
|
#include "intvec.h"
|
2021-11-25 21:33:55 +00:00
|
|
|
#include "coreactor.h"
|
2021-02-16 10:36:08 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Unified chasecam function for all games.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int cameradist, cameraclock;
|
|
|
|
|
2021-11-22 23:28:07 +00:00
|
|
|
bool calcChaseCamPos(int* px, int* py, int* pz, spritetype* pspr, sectortype** psect, binangle ang, fixedhoriz horiz, double const smoothratio)
|
2021-02-16 10:36:08 +00:00
|
|
|
{
|
2021-11-25 22:28:28 +00:00
|
|
|
HitInfoBase hitinfo;
|
2021-02-16 10:36:08 +00:00
|
|
|
binangle daang;
|
|
|
|
int newdist;
|
|
|
|
|
2021-11-22 23:28:07 +00:00
|
|
|
if (!*psect) return false;
|
2021-02-16 10:36:08 +00:00
|
|
|
// Calculate new pos to shoot backwards, using averaged values from the big three.
|
2021-02-16 10:48:59 +00:00
|
|
|
int nx = gi->chaseCamX(ang);
|
|
|
|
int ny = gi->chaseCamY(ang);
|
|
|
|
int nz = gi->chaseCamZ(horiz);
|
2021-02-16 10:36:08 +00:00
|
|
|
|
2021-12-18 15:39:44 +00:00
|
|
|
auto bakcstat = pspr->cstat;
|
|
|
|
pspr->cstat &= ~CSTAT_SPRITE_BLOCK_ALL;
|
2021-11-22 23:28:07 +00:00
|
|
|
updatesectorz(*px, *py, *pz, psect);
|
2021-11-25 22:28:28 +00:00
|
|
|
hitscan({ *px, *py, *pz }, *psect, { nx, ny, nz }, hitinfo, CLIPMASK1);
|
2021-02-16 10:36:08 +00:00
|
|
|
pspr->cstat = bakcstat;
|
|
|
|
|
2021-11-25 22:28:28 +00:00
|
|
|
int hx = hitinfo.hitpos.x - *px;
|
|
|
|
int hy = hitinfo.hitpos.y - *py;
|
2021-02-16 10:36:08 +00:00
|
|
|
|
2021-11-22 23:28:07 +00:00
|
|
|
if (*psect == nullptr)
|
2021-02-16 10:36:08 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If something is in the way, make pp->camera_dist lower if necessary
|
|
|
|
if (abs(nx) + abs(ny) > abs(hx) + abs(hy))
|
|
|
|
{
|
2021-11-25 22:28:28 +00:00
|
|
|
if (hitinfo.hitWall != nullptr)
|
2021-02-16 10:36:08 +00:00
|
|
|
{
|
|
|
|
// Push you a little bit off the wall
|
2021-11-25 22:28:28 +00:00
|
|
|
*psect = hitinfo.hitSector;
|
|
|
|
daang = bvectangbam(hitinfo.hitWall->point2Wall()->x - hitinfo.hitWall->x,
|
|
|
|
hitinfo.hitWall->point2Wall()->y - hitinfo.hitWall->y);
|
2021-02-16 10:36:08 +00:00
|
|
|
newdist = nx * daang.bsin() + ny * -daang.bcos();
|
|
|
|
|
|
|
|
if (abs(nx) > abs(ny))
|
|
|
|
hx -= MulScale(nx, newdist, 28);
|
|
|
|
else
|
|
|
|
hy -= MulScale(ny, newdist, 28);
|
|
|
|
}
|
2021-11-25 22:28:28 +00:00
|
|
|
else if (hitinfo.hitActor == nullptr)
|
2021-02-16 10:36:08 +00:00
|
|
|
{
|
|
|
|
// Push you off the ceiling/floor
|
2021-11-25 22:28:28 +00:00
|
|
|
*psect = hitinfo.hitSector;
|
2021-02-16 10:36:08 +00:00
|
|
|
|
|
|
|
if (abs(nx) > abs(ny))
|
|
|
|
hx -= (nx >> 5);
|
|
|
|
else
|
|
|
|
hy -= (ny >> 5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If you hit a sprite that's not a wall sprite - try again.
|
2021-11-25 22:28:28 +00:00
|
|
|
spritetype* hspr = &hitinfo.hitActor->s();
|
2021-02-16 10:36:08 +00:00
|
|
|
|
|
|
|
if (!(hspr->cstat & CSTAT_SPRITE_ALIGNMENT_WALL))
|
|
|
|
{
|
|
|
|
bakcstat = hspr->cstat;
|
|
|
|
hspr->cstat &= ~(CSTAT_SPRITE_BLOCK | CSTAT_SPRITE_BLOCK_HITSCAN);
|
2021-11-22 23:28:07 +00:00
|
|
|
calcChaseCamPos(px, py, pz, pspr, psect, ang, horiz, smoothratio);
|
2021-02-16 10:36:08 +00:00
|
|
|
hspr->cstat = bakcstat;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// same as wall calculation.
|
|
|
|
daang = buildang(pspr->ang - 512);
|
|
|
|
newdist = nx * daang.bsin() + ny * -daang.bcos();
|
|
|
|
|
|
|
|
if (abs(nx) > abs(ny))
|
|
|
|
hx -= MulScale(nx, newdist, 28);
|
|
|
|
else
|
|
|
|
hy -= MulScale(ny, newdist, 28);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (abs(nx) > abs(ny))
|
|
|
|
newdist = DivScale(hx, nx, 16);
|
|
|
|
else
|
|
|
|
newdist = DivScale(hy, ny, 16);
|
|
|
|
|
|
|
|
if (newdist < cameradist)
|
|
|
|
cameradist = newdist;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually move you! (Camerdist is 65536 if nothing is in the way)
|
|
|
|
*px += MulScale(nx, cameradist, 16);
|
|
|
|
*py += MulScale(ny, cameradist, 16);
|
|
|
|
*pz += MulScale(nz, cameradist, 16);
|
|
|
|
|
|
|
|
// Caculate clock using GameTicRate so it increases the same rate on all speed computers.
|
2021-05-11 23:50:41 +00:00
|
|
|
int myclock = PlayClock + MulScale(120 / GameTicRate, int(smoothratio), 16);
|
2021-02-16 10:36:08 +00:00
|
|
|
if (cameraclock == INT_MIN)
|
|
|
|
{
|
|
|
|
// Third person view was just started.
|
|
|
|
cameraclock = myclock;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slowly increase cameradist until it reaches 65536.
|
|
|
|
cameradist = min(cameradist + ((myclock - cameraclock) << 10), 65536);
|
|
|
|
cameraclock = myclock;
|
|
|
|
|
|
|
|
// Make sure psectnum is correct.
|
2021-11-22 23:28:07 +00:00
|
|
|
updatesectorz(*px, *py, *pz, psect);
|
2021-02-16 10:36:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-18 09:19:13 +00:00
|
|
|
|
2021-03-18 11:49:33 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2021-12-17 20:42:57 +00:00
|
|
|
// consolidated slope calculation
|
2021-03-18 11:49:33 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-17 20:42:57 +00:00
|
|
|
void calcSlope(const sectortype* sec, float xpos, float ypos, float* pceilz, float* pflorz)
|
2021-03-18 11:49:33 +00:00
|
|
|
{
|
2021-12-17 20:42:57 +00:00
|
|
|
int bits = 0;
|
|
|
|
if (pceilz)
|
|
|
|
{
|
|
|
|
bits |= sec->ceilingstat;
|
|
|
|
*pceilz = float(sec->ceilingz);
|
|
|
|
}
|
|
|
|
if (pflorz)
|
|
|
|
{
|
|
|
|
bits |= sec->floorstat;
|
|
|
|
*pflorz = float(sec->floorz);
|
|
|
|
}
|
2021-03-18 11:49:33 +00:00
|
|
|
|
2021-12-17 20:42:57 +00:00
|
|
|
if ((bits & CSTAT_SECTOR_SLOPE) == CSTAT_SECTOR_SLOPE)
|
2021-03-18 11:49:33 +00:00
|
|
|
{
|
2021-11-26 19:55:13 +00:00
|
|
|
auto wal = sec->firstWall();
|
2021-12-17 20:42:57 +00:00
|
|
|
int len = wal->Length();
|
|
|
|
if (len != 0)
|
2021-03-18 11:49:33 +00:00
|
|
|
{
|
2021-12-17 21:53:19 +00:00
|
|
|
float fac = (wal->deltax() * (float(ypos - wal->y)) - wal->deltay() * (float(xpos - wal->x))) * (1.f / 256.f) / len;
|
|
|
|
if (pceilz && sec->ceilingstat & CSTAT_SECTOR_SLOPE) *pceilz += (sec->ceilingheinum * fac);
|
|
|
|
if (pflorz && sec->floorstat & CSTAT_SECTOR_SLOPE) *pflorz += (sec->floorheinum * fac);
|
2021-03-18 11:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 11:47:51 +00:00
|
|
|
|
2021-12-17 20:42:57 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// for the renderer (Polymost variants are in polymost.cpp)
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-17 22:33:16 +00:00
|
|
|
void PlanesAtPoint(const sectortype* sec, float dax, float day, float* pceilz, float* pflorz)
|
2021-12-17 20:42:57 +00:00
|
|
|
{
|
|
|
|
calcSlope(sec, dax, day, pceilz, pflorz);
|
|
|
|
if (pceilz) *pceilz *= -(1 / 256.f);
|
|
|
|
if (pflorz) *pflorz *= -(1 / 256.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2021-12-17 22:33:16 +00:00
|
|
|
// for the games (these are not inlined so that they can inline calcSlope)
|
2021-12-17 20:42:57 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int32_t getceilzofslopeptr(usectorptr_t sec, int32_t dax, int32_t day)
|
|
|
|
{
|
|
|
|
float z;
|
|
|
|
calcSlope(sec, dax, day, &z, nullptr);
|
|
|
|
return int(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t getflorzofslopeptr(usectorptr_t sec, int32_t dax, int32_t day)
|
|
|
|
{
|
|
|
|
float z;
|
|
|
|
calcSlope(sec, dax, day, nullptr, &z);
|
|
|
|
return int(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getzsofslopeptr(usectorptr_t sec, int32_t dax, int32_t day, int32_t* ceilz, int32_t* florz)
|
|
|
|
{
|
|
|
|
float c, f;
|
|
|
|
calcSlope(sec, dax, day, &c, &f);
|
|
|
|
*ceilz = int(c);
|
|
|
|
*florz = int(f);
|
|
|
|
}
|
|
|
|
|
2021-12-17 22:33:16 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int getslopeval(sectortype* sect, int x, int y, int z, int basez)
|
|
|
|
{
|
|
|
|
auto wal = sect->firstWall();
|
|
|
|
auto delta = wal->delta();
|
|
|
|
int i = (y - wal->y) * delta.x - (x - wal->x) * delta.y;
|
|
|
|
return i == 0? 0 : Scale((z - basez) << 8, wal->Length(), i);
|
|
|
|
}
|
|
|
|
|
2021-12-17 20:42:57 +00:00
|
|
|
|
2021-03-20 11:47:51 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Calculate the position of a wall sprite in the world
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-04 18:08:50 +00:00
|
|
|
void GetWallSpritePosition(const tspritetype* spr, vec2_t pos, vec2_t* out, bool render)
|
2021-03-20 11:47:51 +00:00
|
|
|
{
|
2021-03-27 12:22:34 +00:00
|
|
|
auto tex = tileGetTexture(spr->picnum);
|
|
|
|
|
|
|
|
int width, leftofs;
|
2021-04-06 13:55:33 +00:00
|
|
|
if (render && hw_hightile && TileFiles.tiledata[spr->picnum].hiofs.xsize)
|
2021-03-27 12:22:34 +00:00
|
|
|
{
|
2021-04-06 13:55:33 +00:00
|
|
|
width = TileFiles.tiledata[spr->picnum].hiofs.xsize;
|
|
|
|
leftofs = (TileFiles.tiledata[spr->picnum].hiofs.xoffs + spr->xoffset);
|
2021-03-27 12:22:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-28 16:22:30 +00:00
|
|
|
width = (int)tex->GetDisplayWidth();
|
|
|
|
leftofs = ((int)tex->GetDisplayLeftOffset() + spr->xoffset);
|
2021-03-27 12:22:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 11:47:51 +00:00
|
|
|
int x = bsin(spr->ang) * spr->xrepeat;
|
|
|
|
int y = -bcos(spr->ang) * spr->xrepeat;
|
|
|
|
|
2021-04-12 13:50:52 +00:00
|
|
|
int xoff = leftofs;
|
2021-03-20 11:47:51 +00:00
|
|
|
if (spr->cstat & CSTAT_SPRITE_XFLIP) xoff = -xoff;
|
|
|
|
int origin = (width >> 1) + xoff;
|
|
|
|
|
|
|
|
out[0].x = pos.x - MulScale(x, origin, 16);
|
|
|
|
out[0].y = pos.y - MulScale(y, origin, 16);
|
|
|
|
out[1].x = out[0].x + MulScale(x, width, 16);
|
|
|
|
out[1].y = out[0].y + MulScale(y, width, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Calculate the position of a wall sprite in the world
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-04 18:08:50 +00:00
|
|
|
template<class sprt>
|
|
|
|
void TGetFlatSpritePosition(const sprt* spr, vec2_t pos, vec2_t* out, bool render)
|
2021-03-20 11:47:51 +00:00
|
|
|
{
|
|
|
|
auto tex = tileGetTexture(spr->picnum);
|
2021-03-26 19:28:44 +00:00
|
|
|
|
2021-03-27 12:22:34 +00:00
|
|
|
int width, height, leftofs, topofs;
|
2021-04-06 13:55:33 +00:00
|
|
|
if (render && hw_hightile && TileFiles.tiledata[spr->picnum].hiofs.xsize)
|
2021-03-26 19:28:44 +00:00
|
|
|
{
|
2021-04-06 13:55:33 +00:00
|
|
|
width = TileFiles.tiledata[spr->picnum].hiofs.xsize * spr->xrepeat;
|
|
|
|
height = TileFiles.tiledata[spr->picnum].hiofs.ysize * spr->yrepeat;
|
|
|
|
leftofs = (TileFiles.tiledata[spr->picnum].hiofs.xoffs + spr->xoffset) * spr->xrepeat;
|
|
|
|
topofs = (TileFiles.tiledata[spr->picnum].hiofs.yoffs + spr->yoffset) * spr->yrepeat;
|
2021-03-26 19:28:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-28 16:22:30 +00:00
|
|
|
width = (int)tex->GetDisplayWidth() * spr->xrepeat;
|
|
|
|
height = (int)tex->GetDisplayHeight() * spr->yrepeat;
|
|
|
|
leftofs = ((int)tex->GetDisplayLeftOffset() + spr->xoffset) * spr->xrepeat;
|
|
|
|
topofs = ((int)tex->GetDisplayTopOffset() + spr->yoffset) * spr->yrepeat;
|
2021-03-26 19:28:44 +00:00
|
|
|
}
|
2021-03-20 11:47:51 +00:00
|
|
|
|
|
|
|
if (spr->cstat & CSTAT_SPRITE_XFLIP) leftofs = -leftofs;
|
|
|
|
if (spr->cstat & CSTAT_SPRITE_YFLIP) topofs = -topofs;
|
|
|
|
|
|
|
|
int sprcenterx = (width >> 1) + leftofs;
|
|
|
|
int sprcentery = (height >> 1) + topofs;
|
|
|
|
|
|
|
|
int cosang = bcos(spr->ang);
|
|
|
|
int sinang = bsin(spr->ang);
|
|
|
|
|
|
|
|
out[0].x = pos.x + DMulScale(sinang, sprcenterx, cosang, sprcentery, 16);
|
|
|
|
out[0].y = pos.y + DMulScale(sinang, sprcentery, -cosang, sprcenterx, 16);
|
|
|
|
|
|
|
|
out[1].x = out[0].x - MulScale(sinang, width, 16);
|
|
|
|
out[1].y = out[0].y + MulScale(cosang, width, 16);
|
|
|
|
|
|
|
|
vec2_t sub = { MulScale(cosang, height, 16), MulScale(sinang, height, 16) };
|
|
|
|
out[2] = out[1] - sub;
|
|
|
|
out[3] = out[0] - sub;
|
|
|
|
}
|
|
|
|
|
2021-12-04 18:08:50 +00:00
|
|
|
void GetFlatSpritePosition(const spritetype* spr, vec2_t pos, vec2_t* out, bool render)
|
|
|
|
{
|
|
|
|
TGetFlatSpritePosition(spr, pos, out, render);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetFlatSpritePosition(const tspritetype* spr, vec2_t pos, vec2_t* out, bool render)
|
|
|
|
{
|
|
|
|
TGetFlatSpritePosition(spr, pos, out, render);
|
|
|
|
}
|
2021-03-20 16:08:55 +00:00
|
|
|
|
2021-03-20 22:01:16 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2021-03-24 18:45:42 +00:00
|
|
|
// Check if some walls are set to use rotated textures.
|
2021-03-20 22:01:16 +00:00
|
|
|
// Ideally this should just have been done with texture rotation,
|
|
|
|
// but the effects on the render code would be too severe due to the alignment mess.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void checkRotatedWalls()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < numwalls; ++i)
|
|
|
|
{
|
|
|
|
if (wall[i].cstat & CSTAT_WALL_ROTATE_90)
|
|
|
|
{
|
|
|
|
auto& w = wall[i];
|
|
|
|
auto& tile = RotTile(w.picnum + animateoffs(w.picnum, 16384));
|
|
|
|
|
|
|
|
if (tile.newtile == -1 && tile.owner == -1)
|
|
|
|
{
|
|
|
|
auto owner = w.picnum + animateoffs(w.picnum, 16384);
|
|
|
|
|
|
|
|
tile.newtile = TileFiles.tileCreateRotated(owner);
|
|
|
|
assert(tile.newtile != -1);
|
|
|
|
|
|
|
|
RotTile(tile.newtile).owner = w.picnum + animateoffs(w.picnum, 16384);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 17:17:45 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// check if two sectors share a wall connection
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool sectorsConnected(int sect1, int sect2)
|
|
|
|
{
|
|
|
|
for (auto& wal : wallsofsector(sect1))
|
|
|
|
{
|
|
|
|
if (wal.nextsector == sect2) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-20 16:08:55 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// vector serializers
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FSerializer& Serialize(FSerializer& arc, const char* key, vec2_t& c, vec2_t* def)
|
|
|
|
{
|
2021-09-11 19:36:29 +00:00
|
|
|
if (arc.isWriting() && def && !memcmp(&c, def, sizeof(c))) return arc;
|
2021-03-20 16:08:55 +00:00
|
|
|
if (arc.BeginObject(key))
|
|
|
|
{
|
|
|
|
arc("x", c.x, def ? &def->x : nullptr)
|
|
|
|
("y", c.y, def ? &def->y : nullptr)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
return arc;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSerializer& Serialize(FSerializer& arc, const char* key, vec3_t& c, vec3_t* def)
|
|
|
|
{
|
2021-09-11 19:36:29 +00:00
|
|
|
if (arc.isWriting() && def && !memcmp(&c, def, sizeof(c))) return arc;
|
2021-03-20 16:08:55 +00:00
|
|
|
if (arc.BeginObject(key))
|
|
|
|
{
|
|
|
|
arc("x", c.x, def ? &def->x : nullptr)
|
|
|
|
("y", c.y, def ? &def->y : nullptr)
|
|
|
|
("z", c.z, def ? &def->z : nullptr)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
return arc;
|
|
|
|
}
|