File: floor.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (339 lines) | stat: -rw-r--r-- 9,150 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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.
 *
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "engines/stark/resources/floor.h"

#include "engines/stark/formats/xrc.h"

#include "engines/stark/resources/floorface.h"
#include "engines/stark/resources/floorfield.h"

#include "engines/stark/services/stateprovider.h"

namespace Stark {
namespace Resources {

Floor::Floor(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Object(parent, subType, index, name),
		_facesCount(0) {
	_type = TYPE;
}

Floor::~Floor() {
}

Math::Vector3d Floor::getVertex(uint32 index) const {
	return _vertices[index];
}

int32 Floor::findFaceContainingPoint(const Math::Vector3d &point) const {
	for (uint32 i = 0; i < _faces.size(); i++) {
		if (_faces[i]->hasVertices() && _faces[i]->isPointInside(point)) {
			return i;
		}
	}

	return -1;
}

void Floor::computePointHeightInFace(Math::Vector3d &point, uint32 faceIndex) const {
	_faces[faceIndex]->computePointHeight(point);
}

int32 Floor::findFaceHitByRay(const Math::Ray &ray, Math::Vector3d &intersection) const {
	for (uint32 i = 0; i < _faces.size(); i++) {
		// TODO: Check the ray's intersection with an AABB first if this ends up being slow
		if (_faces[i]->intersectRay(ray, intersection)) {
			if (_faces[i]->isEnabled()) {
				return i;
			} else {
				return -1; // Disabled faces block the ray
			}
		}
	}

	return -1;
}

int32 Floor::findFaceClosestToRay(const Math::Ray &ray, Math::Vector3d &center) const {
	float minDistance = FLT_MAX;
	int32 minFace = -1;

	// For some reason, face 0 is not being considered
	for (uint32 i = 1; i < _faces.size(); i++) {
		if (_faces[i]->isEnabled() && _faces[i]->hasVertices()) {
			float distance = _faces[i]->distanceToRay(ray);
			if (distance < minDistance) {
				minFace = i;
				minDistance = distance;
			}
		}
	}

	if (minFace >= 0) {
		center = _faces[minFace]->getCenter();
	}

	return minFace;
}

float Floor::getDistanceFromCamera(uint32 faceIndex) const {
	FloorFace *face = _faces[faceIndex];
	return face->getDistanceFromCamera();
}

FloorFace *Floor::getFace(uint32 index) const {
	return _faces[index];
}

bool Floor::isSegmentInside(const Math::Line3d &segment) const {
	// The segment is inside the floor if at least one of its extremities is,
	// and it does not cross any floor border / disabled floor faces

	int32 beginFace = findFaceContainingPoint(segment.begin());
	if (beginFace < 0) {
		// The segment begin point is not on the floor
		return false;
	}

	if (!_faces[beginFace]->isEnabled()) {
		// The segment begin point is not enabled
		return false;
	}

	for (uint i = 0; i < _edges.size(); i++) {
		const FloorEdge &edge = _edges[i];
		if ((edge.isFloorBorder() || !edge.isEnabled()) && edge.intersectsSegment(this, segment)) {
			return false;
		}
	}

	return true;
}

void Floor::readData(Formats::XRCReadStream *stream) {
	_facesCount = stream->readUint32LE();
	uint32 vertexCount = stream->readUint32LE();

	for (uint i = 0; i < vertexCount; i++) {
		Math::Vector3d v = stream->readVector3();
		_vertices.push_back(v);
	}
}

void Floor::onAllLoaded() {
	Object::onAllLoaded();

	_faces = listChildren<FloorFace>();

	buildEdgeList();
}

void Floor::saveLoad(ResourceSerializer *serializer) {
	for (uint i = 0; i < _edges.size(); i++) {
		_edges[i].saveLoad(serializer);
	}
}

void Floor::buildEdgeList() {
	_edges.clear();

	// Add the triangle edges from all our faces
	for (uint i = 0; i < _faces.size(); i++) {
		if (_faces[i]->hasVertices()) {
			addFaceEdgeToList(i, 2, 0);
			addFaceEdgeToList(i, 0, 1);
			addFaceEdgeToList(i, 1, 2);
		}
	}

	// Add the edges to their faces
	for (uint i = 0; i < _edges.size(); i++) {
		int32 faceIndex1 = _edges[i].getFaceIndex1();
		int32 faceIndex2 = _edges[i].getFaceIndex2();

		if (faceIndex1 >= 0) {
			_faces[faceIndex1]->addEdge(&_edges[i]);
		}

		if (faceIndex2 >= 0) {
			_faces[faceIndex2]->addEdge(&_edges[i]);
		}
	}

	// Build a list of neighbours for each edge
	for (uint i = 0; i < _edges.size(); i++) {
		_edges[i].buildNeighbours(this);
		_edges[i].computeMiddle(this);
	}
}

void Floor::addFaceEdgeToList(uint32 faceIndex, uint32 index1, uint32 index2) {
	uint32 vertexIndex1 = _faces[faceIndex]->getVertexIndex(index1);
	uint32 vertexIndex2 = _faces[faceIndex]->getVertexIndex(index2);
	uint32 startIndex = MIN(vertexIndex1, vertexIndex2);
	uint32 endIndex = MAX(vertexIndex1, vertexIndex2);

	// Check if we already have an edge with the same vertices
	for (uint i = 0; i < _edges.size(); i++) {
		if (_edges[i].hasVertices(startIndex, endIndex)) {
			_edges[i].setOtherFace(faceIndex);
			return;
		}
	}

	_edges.push_back(FloorEdge(startIndex, endIndex, faceIndex));
}

void Floor::enableFloorField(FloorField *floorfield, bool enable) {
	for (uint i = 0; i < _faces.size(); i++) {
		if (floorfield->hasFace(i)) {
			_faces[i]->enable(enable);
		}
	}
}

void Floor::printData() {
	debug("face count: %d", _facesCount);

	Common::StreamDebug debug = streamDbg();
	for (uint i = 0; i < _vertices.size(); i++) {
		debug << i << ": " << _vertices[i] << "\n";
	}
}

FloorEdge::FloorEdge(uint16 vertexIndex1, uint16 vertexIndex2, uint32 faceIndex1) :
		_vertexIndex1(vertexIndex1),
		_vertexIndex2(vertexIndex2),
		_faceIndex1(faceIndex1),
		_faceIndex2(-1),
		_enabled(true) {
}

bool FloorEdge::hasVertices(uint16 vertexIndex1, uint16 vertexIndex2) const {
	return _vertexIndex1 == vertexIndex1 && _vertexIndex2 == vertexIndex2;
}

void FloorEdge::setOtherFace(uint32 faceIndex) {
	_faceIndex2 = faceIndex;
}

Common::Array<FloorEdge *> FloorEdge::getNeighbours() const {
	return _neighbours;
}

float FloorEdge::costTo(const FloorEdge *other) const {
	return _middle.getDistanceTo(other->_middle);
}

Math::Vector3d FloorEdge::getPosition() const {
	return _middle;
}

void FloorEdge::buildNeighbours(const Floor *floor) {
	_neighbours.clear();

	if (_faceIndex1 >= 0) {
		addNeighboursFromFace(floor->getFace(_faceIndex1));
	}

	if (_faceIndex2 >= 0) {
		addNeighboursFromFace(floor->getFace(_faceIndex2));
	}
}

void FloorEdge::addNeighboursFromFace(const FloorFace *face) {
	Common::Array<FloorEdge *> faceEdges = face->getEdges();
	for (uint i = 0; i < faceEdges.size(); i++) {
		if (faceEdges[i] != this) {
			_neighbours.push_back(faceEdges[i]);
		}
	}
}

void FloorEdge::computeMiddle(const Floor *floor) {
	Math::Vector3d vertex1 = floor->getVertex(_vertexIndex1);
	Math::Vector3d vertex2 = floor->getVertex(_vertexIndex2);
	_middle = (vertex1 + vertex2) / 2.0;
}

int32 FloorEdge::getFaceIndex1() const {
	return _faceIndex1;
}

int32 FloorEdge::getFaceIndex2() const {
	return _faceIndex2;
}

bool FloorEdge::isFloorBorder() const {
	return _faceIndex2 == -1;
}

bool FloorEdge::intersectLine2d(const Math::Line3d &s1, const Math::Line3d &s2) {
	const Math::Vector3d &s1begin = s1.begin();
	const Math::Vector3d &s1end = s1.end();
	const Math::Vector3d &s2begin = s2.begin();
	const Math::Vector3d &s2end = s2.end();

	float denom = ((s2end.y() - s2begin.y()) * (s1end.x() - s1begin.x())) -
	              ((s2end.x() - s2begin.x()) * (s1end.y() - s1begin.y()));

	float nume_a = ((s2end.x() - s2begin.x()) * (s1begin.y() - s2begin.y())) -
	               ((s2end.y() - s2begin.y()) * (s1begin.x() - s2begin.x()));

	float nume_b = ((s1end.x() - s1begin.x()) * (s1begin.y() - s2begin.y())) -
	               ((s1end.y() - s1begin.y()) * (s1begin.x() - s2begin.x()));

	if (denom == 0.0f) {
		return false; // Segments are collinear
	}

	float ua = nume_a / denom;
	float ub = nume_b / denom;

	// Non inclusive bounds check, one of the vertices of one segment being inside
	// the other segment is not considered to be an intersection.
	// This is the only difference with Line3d::intersectLine2d.
	return ua > 0 && ua < 1 && ub > 0 && ub < 1;
}

bool FloorEdge::intersectsSegment(const Floor *floor, const Math::Line3d &segment) const {
	Math::Vector3d vertex1 = floor->getVertex(_vertexIndex1);
	Math::Vector3d vertex2 = floor->getVertex(_vertexIndex2);
	Math::Line3d edgeSegment = Math::Line3d(vertex1, vertex2);

	return intersectLine2d(edgeSegment, segment);
}

void FloorEdge::enable(bool e) {
	_enabled = e;
}

bool FloorEdge::isEnabled() const {
	return _enabled;
}

void FloorEdge::saveLoad(ResourceSerializer *serializer) {
	serializer->syncAsUint32LE(_enabled);
}

} // End of namespace Resources
} // End of namespace Stark