Skip to content

Commit b14acf5

Browse files
committed
Fixed illegal memory access when handling hidden tree elements on tree compactification
1 parent 05eba16 commit b14acf5

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

‎TechTree/VanillaTechTreeRenderer.cpp‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,6 @@ void VanillaTechTreeRenderer::RenderSubTree(TechTreeElement *element, DirectDraw
375375
// Need to draw lines below?
376376
if(element->_children.size() > 0)
377377
{
378-
// Draw vertical line
379-
drawBuffer->DrawFilledRectangle(drawX + 31, drawY + 64, drawX + 32, drawY + 67, (elementIsSelectionPathElement && nextSelectionPathElementDirectlyReachable ? 255 : 0));
380-
381378
// Draw vertical line for each child and store left most / right most child coordinates
382379
int leftMostChildDrawX = INT32_MIN;
383380
int rightMostChildDrawX = INT32_MIN;
@@ -439,9 +436,15 @@ void VanillaTechTreeRenderer::RenderSubTree(TechTreeElement *element, DirectDraw
439436
drawBuffer->DrawFilledRectangle(childDrawX + 31, drawY + 68, childDrawX + 32, childDrawY, 0);
440437
}
441438

442-
// Draw horizontal connection from the left most child to the right most child
439+
// Draw vertical line from the parent, and horizontal line from the left most child to the right most child
443440
if(leftMostChildDrawX != INT32_MIN && rightMostChildDrawX != INT32_MIN)
441+
{
442+
// Draw vertical line
443+
drawBuffer->DrawFilledRectangle(drawX + 31, drawY + 64, drawX + 32, drawY + 67, (elementIsSelectionPathElement && nextSelectionPathElementDirectlyReachable ? 255 : 0));
444+
445+
// Draw horicontal line
444446
drawBuffer->DrawFilledRectangle(leftMostChildDrawX + 31, drawY + 68, rightMostChildDrawX + 32, drawY + 69, 0);
447+
}
445448

446449
// If the element is part of a selection path, draw horizontal line along path
447450
if(elementIsSelectionPathElement && nextSelectionPathElementDirectlyReachable)
@@ -526,7 +529,7 @@ void VanillaTechTreeRenderer::SetCurrentCiv(int civId)
526529
if(!enabledChildExists && currElement->_renderState != TechTreeElement::ItemRenderState::Hidden)
527530
++_treeMatrixWidth;
528531
}
529-
532+
530533
// Free tree layout matrix rows if neccessary
531534
for(int i = 0; i < _ageCount * 2; ++i)
532535
if(_treeLayoutMatrix[i])
@@ -577,14 +580,15 @@ int VanillaTechTreeRenderer::ComputeSubTree(TechTreeElement *element, int startC
577580
}
578581

579582
// Check whether there are now collisions with subtrees of different buildings
580-
for(int y = rowIndex - 1; y >= 0; --y)
581-
if(_treeLayoutMatrix[y][elementMinimumStartColumnIndex - 2] != nullptr
582-
&& _treeLayoutMatrix[y][elementMinimumStartColumnIndex - 2]->_parentBuilding != element->_parentBuilding)
583-
{
584-
// Conflict found
585-
conflict = true;
586-
break;
587-
}
583+
if(!conflict)
584+
for(int y = rowIndex - 1; y >= 0; --y)
585+
if(_treeLayoutMatrix[y][elementMinimumStartColumnIndex - 2] != nullptr
586+
&& _treeLayoutMatrix[y][elementMinimumStartColumnIndex - 2]->_parentBuilding != element->_parentBuilding)
587+
{
588+
// Conflict found
589+
conflict = true;
590+
break;
591+
}
588592

589593
// No conflict? => Next position
590594
if(conflict)
@@ -811,12 +815,15 @@ void VanillaTechTreeRenderer::SetSelectedElement(TechTreeElement *element)
811815

812816
void VanillaTechTreeRenderer::MoveTreeLeft(TechTreeElement *element, int amount)
813817
{
814-
// Move recursively
818+
// Move non-hidden items recursively
815819
for(TechTreeElement *currChild : element->_children)
820+
if(currChild->_renderState != TechTreeElement::ItemRenderState::Hidden)
816821
MoveTreeLeft(currChild, amount);
817822

818-
// Calculate now column index
823+
// Calculate new column index
819824
int newDrawX = element->_renderPosition.X - amount;
825+
if(newDrawX < 0)
826+
__asm int 3; // Should never happen
820827

821828
// Update tree layout matrix
822829
_treeLayoutMatrix[element->_renderPosition.Y][element->_renderPosition.X] = nullptr;

0 commit comments

Comments
 (0)