2021-12-26 21:19:38 +01:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2015-09-18 10:35:19 +02:00
|
|
|
*
|
2021-12-26 21:19:38 +01:00
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
2015-09-18 10:35:19 +02:00
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2021-12-26 18:47:58 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2015-09-18 10:35:19 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 18:47:58 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-09-18 10:35:19 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "engines/stark/resources/path.h"
|
|
|
|
|
|
|
|
#include "engines/stark/formats/xrc.h"
|
|
|
|
|
2019-01-25 09:48:31 +01:00
|
|
|
#include "engines/stark/resources/floor.h"
|
|
|
|
|
|
|
|
#include "engines/stark/services/global.h"
|
|
|
|
#include "engines/stark/services/services.h"
|
|
|
|
|
2015-09-18 10:35:19 +02:00
|
|
|
namespace Stark {
|
|
|
|
namespace Resources {
|
|
|
|
|
|
|
|
Object *Path::construct(Object *parent, byte subType, uint16 index, const Common::String &name) {
|
|
|
|
switch (subType) {
|
|
|
|
case kPath2D:
|
|
|
|
return new Path2D(parent, subType, index, name);
|
|
|
|
case kPath3D:
|
|
|
|
return new Path3D(parent, subType, index, name);
|
|
|
|
default:
|
|
|
|
error("Unknown path subtype %d", subType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Path::~Path() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Path::Path(Object *parent, byte subType, uint16 index, const Common::String &name) :
|
2016-01-07 21:50:53 +01:00
|
|
|
Object(parent, subType, index, name),
|
|
|
|
_field_30(0) {
|
2015-09-18 10:35:19 +02:00
|
|
|
_type = TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Path::readData(Formats::XRCReadStream *stream) {
|
|
|
|
_field_30 = stream->readUint32LE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Path::printData() {
|
|
|
|
debug("field_30: %d", _field_30);
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
float Path::getEdgeLength(uint edgeIndex) const {
|
|
|
|
Math::Vector3d edgeStart = getVertexPosition(edgeIndex);
|
|
|
|
Math::Vector3d edgeEnd = getVertexPosition(edgeIndex + 1);
|
|
|
|
|
|
|
|
return edgeStart.getDistanceTo(edgeEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Path::getWeightedEdgeLength(uint edgeIndex) const {
|
|
|
|
float length = getEdgeLength(edgeIndex);
|
|
|
|
float startWeight = getVertexWeight(edgeIndex);
|
|
|
|
float endWeight = getVertexWeight(edgeIndex + 1);
|
|
|
|
|
|
|
|
return 2000.0 * length / (startWeight + endWeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::Vector3d Path::getWeightedPositionInEdge(uint edgeIndex, float positionInEdge) {
|
|
|
|
float edgeLength = getEdgeLength(edgeIndex);
|
|
|
|
float weightedEdgeLength = getWeightedEdgeLength(edgeIndex);
|
|
|
|
|
|
|
|
float startWeight = getVertexWeight(edgeIndex);
|
|
|
|
float endWeight = getVertexWeight(edgeIndex + 1);
|
|
|
|
|
|
|
|
float weightedEdgePosition = ((endWeight - startWeight) / (2 * weightedEdgeLength) * positionInEdge + startWeight) * 0.001
|
|
|
|
* positionInEdge / edgeLength;
|
|
|
|
|
|
|
|
Math::Vector3d edgeStart = getVertexPosition(edgeIndex);
|
|
|
|
Math::Vector3d edgeEnd = getVertexPosition(edgeIndex + 1);
|
|
|
|
|
|
|
|
return edgeEnd * weightedEdgePosition + edgeStart * (1.0 - weightedEdgePosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Path::getSortKey() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::Vector3d Path::getEdgeDirection(uint edgeIndex) const {
|
|
|
|
return Math::Vector3d();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-18 10:35:19 +02:00
|
|
|
Path2D::Path2D(Object *parent, byte subType, uint16 index, const Common::String &name) :
|
|
|
|
Path(parent, subType, index, name) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Path2D::readData(Formats::XRCReadStream *stream) {
|
|
|
|
Path::readData(stream);
|
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
uint32 vertexCount = stream->readUint32LE();
|
|
|
|
for (uint i = 0; i < vertexCount; i++) {
|
|
|
|
Vertex vertex;
|
2018-04-14 11:19:19 +02:00
|
|
|
vertex.weight = stream->readFloatLE();
|
2015-09-23 21:21:53 +02:00
|
|
|
vertex.position = stream->readPoint();
|
2015-09-18 10:35:19 +02:00
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
_vertices.push_back(vertex);
|
2015-09-18 10:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->readUint32LE(); // Unused in the original
|
|
|
|
}
|
|
|
|
|
|
|
|
void Path2D::printData() {
|
|
|
|
Path::printData();
|
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
for (uint i = 0; i < _vertices.size(); i++) {
|
|
|
|
debug("vertex[%d]: (x %d, y %d), weight: %f", i,
|
|
|
|
_vertices[i].position.x, _vertices[i].position.y, _vertices[i].weight);
|
2015-09-18 10:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Path2D::~Path2D() {
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
uint Path2D::getEdgeCount() const {
|
|
|
|
return _vertices.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::Vector3d Path2D::getVertexPosition(uint vertexIndex) const {
|
|
|
|
Common::Point point = _vertices[vertexIndex].position;
|
|
|
|
return Math::Vector3d(point.x, point.y, 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Path2D::getVertexWeight(uint vertexIndex) const {
|
|
|
|
return _vertices[vertexIndex].weight;
|
|
|
|
}
|
|
|
|
|
2015-09-18 10:35:19 +02:00
|
|
|
Path3D::Path3D(Object *parent, byte subType, uint16 index, const Common::String &name) :
|
|
|
|
Path(parent, subType, index, name),
|
|
|
|
_sortKey(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Path3D::readData(Formats::XRCReadStream *stream) {
|
|
|
|
Path::readData(stream);
|
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
uint32 vertexCount = stream->readUint32LE();
|
|
|
|
for (uint i = 0; i < vertexCount; i++) {
|
|
|
|
Vertex vertex;
|
2018-04-14 11:19:19 +02:00
|
|
|
vertex.weight = stream->readFloatLE();
|
2015-09-23 21:21:53 +02:00
|
|
|
vertex.position = stream->readVector3();
|
2015-09-18 10:35:19 +02:00
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
_vertices.push_back(vertex);
|
2015-09-18 10:35:19 +02:00
|
|
|
}
|
|
|
|
|
2018-04-14 11:19:19 +02:00
|
|
|
_sortKey = stream->readFloatLE();
|
2015-09-18 10:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Path3D::printData() {
|
|
|
|
Path::printData();
|
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
for (uint i = 0; i < _vertices.size(); i++) {
|
|
|
|
debug("vertex[%d]: (x %f, y %f, z %f), weight: %f", i,
|
|
|
|
_vertices[i].position.x(), _vertices[i].position.y(), _vertices[i].position.z(), _vertices[i].weight);
|
2015-09-18 10:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug("sortKey: %f", _sortKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
Path3D::~Path3D() {
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:21:53 +02:00
|
|
|
uint Path3D::getEdgeCount() const {
|
|
|
|
return _vertices.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::Vector3d Path3D::getVertexPosition(uint vertexIndex) const {
|
|
|
|
return _vertices[vertexIndex].position;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Path3D::getVertexWeight(uint vertexIndex) const {
|
|
|
|
return _vertices[vertexIndex].weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Path3D::getSortKey() const {
|
|
|
|
return _sortKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::Vector3d Path3D::getEdgeDirection(uint edgeIndex) const {
|
|
|
|
Math::Vector3d direction = getVertexPosition(edgeIndex) - getVertexPosition(edgeIndex + 1);
|
|
|
|
direction.normalize();
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
|
2019-01-25 09:48:31 +01:00
|
|
|
Math::Vector3d Path3D::getVertexPosition3D(uint vertexIndex, int32 *faceIndex) {
|
|
|
|
Math::Vector3d vertex = getVertexPosition(vertexIndex);
|
|
|
|
|
|
|
|
Floor *floor = StarkGlobal->getCurrent()->getFloor();
|
|
|
|
if (floor) {
|
|
|
|
int32 face = floor->findFaceContainingPoint(vertex);
|
|
|
|
if (face >= 0) {
|
|
|
|
floor->computePointHeightInFace(vertex, face);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (faceIndex) {
|
|
|
|
*faceIndex = face;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vertex;
|
|
|
|
}
|
|
|
|
|
2015-09-18 10:35:19 +02:00
|
|
|
} // End of namespace Resources
|
|
|
|
} // End of namespace Stark
|