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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/* 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 "startrek/startrek.h"
namespace StarTrek {
void StarTrekEngine::initStarfieldPosition() {
_starfieldPosition = Point3(0, 0, 0);
_someMatrix = initMatrix();
}
void StarTrekEngine::initStarfield(int16 x, int16 y, int16 width, int16 height, int16 arg8) {
// TODO: finish
_starfieldXVar1 = (x + width) / 2;
_starfieldYVar1 = (y + height) / 2;
_starfieldXVar2 = (width - x + 1) / 2;
_starfieldYVar2 = (height - y + 1) / 2;
_starfieldRect.left = _starfieldXVar1 - _starfieldXVar2;
_starfieldRect.right = _starfieldXVar1 + _starfieldXVar2;
_starfieldRect.top = _starfieldYVar1 - _starfieldYVar2;
_starfieldRect.bottom = _starfieldYVar1 + _starfieldYVar2;
memset(_starList, 0, sizeof(_starList));
_starfieldPointDivisor = 150;
_flt_50898 = 50.0;
}
void StarTrekEngine::addR3(R3 *r3) {
for (int i = 0; i < NUM_SPACE_OBJECTS; i++) {
if (_r3List[i] == nullptr) {
_r3List[i] = r3;
return;
}
}
error("addR3: out of shapes.");
}
void StarTrekEngine::delR3(R3 *r3) {
for (int i = 0; i < NUM_SPACE_OBJECTS; i++) {
if (_r3List[i] == r3) {
_r3List[i] = nullptr;
r3->field1e = 0;
return;
}
}
error("delR3: shape not found.");
}
void StarTrekEngine::clearStarfieldPixels() {
_gfx->fillBackgroundRect(_starfieldRect, 0);
}
void StarTrekEngine::drawStarfield() {
int16 var28 = ((_starfieldXVar2 * 3) >> 1);
int16 xvar = var28 / 2;
int16 var2a = ((_starfieldYVar2 * 3) >> 1);
int16 yvar = var2a / 2;
int16 var8 = _starfieldPointDivisor << 3;
SharedPtr<FileStream> file = loadFile("stars.shp");
for (int i = 0; i < NUM_STARS; i++) {
Star *star = &_starList[i];
if ((i & 0xf) == 0) {
file->seek(0, SEEK_SET);
}
if (!star->active) {
int16 var4 = getRandomWord() / var28 - xvar;
int16 var6 = getRandomWord() / var2a - yvar;
Point3 point = constructPoint3ForStarfield(var4, var6, var8);
star->pos = matrixMult(_starPositionMatrix, point) + _starfieldPosition;
star->active = true;
}
Point3 p = star->pos - _starfieldPosition;
Point3 point2 = matrixMult(p, _starPositionMatrix);
if (point2.z > _flt_50898 && point2.z < 0x3fff
&& abs(point2.x) < point2.z && abs(point2.y) < point2.z) {
int16 x = _starfieldXVar1 + scaleSpacePosition(point2.x, point2.z);
int16 y = _starfieldYVar1 - scaleSpacePosition(point2.y, point2.z);
int fileOffset = file->pos();
file->readUint32();
int16 width = file->readUint16();
int16 height = file->readUint16();
Common::Rect starRect(x, y, x + width, y + height);
Common::Rect drawRect = _starfieldRect.findIntersectingRect(starRect);
file->seek(fileOffset, SEEK_SET);
SharedPtr<Bitmap> bitmap = SharedPtr<Bitmap>(new Bitmap(file));
if (!drawRect.isEmpty())
_gfx->drawBitmapToBackground(starRect, drawRect, bitmap);
bitmap.reset();
} else {
star->active = false;
file->readUint32();
int16 offset2 = file->readUint16() * file->readUint16();
file->seek(file->pos() + offset2, SEEK_SET);
}
}
}
/**
* Compare 2 r3 objects for the purpose of sorting them by layer before drawing.
* FIXME: Original returned an int, not a bool. This may affect the stability of the sort...
*/
bool compareR3Objects(R3 *obj1, R3 *obj2) {
int32 diff = obj1->field54 - obj2->field54;
if (diff < 0)
return true;
else if (diff == 0)
return false; // original would have a distinct value for this
else
return false;
}
void StarTrekEngine::updateStarfieldAndShips(bool arg0) {
bool enterpriseDestroyed = false;
_starfieldSprite.bitmapChanged = true;
_starPositionMatrix = _someMatrix.invert();
clearStarfieldPixels();
drawStarfield();
int numObjects = 0;
for (int i = 0; i < NUM_SPACE_OBJECTS; i++) {
R3 *r3 = _r3List[i];
if (r3 == nullptr)
continue;
r3->field34 = 0;
switch (r3->field1e) { // TODO
case 1:
r3 = sub_19f24(r3);
break;
case 2: // TODO (specifically for the enterprise?)
break;
case 3:
r3 = sub_19f24(r3);
break;
}
if (r3 != nullptr)
_orderedR3List[numObjects++] = r3;
}
if (numObjects != 0) {
Common::sort(_orderedR3List, _orderedR3List + numObjects, &compareR3Objects);
for (int i = 0; i < numObjects; i++) {
R3 *r3 = _orderedR3List[i];
r3->field34 = 1;
switch (r3->field1e) { // TODO
case 1:
break;
case 2:
break;
case 3:
if (r3->field20 == 1) {
// TODO
} else
drawR3Shape(r3);
break;
}
}
}
if (enterpriseDestroyed) {
showTextbox("", "GENE\\SPACE000#The Enterprise has been destroyed!", 20, 20, TEXTCOLOR_YELLOW, 0);
showGameOverMenu();
// FIXME: original game manipulates stack to jump somewhere...
return;
}
// TODO: static on enterprise viewscreen?
}
R3 *StarTrekEngine::sub_19f24(R3 *r3) {
r3->matrix2 = r3->matrix;
r3->field36 = r3->pos;
r3->field36 -= _starfieldPosition;
r3->field54 = r3->field36.getDiagonal();
Point3 point = matrixMult(r3->field36, _starPositionMatrix);
r3->field36 = point;
if (sub_1c022(r3)) {
if (r3->field1e >= 1 && r3->field1e <= 3)
r3->matrix2 *= _starPositionMatrix;
r3->field58 = _starfieldXVar1 + scaleSpacePosition(r3->field36.x, r3->field36.z);
r3->field5a = _starfieldYVar1 - scaleSpacePosition(r3->field36.y, r3->field36.z);
return r3;
} else
return nullptr;
}
void StarTrekEngine::drawR3Shape(R3 *r3) {
if (r3->field1e != 3) // TODO: remove this
return;
if (r3->funcPtr1 != 0) {
// TODO: call it
}
if (r3->shpFile != nullptr) {
r3->shpFile->seek(r3->bitmapOffset, SEEK_SET);
SharedPtr<Bitmap> bitmap(new Bitmap(r3->shpFile));
double dbl68 = ((double)r3->field24 * _starfieldPointDivisor) / r3->field36.z;
double dbl70 = 1.0 / dbl68;
double dbl30 = 1.0;
double dbl28 = 0.0;
double dbl20 = 0.0;
double dbl18 = 1.0;
double dbl60 = 1.0;
double dbl58 = 0.0;
double dbl50 = 0.0;
double dbl48 = 0.0;
switch (r3->field1e) {
case 1: // TODO
break;
case 3:
dbl30 = dbl68;
dbl28 = 0.0;
dbl20 = 0.0;
dbl18 = dbl68;
dbl60 = dbl70;
dbl58 = 0.0;
dbl50 = 0.0;
dbl48 = dbl70;
break;
case 4: // TODO
break;
default: // TODO
break;
}
r3->field80 = dbl60;
r3->field88 = dbl58;
r3->field90 = dbl50;
r3->field98 = dbl48;
// dbl30, (bitmap->xoffset + 2), r3->field58,
double tmp = r3->field58 - (double)(bitmap->xoffset + 2) * dbl30;
// dbl20, (bitmap->yoffset + 2), tmp
double dbl10 = tmp - (double)(bitmap->yoffset + 2) * dbl20;
// dbl28, (bitmap->xoffset + 2), r3->field5a
tmp = r3->field5a - (double)(bitmap->xoffset + 2) * dbl28;
// dbl18, (bitmap->yoffset + 2), tmp
double dbl8 = tmp - (double)(bitmap->yoffset + 2) * dbl18;
// dbl60, r3->field58, bitmap->xoffset + 2
tmp = (bitmap->xoffset + 2) - dbl60 * r3->field58;
double dbl40 = tmp - dbl50 * r3->field5a;
tmp = (bitmap->yoffset + 2) - dbl58 * r3->field58;
double dbl38 = tmp - dbl48 * r3->field5a;
double dbl3e4 = bitmap->width + 2;
double dbl3ec = bitmap->height + 2;
double thing[8];
tmp = 1.0 * dbl30;
thing[0] = 1.0 * dbl20 + tmp + dbl10;
tmp = 1.0 * dbl28;
thing[1] = 1.0 * dbl18 + tmp + dbl8;
tmp = 1.0 * dbl30;
thing[2] = dbl3ec * dbl20 + tmp + dbl10;
tmp = 1.0 * dbl28;
thing[3] = dbl3ec * dbl18 + tmp + dbl8;
thing[4] = dbl3ec * dbl20 + dbl3e4 * dbl30 + dbl10;
thing[5] = dbl3ec * dbl18 + dbl3e4 * dbl28 + dbl8;
thing[6] = 1.0 * dbl20 + dbl3e4 * dbl30 + dbl10;
thing[7] = 1.0 * dbl18 + dbl3e4 * dbl28 + dbl8;
int16 *rightBounds = new int16[SCREEN_HEIGHT];
int16 *leftBounds = new int16[SCREEN_HEIGHT];
for (int y = _starfieldRect.top; y < _starfieldRect.bottom; y++) {
leftBounds[y] = _starfieldRect.right;
rightBounds[y] = _starfieldRect.left - 1;
}
int16 shpImageTop = _starfieldRect.bottom - 1;
int16 shpImageBottom = _starfieldRect.top;
for (int i = 0; i < 4; i++) {
int16 index1 = i;
int16 index2 = (i + 1) & 3;
if (thing[index1 + 1] > thing[index2 + 1]) {
index1 = index2;
index2 = i;
}
int16 top = ceil(thing[index1 + 1]);
int16 bottom = floor(thing[index2 + 1]);
if (top > bottom)
continue;
if (top > _starfieldRect.bottom - 1)
continue;
if (bottom < _starfieldRect.top)
continue;
if (top < _starfieldRect.top)
top = _starfieldRect.top;
if (bottom > _starfieldRect.bottom - 1)
bottom = _starfieldRect.bottom - 1;
if (top < shpImageTop)
shpImageTop = top;
if (bottom > shpImageBottom)
shpImageBottom = bottom;
double dbl3f4;
if (thing[index2 + 1] == thing[index1 + 1])
dbl3f4 = 0.0;
else
dbl3f4 = (thing[index2] - thing[index1]) / (thing[index2 + 1] - thing[index1 + 1]);
int32 var3ec = (int32)(0x10000 * dbl3f4);
int32 var3e8 = (int32)(((top - thing[index1 + 1]) * dbl3f4 + thing[index1]) * 0x10000);
for (int y = top; y <= bottom; y++) {
int16 var3f6 = var3e8 >> 16;
int16 var3f8 = (var3e8 + 0xffff) >> 16;
if (var3f8 < _starfieldRect.left)
var3f8 = _starfieldRect.left;
if (var3f8 < leftBounds[y])
leftBounds[y] = var3f8;
if (var3f6 > _starfieldRect.right - 1)
var3f6 = _starfieldRect.right - 1;
if (var3f6 > rightBounds[y])
rightBounds[y] = var3f6;
var3e8 += var3ec;
}
}
while (shpImageTop <= shpImageBottom) {
if (leftBounds[shpImageTop] > rightBounds[shpImageTop])
shpImageTop++;
else
break;
}
while (shpImageTop <= shpImageBottom) {
if (leftBounds[shpImageBottom] > rightBounds[shpImageBottom])
shpImageBottom--;
else
break;
}
debug("Top: %d, Bot: %d", shpImageTop, shpImageBottom);
if (shpImageTop <= shpImageBottom) {
bool var3fa = false;
if (r3->field1e == 2) {
// TODO
} else
var3fa = (((r3->field24 * _starfieldPointDivisor) << 1) / 3 <= r3->field36.z);
if (!var3fa) {
if (r3->field1e == 3) {
// Is this used anywhere?
//uint16 var3fc = (_frameIndex << 3) ^ _frameIndex;
} else {
// TODO
}
}
int16 xDiff = (int16)(dbl60 * 256);
int16 yDiff = (int16)(dbl58 * 256);
int16 var3f2 = (int16)(dbl50 * 256);
int16 var3f4 = (int16)(dbl48 * 256);
int16 var3f6 = var3f2 * shpImageTop + (int16)(dbl40 * 256);
int16 var3f8 = var3f4 * shpImageTop + (int16)(dbl38 * 256);
Bitmap tmpBitmap(256, 249);
byte *otherBuffer = new byte[256 * 256];
int16 bitmapWidth = bitmap->width;
int16 bitmapHeight = bitmap->height;
if (bitmapHeight > 245)
error("Shape height too big in drawR3Shape!");
if (bitmapWidth > 252)
error("Shape width too big in drawR3Shape!");
for (int i = 0; i < 2; i++)
memset(otherBuffer + i * 256, 0, bitmapWidth + 4);
for (int i = 0; i < 2; i++)
memset(otherBuffer + (bitmapHeight + 2 + i) * 256, 0, bitmapWidth + 4);
for (int i = 0; i < bitmapHeight; i++) {
otherBuffer[512 + i * 256 + 0] = 0;
otherBuffer[512 + i * 256 + 1] = 0;
otherBuffer[512 + i * 256 + bitmapWidth + 2] = 0;
otherBuffer[512 + i * 256 + bitmapWidth + 3] = 0;
}
if (r3->field1e == 2) {
// TODO
} else
_gfx->copyRectBetweenBitmaps(&tmpBitmap, 2, 2, bitmap.get(), 0, 0, bitmapWidth, bitmapHeight);
byte *bgPixels = _gfx->getBackgroundPixels() + shpImageTop * SCREEN_WIDTH;
for (int y = shpImageTop; y <= shpImageBottom; y++) {
int16 leftBound = leftBounds[y];
int16 rowWidth = rightBounds[y] - leftBound;
int16 srcX = leftBound * xDiff + var3f6;
int16 srcY = leftBound * yDiff + var3f8;
var3f6 += var3f2;
var3f8 += var3f4;
byte *bgPixels2 = bgPixels + leftBound;
bgPixels += SCREEN_WIDTH;
if (rowWidth == 0)
continue;
debug("Width: %d", rowWidth);
if (var3fa) {
srcX += 0x80;
srcY += 0x80;
int16 cx = srcX;
int16 bx = srcY;
byte *di = bgPixels2;
for (int a = 0; a < rowWidth; a++) {
byte b = tmpBitmap.pixels[(bx & 0xff00) + (cx >> 8)];
cx += xDiff;
bx += yDiff;
if (b == 0)
*(di++) = 8; // FIXME: shouldn't assign anything, fix after done testing
else
*(di++) = b;
}
} else {
warning("Unimplemented branch in \"drawR3Shape\"");
}
}
delete[] otherBuffer;
}
delete[] rightBounds;
delete[] leftBounds;
}
if (r3->funcPtr2 != 0) {
// TODO: call it
}
}
bool StarTrekEngine::sub_1c022(R3 *r3) {
Point3 point = r3->field36;
if (point.z < _flt_50898)
return false;
return true; // TODO: finish this properly
}
Point3 StarTrekEngine::constructPoint3ForStarfield(int16 x, int16 y, int16 z) {
Point3 point;
point.z = z;
point.y = y * z / _starfieldPointDivisor;
point.x = x * z / _starfieldPointDivisor;
return point;
}
Point3 StarTrekEngine::matrixMult(const Matrix &weight, const Point3 &point) {
Point3 p;
for (int i = 0; i < 3; i++) {
p[i] = 0;
for (int j = 0; j < 3; j++)
p[i] += (int16)weight[i][j].multToInt(point[j]);
}
return p;
}
Point3 StarTrekEngine::matrixMult(const Point3 &point, const Matrix &weight) {
Point3 p = Point3();
for (int i = 0; i < 3; i++) {
p[i] = 0;
for (int j = 0; j < 3; j++)
p[i] += (int16)weight[j][i].multToInt(point[j]);
}
return p;
}
int32 StarTrekEngine::scaleSpacePosition(int32 x, int32 z) {
return (x * _starfieldPointDivisor) / z;
}
Matrix StarTrekEngine::initMatrix() {
Matrix mat;
mat[0][0] = 1.0;
mat[1][1] = 1.0;
mat[2][2] = 1.0;
return mat;
}
Matrix StarTrekEngine::initSpeedMatrixForXZMovement(Angle angle, const Matrix &matrix) {
Fixed14 sinVal = sin(angle);
Fixed14 cosVal = cos(angle);
Matrix matrix1 = initMatrix();
matrix1[0].x = cosVal;
matrix1[0].z = -sinVal;
matrix1[2].x = sinVal;
matrix1[2].z = cosVal;
return matrix * matrix1;
}
} // End of namespace StarTrek
|