Skip to content

Commit

Permalink
M4: Attempt to fix the broken Rails code
Browse files Browse the repository at this point in the history
I am not sure how this code could have ever worked without lots of
crashing, but maybe I am missing something... Still, casting an
arbitrary integer value to an int *pointer* and then later dereferencing
it does not seem like a good idea :).

Changed the code to do what I *guess* it was meant to do. But somebody
who actually knows M4 and its games should double check.
  • Loading branch information
fingolfin committed May 25, 2011
1 parent 7e5113b commit 587811d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions engines/m4/rails.cpp
Expand Up @@ -61,9 +61,7 @@ void Rails::clearRails() {
delete tempNode;
}

for (i = 0; i < _edges.size(); i++) {
_edges.remove_at(i);
}
_edges.clear();

for (j = _noWalkRects.begin(); j != _noWalkRects.end(); ++j)
delete (*j);
Expand Down Expand Up @@ -246,7 +244,7 @@ void Rails::createEdge(int32 node1, int32 node2) {
} else {
distance = SqrtF16(FixedMul(deltaX, deltaX) + FixedMul(deltaY, deltaY)) << 8;
}
_edges.insert_at(index, (int16*)(distance >> 16));
_edges.insert_at(index, distance >> 16);
}

debugCN(kDebugCore, "node1 = %d, node2 = %d, valid = %d\n", node1, node2, valid);
Expand Down Expand Up @@ -312,7 +310,7 @@ int16 Rails::getEdgeLength(int32 node1, int32 node2) {
// Find the table entry i.e. tableWidth * node1 + node2 and then subtract
// n(n+1)/2, since only the upper triangle of the table is stored
index = (MAXRAILNODES-1)*node1 + node2 - 1 - (node1*(node1+1)>>1);
return *_edges[index];
return _edges[index];
}

void Rails::disposePath(RailNode *pathStart) {
Expand Down
2 changes: 1 addition & 1 deletion engines/m4/rails.h
Expand Up @@ -73,7 +73,7 @@ class Rails {

private:
Common::Array<RailNode *> _nodes;
Common::Array<int16 *> _edges;
Common::Array<int16> _edges;
Common::List<NoWalkRect *> _noWalkRects;
M4Surface *_walkCodes;

Expand Down

0 comments on commit 587811d

Please sign in to comment.