@@ -206,10 +206,6 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
206206 cellPos.x = position.vx - 512 ;
207207 cellPos.y = position.vz - 512 ;
208208
209- sdPlane* planeData = (sdPlane*)((char *)buffer + buffer[1 ]);
210- short * bspData = (short *)((char *)buffer + buffer[2 ]);
211- sdNode* nodeData = (sdNode*)((char *)buffer + buffer[3 ]);
212-
213209 check = &buffer[(cellPos.x >> 10 & 63 ) +
214210 (cellPos.y >> 10 & 63 ) * 64 + 4 ];
215211
@@ -227,7 +223,7 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
227223 moreLevels = (*check & 0x8000 ) != 0 ;
228224
229225 if (moreLevels)
230- check = &bspData [(*check & 0x1fff ) + 1 ];
226+ check = &m_bspData [(*check & 0x1fff ) + 1 ];
231227
232228 do
233229 {
@@ -238,11 +234,11 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
238234 // basically it determines surface bounds
239235 if (*check & 0x4000 )
240236 {
241- sdNode* search = &nodeData [*check & 0x1fff ]; // 0x3fff in final
237+ sdNode* search = &m_nodeData [*check & 0x1fff ]; // 0x3fff in final
242238
243239 while (search->node < 0 )
244240 {
245- plane = FindRoadInBSP (search + 1 , planeData );
241+ plane = FindRoadInBSP (search + 1 , m_planeData );
246242
247243 if (plane != nullptr )
248244 break ;
@@ -255,7 +251,7 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
255251 }
256252 else
257253 {
258- plane = &planeData [*check];
254+ plane = &m_planeData [*check];
259255
260256 if (plane->surfaceType >= 32 )
261257 break ;
@@ -271,7 +267,7 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
271267 }
272268 else
273269 {
274- plane = &planeData [*check];
270+ plane = &m_planeData [*check];
275271 }
276272 }
277273 else
@@ -281,7 +277,7 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
281277 moreLevels = (*check & 0x6000 ) == 0x2000 ;
282278
283279 if (moreLevels)
284- check = &bspData [(*check & 0x1fff ) + 1 ];
280+ check = &m_bspData [(*check & 0x1fff ) + 1 ];
285281
286282 do
287283 {
@@ -290,14 +286,14 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
290286
291287 if (*check & 0x4000 )
292288 {
293- plane = FindRoadInBSP (&nodeData [*check & 0x3fff ], planeData );
289+ plane = FindRoadInBSP (&m_nodeData [*check & 0x3fff ], m_planeData );
294290
295291 if (plane != nullptr )
296292 break ;
297293 }
298294 else
299295 {
300- plane = &planeData [*check];
296+ plane = &m_planeData [*check];
301297
302298 if (plane->surfaceType >= 32 )
303299 break ;
@@ -308,7 +304,7 @@ int CDriver2LevelRegion::RoadInCell(VECTOR_NOPAD& position) const
308304 }
309305 else if (!(*check & 0xE000 ))
310306 {
311- plane = &planeData [*check];
307+ plane = &m_planeData [*check];
312308 }
313309 else
314310 plane = nullptr ;
@@ -903,17 +899,12 @@ int CDriver2LevelMap::GetNumJunctions() const
903899PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop (CELL_ITERATOR_D2* iterator, const XZPAIR& cell) const
904900{
905901 // lookup region
906- const int region_x = cell.x / m_mapInfo.region_size ;
907- const int region_z = cell.z / m_mapInfo.region_size ;
908-
909- int regionIdx = region_x + region_z * m_regions_across;
910-
911- CDriver2LevelRegion& region = m_regions[regionIdx];
902+ CDriver2LevelRegion* region = (CDriver2LevelRegion*)GetRegion (cell);
912903
913- iterator->region = & region;
904+ iterator->region = region;
914905
915906 // don't do anything on empty or non-spooled regions
916- if (!region. m_cells )
907+ if (!region-> m_cells )
917908 return nullptr ;
918909
919910 // get cell index on the region
@@ -923,7 +914,7 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop(CELL_ITERATOR_D2* iterat
923914 // FIXME: might be incorrect
924915 int cell_index = region_cell_x + region_cell_z * m_mapInfo.region_size ;
925916
926- ushort cell_ptr = region. m_cellPointers [cell_index];
917+ ushort cell_ptr = region-> m_cellPointers [cell_index];
927918
928919 if (cell_ptr == 0xFFFF )
929920 return nullptr ;
@@ -933,7 +924,7 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop(CELL_ITERATOR_D2* iterat
933924 iterator->nearCell .z = (cell.z - (m_mapInfo.cells_down / 2 )) * m_mapInfo.cell_size ;
934925
935926 // get the packed cell data start and near cell
936- CELL_DATA* celld = ®ion. m_cells [cell_ptr];
927+ CELL_DATA* celld = ®ion-> m_cells [cell_ptr];
937928
938929 /*
939930 Data looks like this:
@@ -954,13 +945,29 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop(CELL_ITERATOR_D2* iterat
954945 celld++; // get to the start
955946 }
956947
957- PACKED_CELL_OBJECT* ppco = region. GetPackedCellObject (celld->num & 0x3fff );
958- iterator->co = region. GetCellObject (celld->num & 0x3fff );
948+ PACKED_CELL_OBJECT* ppco = region-> GetPackedCellObject (celld->num & 0x3fff );
949+ iterator->co = region-> GetCellObject (celld->num & 0x3fff );
959950
960951 iterator->pcd = celld;
961952
962953 if (ppco->value == 0xffff && (ppco->pos .vy & 1 ))
963954 ppco = GetNextPackedCop (iterator);
955+ else if (iterator->cache )
956+ {
957+ CELL_ITERATOR_CACHE* cache = iterator->cache ;
958+ ushort num = celld->num ;
959+ uint value = 1 << (num & 7 ) & 0xffff ;
960+
961+ if ((cache->computedValues [(num & 0x3fff ) >> 3 ] & value))
962+ {
963+ ppco = GetNextPackedCop (iterator);
964+ iterator->ppco = ppco;
965+
966+ return ppco;
967+ }
968+
969+ cache->computedValues [(num & 0x3fff ) >> 3 ] |= value;
970+ }
964971
965972 iterator->ppco = ppco;
966973
@@ -972,31 +979,46 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop(CELL_ITERATOR_D2* iterat
972979// -------------------------------------------------------------
973980PACKED_CELL_OBJECT* CDriver2LevelMap::GetNextPackedCop (CELL_ITERATOR_D2* iterator) const
974981{
975- ushort num;
976982 PACKED_CELL_OBJECT* ppco;
977983 CELL_OBJECT* co;
978984 CDriver2LevelRegion* reg = iterator->region ;
985+ CELL_DATA* celld = iterator->pcd ;
979986
980- do {
981- CELL_DATA* celld = iterator->pcd ;
987+ do
988+ {
989+ do {
990+ if (celld->num & 0x8000 ) // end of the cell objects?
991+ return nullptr ;
982992
983- if (celld->num & 0x8000 ) // end of the cell objects?
984- return nullptr ;
993+ celld++;
985994
986- celld++;
995+ if (celld->num & 0x4000 ) // if we got new list
996+ {
997+ iterator->listType = celld->num ;
998+ celld++; // get to the start
999+ }
9871000
988- if (celld->num & 0x4000 ) // if we got new list
989- {
990- iterator->listType = celld->num ;
991- celld++; // get to the start
992- }
1001+ iterator->pcd = celld;
9931002
994- iterator->pcd = celld;
1003+ ppco = reg->GetPackedCellObject (celld->num & 0x3fff );
1004+ co = reg->GetCellObject (celld->num & 0x3fff );
1005+ } while (ppco->value == 0xffff && (ppco->pos .vy & 1 ));
9951006
996- ppco = reg-> GetPackedCellObject (celld-> num & 0x3fff );
997- co = reg-> GetCellObject (celld-> num & 0x3fff ) ;
1007+ if (!iterator-> cache )
1008+ break ;
9981009
999- } while (ppco->value == 0xffff && (ppco->pos .vy & 1 ));
1010+ ushort num = celld->num ;
1011+
1012+ CELL_ITERATOR_CACHE* cache = iterator->cache ;
1013+
1014+ uint value = 1 << (num & 7 ) & 0xffff ;
1015+
1016+ if ((cache->computedValues [(num & 0x3fff ) >> 3 ] & value) == 0 )
1017+ {
1018+ cache->computedValues [(num & 0x3fff ) >> 3 ] |= value;
1019+ break ;
1020+ }
1021+ } while (true );
10001022
10011023 iterator->ppco = ppco;
10021024 iterator->co = co;
0 commit comments