1515#define IS_CURVED_SURFACE (surfid ) (((surfid) > -1 ) && ((surfid) & 0xFFFFE000 ) == 0x4000 && ((surfid) & 0x1FFF ) < m_numCurves)
1616#define IS_JUNCTION_SURFACE (surfid ) (((surfid) > -1 ) && ((surfid) & 0xFFFFE000 ) == 0x2000 && ((surfid) & 0x1FFF ) < m_numJunctions)
1717
18- sdPlane g_defaultPlane = { 0 , 0 , 0 , 0 , 2048 };
19- sdPlane g_seaPlane = { 9 , 0 , 16384 , 0 , 2048 };
18+ sdPlane g_defaultPlane = { SurfType_Asphalt , 0 , 0 , 0 , 2048 };
19+ sdPlane g_seaPlane = { SurfType_DeepWater , 0 , 16384 , 0 , 2048 };
2020
2121int SdHeightOnPlane (const VECTOR_NOPAD& position, sdPlane* plane, DRIVER2_CURVE* curves)
2222{
@@ -683,19 +683,26 @@ CBaseLevelRegion* CDriver2LevelMap::GetRegion(const XZPAIR& cell) const
683683 const int region_x = cell.x / m_mapInfo.region_size ;
684684 const int region_z = cell.z / m_mapInfo.region_size ;
685685
686- return &m_regions[ region_x + region_z * m_regions_across] ;
686+ return GetRegion ( region_x + region_z * m_regions_across) ;
687687}
688688
689689CBaseLevelRegion* CDriver2LevelMap::GetRegion (int regionIdx) const
690690{
691+ #if 0
692+ const int total_regions = m_regions_across * m_regions_down;
693+
694+ if (regionIdx < 0 && regionIdx >= total_regions)
695+ return nullptr;
696+ #endif
697+
691698 return &m_regions[regionIdx];
692699}
693700
694701void CDriver2LevelMap::SpoolRegion (const SPOOL_CONTEXT& ctx, const XZPAIR& cell)
695702{
696703 CDriver2LevelRegion* region = (CDriver2LevelRegion*)GetRegion (cell);
697704
698- if (!region->m_loaded )
705+ if (region && !region->m_loaded )
699706 {
700707 if (m_regionSpoolInfoOffsets[region->m_regionNumber ] != REGION_EMPTY)
701708 {
@@ -711,7 +718,7 @@ void CDriver2LevelMap::SpoolRegion(const SPOOL_CONTEXT& ctx, int regionIdx)
711718{
712719 CDriver2LevelRegion* region = (CDriver2LevelRegion*)GetRegion (regionIdx);
713720
714- if (!region->m_loaded )
721+ if (region && !region->m_loaded )
715722 {
716723 if (m_regionSpoolInfoOffsets[region->m_regionNumber ] != REGION_EMPTY)
717724 {
@@ -736,14 +743,54 @@ int CDriver2LevelMap::MapHeight(const VECTOR_NOPAD& position) const
736743 WorldPositionToCellXZ (cell, cellPos);
737744 CDriver2LevelRegion* region = (CDriver2LevelRegion*)GetRegion (cell);
738745
739- sdPlane* plane = region->SdGetCell (cellPos, level, SdGetBSP);
746+ sdPlane* plane = &g_seaPlane;
747+ if (region)
748+ plane = region->SdGetCell (cellPos, level, SdGetBSP);
740749
741750 if (plane)
742751 return SdHeightOnPlane (position, plane, m_curves);
743752
744753 return 0 ;
745754}
746755
756+ int CDriver2LevelMap::FindSurface (const VECTOR_NOPAD& position, VECTOR_NOPAD& outNormal, VECTOR_NOPAD& outPoint, sdPlane** outPlane) const
757+ {
758+ VECTOR_NOPAD cellPos;
759+ XZPAIR cell;
760+ int level = 0 ;
761+
762+ cellPos.vx = position.vx - 512 ; // FIXME: is that a quarter of a cell?
763+ cellPos.vy = position.vy ;
764+ cellPos.vz = position.vz - 512 ;
765+
766+ WorldPositionToCellXZ (cell, cellPos);
767+ CDriver2LevelRegion* region = (CDriver2LevelRegion*)GetRegion (cell);
768+
769+ if (region)
770+ {
771+ *outPlane = region->SdGetCell (cellPos, level, SdGetBSP);
772+
773+ outPoint.vx = position.vx ;
774+ outPoint.vz = position.vz ;
775+ outPoint.vy = SdHeightOnPlane (position, *outPlane, m_curves);
776+
777+ if (*outPlane == NULL || (*outPlane)->b == 0 )
778+ {
779+ outNormal.vx = 0 ;
780+ outNormal.vy = 4096 ;
781+ outNormal.vz = 0 ;
782+ }
783+ else
784+ {
785+ outNormal.vx = (int )(*outPlane)->a >> 2 ;
786+ outNormal.vy = (int )(*outPlane)->b >> 2 ;
787+ outNormal.vz = (int )(*outPlane)->c >> 2 ;
788+ }
789+ }
790+
791+ return 4096 ;
792+ }
793+
747794int CDriver2LevelMap::GetRoadIndex (VECTOR_NOPAD& position) const
748795{
749796 VECTOR_NOPAD cellPos;
@@ -915,6 +962,7 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetNextPackedCop(CELL_ITERATOR_D2* iterato
915962 ushort num;
916963 PACKED_CELL_OBJECT* ppco;
917964 CELL_OBJECT* co;
965+ CDriver2LevelRegion* reg = iterator->region ;
918966
919967 do {
920968 CELL_DATA* celld = iterator->pcd ;
@@ -932,8 +980,8 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetNextPackedCop(CELL_ITERATOR_D2* iterato
932980
933981 iterator->pcd = celld;
934982
935- ppco = iterator-> region ->GetPackedCellObject (celld->num & 0x3fff );
936- co = iterator-> region ->GetCellObject (celld->num & 0x3fff );
983+ ppco = reg ->GetPackedCellObject (celld->num & 0x3fff );
984+ co = reg ->GetCellObject (celld->num & 0x3fff );
937985
938986 } while (ppco->value == 0xffff && (ppco->pos .vy & 1 ));
939987
0 commit comments