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
|
/* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "bladerunner/scene_objects.h"
#include "bladerunner/bladerunner.h"
#include "bladerunner/obstacles.h"
#include "bladerunner/view.h"
namespace BladeRunner {
SceneObjects::SceneObjects(BladeRunnerEngine *vm, View *view) {
_vm = vm;
_view = view;
_count = 0;
for (int i = 0; i < kSceneObjectCount; ++i) {
_sceneObjectsSortedByDistance[i] = -1;
}
}
SceneObjects::~SceneObjects() {
_vm = nullptr;
_view = nullptr;
_count = 0;
}
void SceneObjects::clear() {
for (int i = 0; i < kSceneObjectCount; ++i) {
_sceneObjects[i].id = -1;
_sceneObjects[i].type = kSceneObjectTypeUnknown;
_sceneObjects[i].distanceToCamera = 0.0f;
_sceneObjects[i].isPresent = false;
_sceneObjects[i].isClickable = false;
_sceneObjects[i].isObstacle = false;
_sceneObjects[i].unknown1 = 0;
_sceneObjects[i].isTarget = false;
_sceneObjects[i].isMoving = false;
_sceneObjects[i].isRetired = false;
_sceneObjectsSortedByDistance[i] = -1;
}
_count = 0;
}
bool SceneObjects::addActor(int sceneObjectId, BoundingBox *boundingBox, Common::Rect *screenRectangle, bool isClickable, bool isMoving, bool isTarget, bool isRetired) {
return addSceneObject(sceneObjectId, kSceneObjectTypeActor, boundingBox, screenRectangle, isClickable, false, 0, isTarget, isMoving, isRetired);
}
bool SceneObjects::addObject(int sceneObjectId, BoundingBox *boundingBox, bool isClickable, bool isObstacle, uint8 unknown1, bool isTarget) {
Common::Rect rect(-1, -1, -1, -1);
return addSceneObject(sceneObjectId, kSceneObjectTypeObject, boundingBox, &rect, isClickable, isObstacle, unknown1, isTarget, false, false);
}
bool SceneObjects::addItem(int sceneObjectId, BoundingBox *boundingBox, Common::Rect *screenRectangle, bool isTarget, bool isObstacle) {
return addSceneObject(sceneObjectId, kSceneObjectTypeItem, boundingBox, screenRectangle, isObstacle, 0, 0, isTarget, 0, 0);
}
bool SceneObjects::remove(int sceneObjectId) {
int i = findById(sceneObjectId);
if (i == -1) {
return false;
}
_sceneObjects[i].isPresent = false;
int j;
for (j = 0; j < _count; ++j) {
if (_sceneObjectsSortedByDistance[j] == i) {
break;
}
}
for (int k = j; k < _count - 1; ++k) {
_sceneObjectsSortedByDistance[k] = _sceneObjectsSortedByDistance[k + 1];
}
--_count;
return true;
}
int SceneObjects::findByXYZ(bool *isClickable, bool *isObstacle, bool *isTarget, Vector3 &position, bool findClickables, bool findObstacles, bool findTargets) const {
*isClickable = false;
*isObstacle = false;
*isTarget = false;
for (int i = 0; i < _count; ++i) {
assert(_sceneObjectsSortedByDistance[i] < kSceneObjectCount);
const SceneObject *sceneObject = &_sceneObjects[_sceneObjectsSortedByDistance[i]];
if ((findClickables && sceneObject->isClickable) ||
(findObstacles && sceneObject->isObstacle) ||
(findTargets && sceneObject->isTarget)) {
BoundingBox boundingBox = *sceneObject->boundingBox;
if (sceneObject->type == kSceneObjectTypeActor) {
boundingBox.expand(-4.0, 0.0, -4.0, 4.0, 0.0, 4.0);
}
if (boundingBox.inside(position)) {
*isClickable = sceneObject->isClickable;
*isObstacle = sceneObject->isObstacle;
*isTarget = sceneObject->isTarget;
return sceneObject->id;
}
}
}
return -1;
}
bool SceneObjects::existsOnXZ(int exceptSceneObjectId, float x, float z, bool movingActorIsObstacle, bool standingActorIsObstacle) const {
float xMin = x - 12.5f;
float xMax = x + 12.5f;
float zMin = z - 12.5f;
float zMax = z + 12.5f;
int count = _count;
if (count > 0) {
for (int i = 0; i < count; i++) {
const SceneObject *sceneObject = &_sceneObjects[_sceneObjectsSortedByDistance[i]];
bool isObstacle = false;
if (sceneObject->type == kSceneObjectTypeActor) {
if (sceneObject->isRetired) {
isObstacle = false;
} else if (sceneObject->isMoving) {
isObstacle = movingActorIsObstacle != 0;
} else {
isObstacle = standingActorIsObstacle != 0;
}
} else {
isObstacle = sceneObject->isObstacle;
}
if (isObstacle && sceneObject->id != exceptSceneObjectId) {
float x1, y1, z1, x2, y2, z2;
sceneObject->boundingBox->getXYZ(&x1, &y1, &z1, &x2, &y2, &z2);
if (z1 <= zMax && z2 >= zMin && x1 <= xMax && x2 >= xMin) {
return true;
}
}
}
}
return false;
}
int SceneObjects::findById(int sceneObjectId) const {
for (int i = 0; i < _count; ++i) {
int j = this->_sceneObjectsSortedByDistance[i];
if (_sceneObjects[j].isPresent && _sceneObjects[j].id == sceneObjectId) {
return j;
}
}
return -1;
}
bool SceneObjects::addSceneObject(int sceneObjectId, SceneObjectType sceneObjectType, BoundingBox *boundingBox, Common::Rect *screenRectangle, bool isClickable, bool isObstacle, uint8 unknown1, bool isTarget, bool isMoving, bool isRetired) {
int index = findEmpty();
if (index == -1) {
return false;
}
_sceneObjects[index].id = sceneObjectId;
_sceneObjects[index].type = sceneObjectType;
_sceneObjects[index].isPresent = true;
_sceneObjects[index].boundingBox = boundingBox;
_sceneObjects[index].screenRectangle = screenRectangle;
_sceneObjects[index].isClickable = isClickable;
_sceneObjects[index].isObstacle = isObstacle;
_sceneObjects[index].unknown1 = unknown1;
_sceneObjects[index].isTarget = isTarget;
_sceneObjects[index].isMoving = isMoving;
_sceneObjects[index].isRetired = isRetired;
float centerZ = (_sceneObjects[index].boundingBox->getZ0() + _sceneObjects[index].boundingBox->getZ1()) / 2.0f;
float distanceToCamera = fabs(-centerZ - _view->_cameraPosition.y); // y<->z is intentional, not a bug
_sceneObjects[index].distanceToCamera = distanceToCamera;
// insert according to distance from camera
int i;
for (i = 0; i < _count; ++i) {
if (distanceToCamera < _sceneObjects[_sceneObjectsSortedByDistance[i]].distanceToCamera) {
break;
}
}
for (int j = CLIP(_count - 1, 0, kSceneObjectCount - 2); j >= i; --j) {
_sceneObjectsSortedByDistance[j + 1] = _sceneObjectsSortedByDistance[j];
}
_sceneObjectsSortedByDistance[i] = index;
++_count;
return true;
}
int SceneObjects::findEmpty() const {
for (int i = 0; i < kSceneObjectCount; ++i) {
if (!_sceneObjects[i].isPresent)
return i;
}
return -1;
}
void SceneObjects::setMoving(int sceneObjectId, bool isMoving) {
int i = findById(sceneObjectId);
if (i == -1) {
return;
}
_sceneObjects[i].isMoving = isMoving;
}
void SceneObjects::setRetired(int sceneObjectId, bool isRetired) {
int i = findById(sceneObjectId);
if (i == -1) {
return;
}
_sceneObjects[i].isRetired = isRetired;
}
bool SceneObjects::isBetween(float sourceX, float sourceZ, float targetX, float targetZ, int sceneObjectId) const {
int i = findById(sceneObjectId);
if (i == -1) {
return false;
}
float objectX1, objectY1, objectZ1, objectX2, objectY2, objectZ2;
_sceneObjects[i].boundingBox->getXYZ(&objectX1, &objectY1, &objectZ1, &objectX2, &objectY2, &objectZ2);
Vector2 intersection;
return lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX1, objectZ1), Vector2(objectX2, objectZ1), &intersection)
|| lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX2, objectZ1), Vector2(objectX2, objectZ2), &intersection)
|| lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX2, objectZ2), Vector2(objectX1, objectZ2), &intersection)
|| lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX1, objectZ2), Vector2(objectX1, objectZ1), &intersection);
}
bool SceneObjects::isObstacleBetween(float sourceX, float sourceZ, float targetX, float targetZ, float altitude, int exceptSceneObjectId) const {
for (int i = 0; i < _count; ++i) {
const SceneObject *sceneObject = &_sceneObjects[_sceneObjectsSortedByDistance[i]];
if (sceneObject->type == kSceneObjectTypeActor || !sceneObject->isObstacle || sceneObject->id == exceptSceneObjectId) {
continue;
}
float objectX1, objectY1, objectZ1, objectX2, objectY2, objectZ2;
_sceneObjects[i].boundingBox->getXYZ(&objectX1, &objectY1, &objectZ1, &objectX2, &objectY2, &objectZ2);
if (84.0f <= objectY1 - altitude || 72.0f >= objectY2 - altitude) {
continue;
}
float xAdjustement = (objectX2 - objectX1) * 0.1f;
float zAdjustement = (objectZ2 - objectZ1) * 0.1f;
objectX1 = objectX1 + xAdjustement;
objectZ1 = objectZ1 + zAdjustement;
objectX2 = objectX2 - xAdjustement;
objectZ2 = objectZ2 - zAdjustement;
Vector2 intersection;
if (lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX1, objectZ1), Vector2(objectX2, objectZ1), &intersection)
|| lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX2, objectZ1), Vector2(objectX2, objectZ2), &intersection)
|| lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX2, objectZ2), Vector2(objectX1, objectZ2), &intersection)
|| lineIntersection(Vector2(sourceX, sourceZ), Vector2(targetX, targetZ), Vector2(objectX1, objectZ2), Vector2(objectX1, objectZ1), &intersection)) {
return true;
}
}
return false;
}
void SceneObjects::setIsClickable(int sceneObjectId, bool isClickable) {
int i = findById(sceneObjectId);
if (i == -1) {
return;
}
_sceneObjects[i].isClickable = isClickable;
}
void SceneObjects::setIsObstacle(int sceneObjectId, bool isObstacle) {
int i = findById(sceneObjectId);
if (i == -1) {
return;
}
_sceneObjects[i].isObstacle = isObstacle;
}
void SceneObjects::setIsTarget(int sceneObjectId, bool isTarget) {
int i = findById(sceneObjectId);
if (i == -1) {
return;
}
_sceneObjects[i].isTarget = isTarget;
}
void SceneObjects::updateObstacles() {
_vm->_obstacles->clear();
for (int i = 0; i < _count; ++i) {
int index = _sceneObjectsSortedByDistance[i];
const SceneObject *sceneObject = &_sceneObjects[index];
if (sceneObject->isObstacle) {
float x0, y0, z0, x1, y1, z1;
sceneObject->boundingBox->getXYZ(&x0, &y0, &z0, &x1, &y1, &z1);
_vm->_obstacles->add(x0, z0, x1, z1);
}
}
_vm->_obstacles->backup();
}
} // End of namespace BladeRunner
|