[Scummvm-git-logs] scummvm master -> 74283f3b785a957a2940da449832821534d42c9c

aquadran noreply at scummvm.org
Thu Nov 26 17:49:42 UTC 2026


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://api.github.com/repos/scummvm/scummvm .

Summary:
74283f3b78 EMI: Improve accuracy of walking


Commit: 74283f3b785a957a2940da449832821534d42c9c
    https://github.com/scummvm/scummvm/commit/74283f3b785a957a2940da449832821534d42c9c
Author: Joel Teichroeb (joel at teichroeb.net)
Date: 2026-11-26T18:49:37+01:00

Commit Message:
EMI: Improve accuracy of walking

Navigating between sectors is very different in EMI and GRIM. In GRIM,
sectors don't overlap and connect well, whereas in EMI sectors often
overlap and don't connect to each other directly.

Give EMI its own walkForward, modelled on the original's. Unlike the
original, a crossing close to a vertex snaps to it, so the actor does
not get stuck in corners.

The GRIM path is unchanged in behaviour and only loses its EMI branches.

Fixes #11791

Changed paths:
    engines/grim/actor.cpp
    engines/grim/actor.h
    engines/grim/sector.cpp
    engines/grim/sector.h
    engines/grim/set.cpp
    engines/grim/set.h


diff --git a/engines/grim/actor.cpp b/engines/grim/actor.cpp
index 096ba67f74d..fe75651416a 100644
--- a/engines/grim/actor.cpp
+++ b/engines/grim/actor.cpp
@@ -808,6 +808,70 @@ void Actor::moveTo(const Math::Vector3d &pos) {
 	_pos += moveVec;
 }
 
