2007-05-30 21:56:52 +00:00
/* ScummVM - Graphic Adventure Engine
2007-04-27 12:58:35 +00:00
*
2007-05-30 21:56:52 +00:00
* ScummVM is the legal property of its developers , whose names
* are too numerous to list here . Please refer to the COPYRIGHT
* file distributed with this source distribution .
2007-04-27 12:58:35 +00:00
*
* This program 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 .
*
* $ URL $
* $ Id $
*
*/
# include "cruise/cruise_main.h"
2007-04-28 04:29:05 +00:00
# include "common/util.h"
2007-04-27 12:58:35 +00:00
namespace Cruise {
2007-04-27 22:33:45 +00:00
ctpVar19Struct * ptr_something ;
ctpVar19Struct * polyStruct ;
ctpVar19Struct * ctpVar11 ;
ctpVar19Struct * ctpVar13 ;
ctpVar19Struct * ctpVar15 ;
2007-04-27 12:58:35 +00:00
2007-04-27 22:33:45 +00:00
uint8 * ctpVar17 ;
ctpVar19Struct * ctpVar19 ;
2007-04-27 12:58:35 +00:00
int currentWalkBoxCenterX ;
int currentWalkBoxCenterY ;
int currentWalkBoxCenterXBis ;
int currentWalkBoxCenterYBis ;
int ctpVarUnk ;
2007-04-27 22:33:45 +00:00
uint8 walkboxTable [ 0x12 ] ;
2007-04-27 12:58:35 +00:00
2007-04-27 22:33:45 +00:00
int ctpProc2 ( int varX , int varY , int paramX , int paramY ) {
2007-04-28 04:29:05 +00:00
int diffX = ABS ( paramX - varX ) ;
int diffY = ABS ( paramY - varY ) ;
2007-04-27 12:58:35 +00:00
2007-04-27 22:33:45 +00:00
if ( diffX > diffY ) {
2007-04-27 12:58:35 +00:00
diffY = diffX ;
}
2007-04-27 22:33:45 +00:00
ctpVar14 = diffY ; // highest difference
return ( diffY ) ;
2007-04-27 12:58:35 +00:00
}
// this function process path finding coordinates
2007-04-27 22:33:45 +00:00
void loadCtpSub2 ( short int coordCount , short int * ptr ) {
// coordCount = ctp_routeCoordCount, ptr = ctpVar8
2007-04-27 12:58:35 +00:00
int i ;
int offset = 0 ;
2007-04-27 22:33:45 +00:00
short int * cur_ctp_routeCoords = ( short int * ) ctp_routeCoords ; // coordinates table
int8 * cur_ctp_routes = ( int8 * ) ctp_routes ;
2007-04-27 12:58:35 +00:00
2007-09-10 13:17:20 +00:00
for ( i = 0 ; i < coordCount ; i + + ) { // for i < ctp_routeCoordCount
2007-04-27 22:33:45 +00:00
int varX = cur_ctp_routeCoords [ 0 ] ; // x
int varY = cur_ctp_routeCoords [ 1 ] ; // y
2007-04-27 12:58:35 +00:00
int di = 0 ;
int var4Offset = 2 ;
2007-09-10 13:17:20 +00:00
while ( * ( int16 * ) cur_ctp_routes > di ) { // while (coordCount > counter++)
2007-04-27 22:33:45 +00:00
int idx = * ( int16 * ) ( cur_ctp_routes + var4Offset ) ;
ptr [ offset + idx ] =
ctpProc2 ( varX , varY , ctp_routeCoords [ idx ] [ 0 ] ,
ctp_routeCoords [ idx * 2 ] [ 1 ] ) ;
2007-04-27 12:58:35 +00:00
var4Offset + = 2 ;
di + + ;
}
offset + = 10 ;
cur_ctp_routes + = 20 ;
cur_ctp_routeCoords + = 2 ;
}
}
2007-04-27 22:33:45 +00:00
void getWalkBoxCenter ( int boxIdx , uint16 * _walkboxTable ) {
2007-04-27 12:58:35 +00:00
int minX = 1000 ;
int minY = 1000 ;
int maxX = - 1 ;
int maxY = - 1 ;
2007-04-27 22:33:45 +00:00
ASSERT ( boxIdx < = 15 ) ; // max number of walkboxes is 16
2007-04-27 18:54:33 +00:00
ASSERT ( _walkboxTable [ boxIdx * 40 ] ) ; // we should never have an empty walk box
2007-04-27 12:58:35 +00:00
2007-04-27 22:33:45 +00:00
if ( _walkboxTable [ boxIdx * 40 ] > 0 ) {
int numPoints = _walkboxTable [ boxIdx * 40 ] ;
uint16 * pCurrentPtr = _walkboxTable + ( boxIdx * 40 ) + 1 ;
2007-04-27 12:58:35 +00:00
int i ;
2007-04-27 22:33:45 +00:00
for ( i = 0 ; i < numPoints ; i + + ) {
2007-04-27 12:58:35 +00:00
int X = * ( pCurrentPtr + + ) ;
int Y = * ( pCurrentPtr + + ) ; ;
2007-04-27 22:33:45 +00:00
if ( X < minX )
2007-04-27 12:58:35 +00:00
minX = X ;
2007-04-27 22:33:45 +00:00
if ( X > maxX )
2007-04-27 12:58:35 +00:00
maxX = X ;
2007-04-27 22:33:45 +00:00
if ( Y < minY )
2007-04-27 12:58:35 +00:00
minY = Y ;
2007-04-27 22:33:45 +00:00
if ( Y > maxY )
2007-04-27 12:58:35 +00:00
maxY = Y ;
}
}
currentWalkBoxCenterX = ( ( maxX - minX ) / 2 ) + minX ;
currentWalkBoxCenterY = ( ( maxY - minY ) / 2 ) + minY ;
}
// ax dx bx
2007-04-27 22:33:45 +00:00
void renderCTPWalkBox ( int X1 , int Y1 , int X2 , int scale , int Y2 ,
uint16 * walkboxData ) {
2007-04-27 12:58:35 +00:00
int numPoints ;
int wbSelf1 ;
int wbSelf2 ;
int i ;
2007-04-27 22:33:45 +00:00
int16 * destination ;
2007-04-27 12:58:35 +00:00
wbSelf1 = upscaleValue ( X1 , scale ) - X2 ;
wbSelf2 = upscaleValue ( Y1 , scale ) - Y2 ;
numPoints = * ( walkboxData + + ) ;
destination = polyBuffer2 ;
2007-04-27 22:33:45 +00:00
for ( i = 0 ; i < numPoints ; i + + ) {
2007-04-27 12:58:35 +00:00
int pointX = * ( walkboxData + + ) ;
int pointY = * ( walkboxData + + ) ;
int scaledX = upscaleValue ( pointX , scale ) - wbSelf1 ;
int scaledY = upscaleValue ( pointY , scale ) - wbSelf2 ;
* ( destination + + ) = scaledX > > 16 ;
* ( destination + + ) = scaledY > > 16 ;
}
m_color = 0 ;
ctpVarUnk = 0 ;
2007-04-27 22:33:45 +00:00
for ( i = 0 ; i < numPoints ; i + + ) {
2007-04-27 12:58:35 +00:00
walkboxTable [ i ] = i ;
}
2007-11-03 22:47:19 +00:00
drawPolyMode2 ( ( unsigned char * ) walkboxTable , numPoints ) ;
2007-04-27 12:58:35 +00:00
}
// this process the walkboxes
2007-04-27 22:33:45 +00:00
void loadCtpSub1 ( int boxIdx , int scale , uint16 * _walkboxTable ,
ctpVar19Struct * param4 ) {
2007-04-27 12:58:35 +00:00
int minX = 1000 ;
int minY = 1000 ;
int maxX = - 1 ;
int maxY = - 1 ;
2007-04-27 22:33:45 +00:00
ctpVar19Struct * var_1C ;
ctpVar19Struct * var_12 ;
2007-11-01 11:22:11 +00:00
// int16 *var_18;
// int16 *si;
2007-04-27 12:58:35 +00:00
// int16* di;
// uint8* cx;
// int bx;
// int ax;
// int var_2;
2007-11-01 11:22:11 +00:00
// int var_E;
2007-04-27 18:54:33 +00:00
//int var_C = 1000;
//int var_A = 0;
2007-04-27 22:33:45 +00:00
ctpVar19SubStruct * subStruct ;
2007-04-27 12:58:35 +00:00
ASSERT ( boxIdx < = 15 ) ;
2007-09-10 13:17:20 +00:00
if ( _walkboxTable [ boxIdx * 40 ] > 0 ) { // is walkbox used ?
2007-04-27 18:54:33 +00:00
getWalkBoxCenter ( boxIdx , _walkboxTable ) ;
2007-04-27 12:58:35 +00:00
currentWalkBoxCenterYBis = currentWalkBoxCenterY ;
currentWalkBoxCenterXBis = currentWalkBoxCenterX ;
// + 512
2007-04-27 22:33:45 +00:00
renderCTPWalkBox ( currentWalkBoxCenterX , currentWalkBoxCenterY ,
currentWalkBoxCenterX , scale + 0x200 ,
currentWalkBoxCenterY , _walkboxTable + boxIdx * 40 ) ;
2007-04-27 12:58:35 +00:00
var_1C = param4 ;
2007-04-27 22:33:45 +00:00
var_12 = var_1C + 1 ; // next
2007-11-01 11:22:11 +00:00
/*
var_18 = XMIN_XMAX ;
2007-04-27 22:33:45 +00:00
var_E = 0 ;
2007-04-27 12:58:35 +00:00
2007-11-01 11:22:11 +00:00
si = & XMIN_XMAX [ 1 ] ;
if ( * si > = 0 )
2007-04-27 22:33:45 +00:00
* {
* di = si ;
* cx = var_12 ;
*
* do
* {
* di + + ;
* bx = di [ - 1 ] ;
* ax = di [ 0 ] ;
* di + + ;
*
* var_2 = ax ;
2007-09-18 20:16:33 +00:00
* if ( var_C < bx )
2007-04-27 22:33:45 +00:00
* {
* var_C = bx ;
* }
*
2007-09-18 20:16:33 +00:00
* if ( var_2 < var_A )
2007-04-27 22:33:45 +00:00
* {
* var_A = var_2 ;
* }
*
* * cx = bx ;
* cx + + ;
* * cx = var_2 ;
* cx + + ;
* var_E + + ;
2007-11-04 13:01:41 +00:00
* } while ( di ) ;
2007-04-27 22:33:45 +00:00
*
* var_12 = cx ;
* } */
2007-04-27 12:58:35 +00:00
/*************/
{
int i ;
int numPoints ;
2007-04-27 22:33:45 +00:00
uint16 * pCurrentPtr = _walkboxTable + boxIdx * 40 ;
2007-04-27 12:58:35 +00:00
numPoints = * ( pCurrentPtr + + ) ;
2007-04-27 22:33:45 +00:00
for ( i = 0 ; i < numPoints ; i + + ) {
2007-04-27 12:58:35 +00:00
int X = * ( pCurrentPtr + + ) ;
int Y = * ( pCurrentPtr + + ) ;
2007-04-27 22:33:45 +00:00
if ( X < minX )
2007-04-27 12:58:35 +00:00
minX = X ;
2007-04-27 22:33:45 +00:00
if ( X > maxX )
2007-04-27 12:58:35 +00:00
maxX = X ;
2007-04-27 22:33:45 +00:00
if ( Y < minY )
2007-04-27 12:58:35 +00:00
minY = Y ;
2007-04-27 22:33:45 +00:00
if ( Y > maxY )
2007-04-27 12:58:35 +00:00
maxY = Y ;
}
}
/************/
var_1C - > field_0 = var_12 ;
2007-04-27 22:33:45 +00:00
ctpVar13 = var_12 ;
var_12 - > field_0 = ( ctpVar19Struct * ) ( - 1 ) ;
2007-04-27 12:58:35 +00:00
subStruct = & var_1C - > subStruct ;
subStruct - > boxIdx = boxIdx ;
2007-04-27 22:33:45 +00:00
subStruct - > type = walkboxType [ boxIdx ] ;
subStruct - > minX = minX ;
subStruct - > maxX = maxX ;
subStruct - > minY = minY ;
subStruct - > maxY = maxY ;
2007-04-27 12:58:35 +00:00
}
}
2007-11-04 01:11:39 +00:00
int getNode ( int nodeResult [ 2 ] , int nodeId ) {
2007-11-04 13:01:41 +00:00
if ( nodeId < 0 | | nodeId > = ctp_routeCoordCount )
2007-11-04 01:11:39 +00:00
return - 1 ;
nodeResult [ 0 ] = ctp_routeCoords [ nodeId ] [ 0 ] ;
nodeResult [ 1 ] = ctp_routeCoords [ nodeId ] [ 1 ] ;
return 0 ;
}
2007-11-10 17:15:48 +00:00
int loadCtp ( const char * ctpName ) {
2007-04-27 22:33:45 +00:00
int walkboxCounter ; // si
uint8 * ptr ;
uint8 * dataPointer ; // ptr2
char fileType [ 5 ] ; // string2
2007-04-27 12:58:35 +00:00
short int segementSizeTable [ 7 ] ; // tempTable
2007-04-27 22:33:45 +00:00
if ( ctpVar1 = = 0 ) {
2007-04-27 12:58:35 +00:00
int i ;
2007-04-27 22:33:45 +00:00
for ( i = 0 ; i < 10 ; i + + ) {
2007-04-27 12:58:35 +00:00
persoTable [ i ] = NULL ;
}
}
2007-04-27 22:33:45 +00:00
if ( ! loadFileSub1 ( & ptr , ctpName , 0 ) ) {
2007-04-27 12:58:35 +00:00
free ( ptr ) ;
2007-04-27 22:33:45 +00:00
return ( - 18 ) ;
2007-04-27 12:58:35 +00:00
}
dataPointer = ptr ;
fileType [ 4 ] = 0 ;
2007-04-27 22:33:45 +00:00
memcpy ( fileType , dataPointer , 4 ) ; // get the file type, first 4 bytes of the CTP file
2007-04-27 12:58:35 +00:00
dataPointer + = 4 ;
2007-04-27 22:33:45 +00:00
if ( strcmp ( fileType , " CTP " ) ) {
2007-04-27 12:58:35 +00:00
free ( ptr ) ;
2007-04-27 22:33:45 +00:00
return ( 0 ) ;
2007-04-27 12:58:35 +00:00
}
2007-04-27 22:33:45 +00:00
memcpy ( & ctp_routeCoordCount , dataPointer , 2 ) ; // get the number of path-finding coordinates
2007-04-27 12:58:35 +00:00
dataPointer + = 2 ;
flipShort ( & ctp_routeCoordCount ) ;
memcpy ( segementSizeTable , dataPointer , 0xE ) ;
2007-04-27 22:33:45 +00:00
dataPointer + = 0xE ; // + 14
2007-04-27 12:58:35 +00:00
flipGen ( segementSizeTable , 0xE ) ;
memcpy ( ctp_routeCoords , dataPointer , segementSizeTable [ 0 ] ) ; // get the path-finding coordinates
dataPointer + = segementSizeTable [ 0 ] ;
flipGen ( ctp_routeCoords , segementSizeTable [ 0 ] ) ;
2007-04-27 22:33:45 +00:00
memcpy ( ctp_routes , dataPointer , segementSizeTable [ 1 ] ) ; // get the path-finding line informations (indexing the routeCoords array)
2007-04-27 12:58:35 +00:00
dataPointer + = segementSizeTable [ 1 ] ;
flipGen ( ctp_routes , segementSizeTable [ 1 ] ) ;
2007-04-27 22:33:45 +00:00
memcpy ( ctp_walkboxTable , dataPointer , segementSizeTable [ 2 ] ) ; // get the walkbox coordinates and lines
2007-04-27 12:58:35 +00:00
dataPointer + = segementSizeTable [ 2 ] ;
flipGen ( ctp_walkboxTable , segementSizeTable [ 2 ] ) ;
2007-04-27 22:33:45 +00:00
if ( ctpVar1 ) {
2007-04-27 12:58:35 +00:00
dataPointer + = segementSizeTable [ 3 ] ;
dataPointer + = segementSizeTable [ 4 ] ;
2007-04-27 22:33:45 +00:00
} else {
2007-04-27 12:58:35 +00:00
memcpy ( walkboxType , dataPointer , segementSizeTable [ 3 ] ) ; // get the walkbox type
dataPointer + = segementSizeTable [ 3 ] ;
2007-04-27 22:33:45 +00:00
flipGen ( walkboxType , segementSizeTable [ 3 ] ) ; // Type: 0x00 - non walkable, 0x01 - walkable, 0x02 - exit zone
2007-04-27 12:58:35 +00:00
memcpy ( walkboxChange , dataPointer , segementSizeTable [ 4 ] ) ; // change indicator, walkbox type can change, i.e. blocked by object (values are either 0x00 or 0x01)
dataPointer + = segementSizeTable [ 4 ] ;
flipGen ( walkboxChange , segementSizeTable [ 4 ] ) ;
}
2007-04-27 22:33:45 +00:00
memcpy ( ctpVar6 , dataPointer , segementSizeTable [ 5 ] ) ; // unknown? always 2*16 bytes (used by S24.CTP, S33.CTP, S33_2.CTP, S34.CTP, S35.CTP, S36.CTP; values can be 0x00, 0x01, 0x03, 0x05)
2007-04-27 12:58:35 +00:00
dataPointer + = segementSizeTable [ 5 ] ;
2007-04-27 22:33:45 +00:00
flipGen ( ctpVar6 , segementSizeTable [ 5 ] ) ;
2007-04-27 12:58:35 +00:00
2007-04-27 22:33:45 +00:00
memcpy ( ctp_scale , dataPointer , segementSizeTable [ 6 ] ) ; // scale values for the walkbox coordinates (don't know why there is a need for scaling walkboxes)
2007-04-27 12:58:35 +00:00
dataPointer + = segementSizeTable [ 6 ] ;
2007-04-27 22:33:45 +00:00
flipGen ( ctp_scale , segementSizeTable [ 6 ] ) ; // ok
2007-04-27 12:58:35 +00:00
free ( ptr ) ;
2007-11-10 17:15:48 +00:00
strcpy ( currentCtpName , ctpName ) ;
2007-04-27 12:58:35 +00:00
2007-04-27 22:33:45 +00:00
numberOfWalkboxes = segementSizeTable [ 6 ] / 2 ; // get the number of walkboxes
2007-04-27 12:58:35 +00:00
2007-04-27 22:33:45 +00:00
loadCtpSub2 ( ctp_routeCoordCount , ctpVar8 ) ; // process path-finding stuff
2007-04-27 12:58:35 +00:00
polyStruct = ctpVar11 = ctpVar13 = ptr_something ;
2007-04-27 22:33:45 +00:00
ptr = ( uint8 * ) polyStruct ;
2007-04-27 12:58:35 +00:00
walkboxCounter = numberOfWalkboxes ;
2007-04-27 22:33:45 +00:00
while ( ( - - walkboxCounter ) > = 0 ) {
2007-04-27 12:58:35 +00:00
loadCtpSub1 ( walkboxCounter , 0 , ctp_walkboxTable , ctpVar13 ) ;
}
2007-04-27 22:33:45 +00:00
ctpVar15 = ctpVar13 + 1 ; // was after the -1 thing
2007-04-27 12:58:35 +00:00
walkboxCounter = numberOfWalkboxes ;
2007-04-27 22:33:45 +00:00
while ( - - walkboxCounter ) {
loadCtpSub1 ( walkboxCounter , ctp_scale [ walkboxCounter ] * 20 ,
ctp_walkboxTable , ctpVar13 ) ;
2007-04-27 12:58:35 +00:00
}
//ctpVar17 = ctpVar13 - ptr + 4;
{
2007-04-27 22:33:45 +00:00
int numOfUsedEntries = ctpVar13 - ( ctpVar19Struct * ) ptr ;
numOfUsedEntries + + ; // there is a -1 entry at the end... Original was only mallocing numOfUsedEntries*sizeof(ctpVar19Struct)+4, but this is a bit ugly...
ctpVar13 = ctpVar11 = polyStruct =
( ctpVar19Struct * ) malloc ( numOfUsedEntries *
sizeof ( ctpVar19Struct ) ) ;
2007-04-27 12:58:35 +00:00
}
walkboxCounter = numberOfWalkboxes ;
2007-04-27 22:33:45 +00:00
while ( ( - - walkboxCounter ) > = 0 ) {
2007-04-27 12:58:35 +00:00
loadCtpSub1 ( walkboxCounter , 0 , ctp_walkboxTable , ctpVar13 ) ;
}
ctpVar15 = ctpVar13 + 1 ;
walkboxCounter = numberOfWalkboxes ;
2007-04-27 22:33:45 +00:00
while ( - - walkboxCounter ) {
loadCtpSub1 ( walkboxCounter , ctp_scale [ walkboxCounter ] * 20 ,
ctp_walkboxTable , ctpVar13 ) ;
2007-04-27 12:58:35 +00:00
}
ctpVar19 = ctpVar11 ;
2007-04-27 22:33:45 +00:00
return ( 1 ) ;
2007-04-27 12:58:35 +00:00
}
} // End of namespace Cruise