SBSPSS/source/enemy/npcpath.cpp

269 lines
4.6 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-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++;
}
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-01-16 22:40:14 +01:00
}
}
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-01-16 22:56:29 +01:00
void CNpcPath::setPathType( NPC_PATH_TYPE newPathType )
{
pathType = newPathType;
}
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-01-23 18:03:27 +01:00
s32 CNpcPath::think( DVECTOR currentPos, bool *pathComplete, bool *waypointChange )
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
s32 xDist, yDist;
2001-01-16 22:56:29 +01:00
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-01-26 16:16:07 +01:00
if ( currentWaypoint->isPointNear( currentPos, &xDist, &yDist ) )
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-01-16 22:56:29 +01:00
}
2001-01-18 20:18:39 +01:00
2001-01-19 22:46:30 +01:00
s32 headingToTarget = ratan2( yDist, xDist );
2001-01-18 20:18:39 +01:00
return( headingToTarget );
2001-02-22 16:39:38 +01:00
}
2001-02-22 21:20:01 +01:00
bool CNpcPath::thinkFlat( DVECTOR currentPos, s32 *distX, s32 *distY, s32 *heading )
2001-02-22 16:39:38 +01:00
{
bool pointChange = false;
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( *distX ) < 10 )
{
pointChange = true;
incPath();
}
2001-02-22 17:56:27 +01:00
if ( *distX > 0 )
{
*heading = 0;
}
else
{
*heading = 2048;
}
2001-02-22 16:39:38 +01:00
return( pointChange );
2001-01-16 22:56:29 +01:00
}