SBSPSS/source/enemy/npcpath.cpp

362 lines
6.2 KiB
C++
Raw Normal View History

2001-01-16 22:40:14 +01:00
/*=========================================================================
npcpath.cpp
Author: CRB
Created:
Project: Spongebob
Purpose:
Copyright (c) 2000 Climax Development Ltd
===========================================================================*/
2001-01-18 20:18:39 +01:00
#ifndef __ENEMY_NPCPATH_H__
#include "enemy\npcpath.h"
2001-01-16 22:56:29 +01:00
#endif
2001-01-18 20:18:39 +01:00
bool CNpcWaypoint::isPointNear( DVECTOR testPos, s32 *xDist, s32 *yDist )
2001-01-16 22:40:14 +01:00
{
s32 xDistSqr, yDistSqr;
2001-01-19 22:46:30 +01:00
*xDist = this->pos.vx - testPos.vx;
2001-01-18 20:18:39 +01:00
xDistSqr = (*xDist) * (*xDist);
2001-01-16 22:40:14 +01:00
2001-01-19 22:46:30 +01:00
*yDist = this->pos.vy - testPos.vy;
2001-01-18 20:18:39 +01:00
yDistSqr = (*yDist) * (*yDist);
2001-01-16 22:40:14 +01:00
if ( xDistSqr + yDistSqr < 100 )
{
return( true );
}
else
{
return( false );
}
}
2001-01-16 22:56:29 +01:00
void CNpcPath::initPath()
{
2001-01-26 16:16:07 +01:00
waypoint = NULL;
2001-01-16 22:56:29 +01:00
pathType = SINGLE_USE_PATH;
2001-01-26 16:16:07 +01:00
currentWaypoint = NULL;
2001-02-05 15:55:11 +01:00
lastWaypoint = NULL;
2001-01-16 22:56:29 +01:00
waypointCount = 0;
reversePath = false;
2001-04-07 17:39:22 +02:00
minX = maxX = minY = maxY = 0;
2001-01-16 22:56:29 +01:00
}
2001-01-26 16:16:07 +01:00
void CNpcPath::resetPath()
{
2001-01-31 17:14:12 +01:00
currentWaypoint = waypoint;
2001-02-05 15:55:11 +01:00
lastWaypoint = NULL;
2001-01-26 16:16:07 +01:00
}
2001-01-16 22:40:14 +01:00
void CNpcPath::addWaypoint( DVECTOR newPos )
{
2001-01-26 16:16:07 +01:00
CNpcWaypoint *testWaypoint;
CNpcWaypoint *newWaypoint;
testWaypoint = this->waypoint;
if ( testWaypoint )
{
// find end of path
while ( testWaypoint->nextWaypoint )
{
testWaypoint = testWaypoint->nextWaypoint;
}
newWaypoint = new( "waypoint" ) CNpcWaypoint;
newWaypoint->pos = newPos;
newWaypoint->nextWaypoint = NULL;
newWaypoint->prevWaypoint = testWaypoint;
testWaypoint->nextWaypoint = newWaypoint;
waypointCount++;
2001-04-04 20:35:07 +02:00
if ( newPos.vx < minX )
{
minX = newPos.vx;
}
else if ( newPos.vx > maxX )
{
maxX = newPos.vx;
}
2001-04-07 17:39:22 +02:00
if ( newPos.vy < minY )
{
minY = newPos.vy;
}
else if ( newPos.vy > maxY )
{
maxY = newPos.vy;
}
2001-01-26 16:16:07 +01:00
}
else
2001-01-16 22:40:14 +01:00
{
2001-01-26 16:16:07 +01:00
// no waypoints exist in this path, create
newWaypoint = new( "waypoint" ) CNpcWaypoint;
newWaypoint->pos = newPos;
newWaypoint->nextWaypoint = NULL;
newWaypoint->prevWaypoint = NULL;
this->waypoint = newWaypoint;
2001-01-16 22:40:14 +01:00
waypointCount++;
2001-01-30 21:30:52 +01:00
currentWaypoint = this->waypoint;
2001-04-04 20:35:07 +02:00
2001-05-04 00:15:13 +02:00
minX = maxX = newPos.vx;
minY = maxY = newPos.vy;
2001-01-16 22:40:14 +01:00
}
}
2001-04-04 20:35:07 +02:00
void CNpcPath::getPathXExtents( s32 *minExtent, s32 *maxExtent )
{
*minExtent = minX;
*maxExtent = maxX;
}
2001-04-07 17:39:22 +02:00
void CNpcPath::getPathYExtents( s32 *minExtent, s32 *maxExtent )
{
*minExtent = minY;
*maxExtent = maxY;
}
2001-01-26 16:16:07 +01:00
void CNpcPath::removeAllWaypoints()
{
CNpcWaypoint *testWaypoint;
CNpcWaypoint *lastWaypoint;
testWaypoint = this->waypoint;
while ( testWaypoint )
{
lastWaypoint = testWaypoint;
testWaypoint = testWaypoint->nextWaypoint;
delete lastWaypoint;
}
this->waypoint = NULL;
}
2001-04-02 21:21:46 +02:00
void CNpcPath::setPathType( u8 newPathType )
2001-01-16 22:56:29 +01:00
{
2001-04-02 21:21:46 +02:00
pathType = (NPC_PATH_TYPE) newPathType;
2001-01-16 22:56:29 +01:00
}
2001-04-19 01:12:24 +02:00
u8 CNpcPath::getPathType()
{
return( pathType );
}
2001-01-16 22:56:29 +01:00
bool CNpcPath::incPath()
{
if ( !reversePath )
{
2001-01-26 16:16:07 +01:00
if ( currentWaypoint->nextWaypoint )
2001-01-16 22:56:29 +01:00
{
2001-02-05 15:55:11 +01:00
lastWaypoint = currentWaypoint;
2001-01-26 16:16:07 +01:00
currentWaypoint = currentWaypoint->nextWaypoint;
2001-01-16 22:56:29 +01:00
}
else
{
switch( pathType )
{
case SINGLE_USE_PATH:
// path is completed
return( true );
case REPEATING_PATH:
// go back to start
2001-02-05 15:55:11 +01:00
lastWaypoint = currentWaypoint;
2001-01-26 16:16:07 +01:00
currentWaypoint = this->waypoint;
2001-01-16 22:56:29 +01:00
break;
case PONG_PATH:
// reverse path
reversePath = !reversePath;
2001-01-26 16:16:07 +01:00
if ( currentWaypoint->prevWaypoint )
{
2001-02-05 15:55:11 +01:00
lastWaypoint = currentWaypoint;
2001-01-26 16:16:07 +01:00
currentWaypoint = currentWaypoint->prevWaypoint;
}
2001-01-16 22:56:29 +01:00
break;
}
}
}
else
{
// must be pong path if reversed
2001-01-26 16:16:07 +01:00
if ( currentWaypoint->prevWaypoint )
2001-01-16 22:56:29 +01:00
{
2001-02-05 15:55:11 +01:00
lastWaypoint = currentWaypoint;
2001-01-26 16:16:07 +01:00
currentWaypoint = currentWaypoint->prevWaypoint;
2001-01-16 22:56:29 +01:00
}
else
{
reversePath = !reversePath;
2001-01-26 16:16:07 +01:00
if ( currentWaypoint->nextWaypoint )
{
2001-02-05 15:55:11 +01:00
lastWaypoint = currentWaypoint;
2001-01-26 16:16:07 +01:00
currentWaypoint = currentWaypoint->nextWaypoint;
}
2001-01-16 22:56:29 +01:00
}
}
return( false );
}
2001-02-05 15:55:11 +01:00
void CNpcPath::reversePathDir()
{
if ( lastWaypoint )
{
CNpcWaypoint *tempWaypoint;
tempWaypoint = currentWaypoint;
currentWaypoint = lastWaypoint;
lastWaypoint = tempWaypoint;
if ( pathType == PONG_PATH )
{
reversePath = !reversePath;
}
}
}
2001-01-19 22:46:30 +01:00
bool CNpcPath::getDistToNextWaypoint( DVECTOR currentPos, s32 *distX, s32 *distY )
{
2001-01-26 16:16:07 +01:00
return( currentWaypoint->isPointNear( currentPos, distX, distY ) );
2001-01-19 22:46:30 +01:00
}
2001-04-28 19:39:24 +02:00
s32 CNpcPath::think( DVECTOR currentPos, bool *pathComplete, bool *waypointChange, s32 *distX, s32 *distY )
2001-01-16 22:56:29 +01:00
{
2001-01-26 16:16:07 +01:00
if ( !this->waypoint )
{
return( 0 );
}
if ( !currentWaypoint )
{
// if no currentWaypoint set, start it off
currentWaypoint = this->waypoint;
}
2001-01-18 20:18:39 +01:00
*pathComplete = false;
2001-01-23 18:03:27 +01:00
*waypointChange = false;
2001-01-16 22:56:29 +01:00
2001-04-28 19:39:24 +02:00
if ( currentWaypoint->isPointNear( currentPos, distX, distY ) )
2001-01-16 22:56:29 +01:00
{
2001-01-18 20:18:39 +01:00
*pathComplete = incPath();
2001-01-23 18:03:27 +01:00
*waypointChange = true;
2001-04-03 20:32:24 +02:00
2001-04-28 19:39:24 +02:00
*distX = currentWaypoint->pos.vx - currentPos.vx;
*distY = currentWaypoint->pos.vy - currentPos.vy;
2001-01-16 22:56:29 +01:00
}
2001-01-18 20:18:39 +01:00
2001-04-28 19:39:24 +02:00
s32 headingToTarget = ratan2( *distY, *distX );
2001-01-18 20:18:39 +01:00
return( headingToTarget );
2001-02-22 16:39:38 +01:00
}
2001-05-14 17:08:47 +02:00
bool CNpcPath::thinkFlat( DVECTOR currentPos, bool *pathComplete, s32 *distX, s32 *distY, s32 *heading, u8 waypointDist )
2001-02-22 16:39:38 +01:00
{
bool pointChange = false;
2001-04-18 18:34:17 +02:00
*pathComplete = false;
2001-02-22 16:39:38 +01:00
if ( !this->waypoint )
{
return( true );
}
if ( !currentWaypoint )
{
// if no currentWaypoint set, start it off
currentWaypoint = this->waypoint;
}
*distX = currentWaypoint->pos.vx - currentPos.vx;
*distY = currentWaypoint->pos.vy - currentPos.vy;
2001-05-14 17:08:47 +02:00
if ( abs( *distX ) < waypointDist )
2001-02-22 16:39:38 +01:00
{
pointChange = true;
2001-04-18 18:34:17 +02:00
*pathComplete = incPath();
2001-02-22 16:39:38 +01:00
}
2001-04-03 20:32:24 +02:00
*distX = currentWaypoint->pos.vx - currentPos.vx;
*distY = currentWaypoint->pos.vy - currentPos.vy;
2001-02-22 17:56:27 +01:00
if ( *distX > 0 )
{
*heading = 0;
}
else
{
*heading = 2048;
}
2001-04-07 17:39:22 +02:00
return( pointChange );
}
bool CNpcPath::thinkVertical( DVECTOR currentPos, bool *pathComplete, s32 *distX, s32 *distY, s32 *heading )
{
bool pointChange = false;
2001-04-18 18:34:17 +02:00
*pathComplete = false;
2001-04-07 17:39:22 +02:00
if ( !this->waypoint )
{
return( true );
}
if ( !currentWaypoint )
{
// if no currentWaypoint set, start it off
currentWaypoint = this->waypoint;
}
*distX = currentWaypoint->pos.vx - currentPos.vx;
*distY = currentWaypoint->pos.vy - currentPos.vy;
if ( abs( *distY ) < 10 )
{
pointChange = true;
*pathComplete = incPath();
}
*distX = currentWaypoint->pos.vx - currentPos.vx;
*distY = currentWaypoint->pos.vy - currentPos.vy;
if ( *distY > 0 )
{
*heading = 1024;
}
else
{
*heading = 3072;
}
2001-02-22 16:39:38 +01:00
return( pointChange );
2001-04-19 01:12:24 +02:00
}
CNpcWaypoint *CNpcPath::getWaypointList()
{
return( waypoint );
}