+void Actor::walkForwardEMI(float dist, bool backwards) {
+	const int maxHandovers = 8;
+	const float minMove = 0.0001f;
+	const float turnMargin = 0.1f;
+	const float reverseTurn = 180.2f;
+	Set *set = g_grim->getCurrSet();
+	Common::Array<const Sector *> walked;
+
+	Sector *sector = set->findFloorSector(_pos, Sector::WalkType, walked);
+	if (!sector)
+		set->findClosestSector(_pos, &sector, &_pos);
+
+	for (int depth = 0; dist > 0.f; depth++) {
+		Math::Vector3d heading(_moveYaw.getSine(), 0.f, _moveYaw.getCosine());
+		if (backwards)
+			heading = -heading;
+		if (!sector) {
+			moveTo(_pos + heading * dist);
+			_walkedCur = true;
+			break;
+		}
+
+		Math::Vector3d ahead = _pos + heading;
+		ahead.y() = sector->getPlaneHeight(ahead);
+		Math::Vector3d dir = ahead - _pos;
+		dir.normalize();
+		Math::Vector3d end = _pos + dir * dist;
+		if (sector->isPointOnFloor(end)) {
+			end.y() = sector->getPlaneHeight(end);
+			moveTo(end);
+			_walkedCur = true;
+			break;
+		}
+		if (depth >= maxHandovers)
+			break;
+
+		Sector::ExitInfo ei;
+		sector->getFloorExitInfo(Math::Line3d(_pos, end), &ei);
+		Math::Vector3d toExit = ei.exitPoint - _pos;
+		if (toExit.getMagnitude() > minMove)
+			_walkedCur = true;
+		dist -= toExit.dotProduct(heading);
+		_pos = ei.exitPoint;
+
+		walked.push_back(sector);
+		Sector *next = set->findFloorSector(_pos, Sector::WalkType, walked);
+		if (next) {
+			sector = next;
+			continue;
+		}
+		walked.pop_back();
+
+		float turn = ei.angleWithEdge.getDegrees();
+		if (ei.edgeVertex >= 0) {
+			if (turn > 90.f)
+				turn = -(reverseTurn - turn);
+			else
+				turn += turnMargin;
+		}
+		if (turn != 0.f)
+			turnTo(0, _moveYaw.getDegrees() + turn, 0, true);
+	}
+}
+
 void Actor::walkForward() {
 	float dist = g_grim->getPerSecond(_walkRate) * _timeScale;
 	// Limit the amount of the movement per frame, otherwise with low fps
@@ -847,6 +911,11 @@ void Actor::walkForward() {
 		backwards = true;
 	}
 
+	if (g_grim->getGameType() == GType_MONKEY4) {
+		walkForwardEMI(dist, backwards);
+		return;
+	}
+
 	int tries = 0;
 	while (dist > 0.0f) {
 		Sector *currSector = nullptr, *prevSector = nullptr, *startSector = nullptr;
@@ -857,13 +926,6 @@ void Actor::walkForward() {
 			Math::Vector3d forwardVec(-_moveYaw.getSine(),
 			                           _moveYaw.getCosine(), 0);
 
-			// EMI: Y is up-down, actors use an X-Z plane for movement
-			if (g_grim->getGameType() == GType_MONKEY4) {
-				forwardVec.x() = -forwardVec.x();
-				forwardVec.z() = forwardVec.y();
-				forwardVec.y() = 0;
-			}
-
 			if (backwards)
 				forwardVec = -forwardVec;
 
@@ -878,23 +940,13 @@ void Actor::walkForward() {
 			prevSector = currSector;
 			Math::Vector3d forwardVec;
 			const Math::Vector3d &normal = currSector->getNormal();
-			if (g_grim->getGameType() == GType_GRIM) {
-				Math::Angle ax = Math::Vector2d(normal.x(), normal.z()).getAngle();
-				Math::Angle ay = Math::Vector2d(normal.y(), normal.z()).getAngle();
+			Math::Angle ax = Math::Vector2d(normal.x(), normal.z()).getAngle();
+			Math::Angle ay = Math::Vector2d(normal.y(), normal.z()).getAngle();
 
-				float z1 = -_moveYaw.getCosine() * ay.getCosine();
-				float z2 = _moveYaw.getSine() * ax.getCosine();
-				forwardVec = Math::Vector3d(-_moveYaw.getSine() * ax.getSine(),
-				                             _moveYaw.getCosine() * ay.getSine(), z1 + z2);
-			} else {
-				Math::Angle ax = Math::Vector2d(normal.x(), normal.y()).getAngle();
-				Math::Angle az = Math::Vector2d(normal.z(), normal.y()).getAngle();
-
-				float y1 = _moveYaw.getCosine() * az.getCosine();
-				float y2 = _moveYaw.getSine() * ax.getCosine();
-				forwardVec = Math::Vector3d(-_moveYaw.getSine() * ax.getSine(), y1 + y2,
-				                            -_moveYaw.getCosine() * az.getSine());
-			}
+			float z1 = -_moveYaw.getCosine() * ay.getCosine();
+			float z2 = _moveYaw.getSine() * ax.getCosine();
+			forwardVec = Math::Vector3d(-_moveYaw.getSine() * ax.getSine(),
+			                             _moveYaw.getCosine() * ay.getSine(), z1 + z2);
 
 			if (backwards)
 				forwardVec = -forwardVec;
@@ -917,13 +969,6 @@ void Actor::walkForward() {
 			// the path
 			currSector = g_grim->getCurrSet()->findPointSector(ei.exitPoint + (float)0.0001 * puckVec, Sector::WalkType);
 
-			// EMI: some sectors are significantly higher/lower than others.
-			if (currSector && g_grim->getGameType() == GType_MONKEY4) {
-				float planeDist = currSector->distanceToPoint(_pos);
-				if (fabs(planeDist) < 1.f)
-					_pos -= planeDist * currSector->getNormal();
-			}
-
 			if (currSector == prevSector || currSector == startSector)
 				break;
 		}
@@ -939,9 +984,6 @@ void Actor::walkForward() {
 
 		ei.angleWithEdge += (float)1.0f;
 
-		if (g_grim->getGameType() == GType_MONKEY4) {
-			ei.angleWithEdge = -ei.angleWithEdge;
-		}
 		turnTo(0, _moveYaw + ei.angleWithEdge * turnDir, 0, true);
 
 		if (oldDist <= dist + 0.001f) {
diff --git a/engines/grim/actor.h b/engines/grim/actor.h
index 96a992cf27f..486505bb5ea 100644
--- a/engines/grim/actor.h
+++ b/engines/grim/actor.h
@@ -583,6 +583,7 @@ private:
 	ScreenBounds getScreenBounds() const;
 	void collisionHandlerCallback(Actor *other) const;
 	void updateWalk();
+	void walkForwardEMI(float dist, bool backwards);
 	void addShadowPlane(const char *n, Set *scene, int shadowId);
 	bool shouldDrawShadow(int shadowId);
 	void stopTalking();
diff --git a/engines/grim/sector.cpp b/engines/grim/sector.cpp
index 076c341b965..d22d1e59cdd 100644
--- a/engines/grim/sector.cpp
+++ b/engines/grim/sector.cpp
@@ -21,6 +21,8 @@
 
 #include "common/util.h"
 
+#include "math/vector2d.h"
+
 #include "engines/grim/debug.h"
 #include "engines/grim/grim.h"
 #include "engines/grim/sector.h"
@@ -342,6 +344,35 @@ bool Sector::isPointInSector(const Math::Vector3d &point) const {
 	return true;
 }
 
+bool Sector::isPointOnFloor(const Math::Vector3d &point) const {
+	const float defaultHeight = 0.005f;
+	const float edgeMargin = 1e-5f;
+	float height = _height;
+	if (height == 0.f)
+		height = defaultHeight;
+	if (_normal.y() == 0.f)
+		return false;
+	if (fabsf(distanceToPoint(point) / _normal.y()) > height)
+		return false;
+
+	for (int i = 0; i < _numVertices; i++) {
+		Math::Vector3d edge = _vertices[i] - _vertices[i + 1];
+		Math::Vector3d delta = _vertices[i + 1] - point;
+		edge.y() = 0;
+		delta.y() = 0;
+		float side = Math::Vector3d::crossProduct(delta, edge).y() / edge.getSquareMagnitude();
+		if (side < -edgeMargin)
+			return false;
+	}
+	return true;
+}
+
+float Sector::getPlaneHeight(const Math::Vector3d &point) const {
+	if (_normal.y() == 0.f)
+		return _vertices[0].y();
+	return point.y() - distanceToPoint(point) / _normal.y();
+}
+
 Common::List<Math::Line3d> Sector::getBridgesTo(Sector *sector) const {
 	// This returns a list of "bridges", which are edges that can be travelled
 	// through to get to another sector. 0 bridges mean the sectors aren't
@@ -517,6 +548,70 @@ void Sector::getExitInfo(const Math::Vector3d &s, const Math::Vector3d &dirVec,
 	result->exitPoint = start + (Math::Vector3d::dotProduct(_vertices[i] - start, edgeNormal) / d) * dir;
 }
 
+void Sector::getFloorExitInfo(const Math::Line3d &segment, struct ExitInfo *result) const {
+	const float vertexSnap = 1e-5f;
+	const Math::Vector3d start = segment.begin();
+	const Math::Vector3d step = segment.end() - start;
+	const Math::Vector2d floorStep(step.x(), step.z());
+	const float stepLength = floorStep.getMagnitude();
+	float exitFraction = -1.f;
+	bool exitAtStartVertex = false;
+	int snapVertex = -1;
+	Math::Angle angle;
+	for (int i = 0; i < _numVertices; i++) {
+		int prev = i - 1;
+		if (prev < 0)
+			prev = _numVertices - 1;
+		const Math::Vector3d edge = _vertices[prev] - _vertices[i];
+		float stepCrossEdge = step.x() * edge.z() - edge.x() * step.z();
+		if (stepCrossEdge == 0.f)
+			continue;
+		const Math::Vector3d fromVertex = start - _vertices[i];
+		const float fromCrossEdge = fromVertex.z() * edge.x() - fromVertex.x() * edge.z();
+		float stepFraction = fromCrossEdge / stepCrossEdge;
+		if (stepFraction < 0.f && fromCrossEdge > 0.f)
+			stepFraction = 0.f;
+		if (stepFraction < 0.f || stepFraction > 1.f)
+			continue;
+		float fromCrossStep = fromVertex.z() * step.x() - fromVertex.x() * step.z();
+		if (stepCrossEdge < 0.f) {
+			fromCrossStep = -fromCrossStep;
+			stepCrossEdge = -stepCrossEdge;
+		}
+		if (fromCrossStep < -vertexSnap * stepCrossEdge || fromCrossStep > (1.f + vertexSnap) * stepCrossEdge)
+			continue;
+		const bool atStartVertex = fromCrossStep < vertexSnap * stepCrossEdge;
+		if (stepFraction < exitFraction || (stepFraction == exitFraction && (exitAtStartVertex || !atStartVertex)))
+			continue;
+		exitFraction = stepFraction;
+		exitAtStartVertex = atStartVertex;
+		snapVertex = -1;
+		if (atStartVertex)
+			snapVertex = i;
+		else if (fromCrossStep > (1.f - vertexSnap) * stepCrossEdge)
+			snapVertex = prev;
+		result->edgeVertex = prev;
+		result->edgeDir = -edge;
+		const Math::Vector2d floorEdge(edge.x(), edge.z());
+		const float edgeLength = floorEdge.getMagnitude();
+		const float cosine = step.x() / stepLength * (edge.x() / edgeLength) + step.z() / stepLength * (edge.z() / edgeLength);
+		angle = Math::Angle::arcCosine(CLIP(cosine, -1.0f, 1.0f));
+	}
+	result->exitPoint = start;
+	if (exitFraction > 0.f) {
+		result->exitPoint.x() = step.x() * exitFraction + start.x();
+		result->exitPoint.z() = step.z() * exitFraction + start.z();
+	} else {
+		result->edgeVertex = -1;
+	}
+	if (snapVertex >= 0) {
+		result->exitPoint.x() = _vertices[snapVertex].x();
+		result->exitPoint.z() = _vertices[snapVertex].z();
+	}
+	result->exitPoint.y() = getPlaneHeight(result->exitPoint);
+	result->angleWithEdge = angle;
+}
+
 int Sector::getSortplane(int setup) {
 	// An unkown sortplane is 0
 	if (setup < 0 || setup >= _numSortplanes) {
diff --git a/engines/grim/sector.h b/engines/grim/sector.h
index 810a1bd7560..9911610b745 100644
--- a/engines/grim/sector.h
+++ b/engines/grim/sector.h
@@ -66,6 +66,8 @@ public:
 	SectorType getType() const { return _type; } // FIXME: Implement type de-masking
 	bool isVisible() const { return _visible && !_invalid; }
 	bool isPointInSector(const Math::Vector3d &point) const;
+	bool isPointOnFloor(const Math::Vector3d &point) const;
+	float getPlaneHeight(const Math::Vector3d &point) const;
 	float distanceToPoint(const Math::Vector3d &point) const;
 	Common::List<Math::Line3d> getBridgesTo(Sector *sector) const;
 
@@ -82,6 +84,7 @@ public:
 		int edgeVertex;
 	};
 	void getExitInfo(const Math::Vector3d &start, const Math::Vector3d &dir, struct ExitInfo *result) const;
+	void getFloorExitInfo(const Math::Line3d &segment, struct ExitInfo *result) const;
 
 	int getNumSortplanes() { return _numSortplanes; }
 	int getSortplane(int setup);
diff --git a/engines/grim/set.cpp b/engines/grim/set.cpp
index 20f7de0f1f1..ba1f86cf7a9 100644
--- a/engines/grim/set.cpp
+++ b/engines/grim/set.cpp
@@ -927,6 +927,17 @@ Sector *Set::findPointSector(const Math::Vector3d &p, Sector::SectorType type) {
 	return nullptr;
 }
 
+Sector *Set::findFloorSector(const Math::Vector3d &p, Sector::SectorType type, const Common::Array<const Sector *> &exclude) {
+	for (int i = 0; i < _numSectors; i++) {
+		Sector *sector = _sectors[i];
+		if (!sector || !(sector->getType() & type) || !sector->isVisible() || !sector->isPointOnFloor(p))
+			continue;
+		if (Common::find(exclude.begin(), exclude.end(), sector) == exclude.end())
+			return sector;
+	}
+	return nullptr;
+}
+
 Common::Pair<bool, int> Set::findSectorSortOrder(const Math::Vector3d &p, Sector::SectorType type) {
 	int setup = getSetup();
 	float minDist = 0.01f;
diff --git a/engines/grim/set.h b/engines/grim/set.h
index 0693da86a48..538a1fe6487 100644
--- a/engines/grim/set.h
+++ b/engines/grim/set.h
@@ -108,6 +108,7 @@ public:
 	Sector *findNamedSector(const Common::String &name, const Math::Vector3d &pos);
 
 	Sector *findPointSector(const Math::Vector3d &p, Sector::SectorType type);
+	Sector *findFloorSector(const Math::Vector3d &p, Sector::SectorType type, const Common::Array<const Sector *> &exclude);
 	Common::Pair<bool, int> findSectorSortOrder(const Math::Vector3d &p, Sector::SectorType type);
 	void findClosestSector(const Math::Vector3d &p, Sector **sect, Math::Vector3d *closestPt);
 	void shrinkBoxes(float radius);




More information about the Scummvm-git-logs mailing list