forked from fuzzie/unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unity.cpp
1370 lines (1157 loc) · 38.8 KB
/
unity.cpp
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "unity.h"
#include "bridge.h"
#include "computer.h"
#include "conversation.h"
#include "graphics.h"
#include "sound.h"
#include "sprite_player.h"
#include "object.h"
#include "trigger.h"
#include "viewscreen.h"
#include "common/fs.h"
#include "common/config-manager.h"
#include "common/error.h"
#include "common/textconsole.h"
#include "engines/util.h"
#include "common/events.h"
#include "common/stream.h"
#include "common/system.h"
#include "common/unzip.h"
#include "common/file.h"
#include "common/archive.h"
#include "graphics/cursorman.h"
#include "graphics/font.h"
#include "common/debug-channels.h"
namespace Unity {
UnityEngine::UnityEngine(OSystem *syst) : Engine(syst), data(this) {
DebugMan.addDebugChannel(kDebugResource, "Resource", "Resource Debug Flag");
DebugMan.addDebugChannel(kDebugSaveLoad, "Saveload", "Saveload Debug Flag");
DebugMan.addDebugChannel(kDebugScript, "Script", "Script Debug Flag");
DebugMan.addDebugChannel(kDebugGraphics, "Graphics", "Graphics Debug Flag");
DebugMan.addDebugChannel(kDebugSound, "Sound", "Sound Debug Flag");
_in_dialog = false;
_dialog_choosing = false;
_icon = NULL;
_next_conversation = NULL;
_next_situation = 0xffffffff;
_beam_world = 0;
_mode = mode_Look;
_current_away_team_member = NULL;
_current_away_team_icon = NULL;
_inventory_index = 0;
_viewscreen_sprite_id = ~0; // should be set by startup scripts
// TODO: de-hardcode
_dialog_x = 100;
_dialog_y = 280;
_bridgeScreen = new BridgeScreen(this);
_computerScreen = new ComputerScreen(this);
_viewscreenScreen = new ViewscreenScreen(this);
_currScreen = NULL;
_currScreenType = NoScreenType;
}
UnityEngine::~UnityEngine() {
if (_currScreen)
_currScreen->shutdown();
delete _bridgeScreen;
delete _computerScreen;
delete _viewscreenScreen;
delete _snd;
delete _console;
delete _gfx;
// FIXME: Segfaults if deletion is done
//delete data.data;
delete _icon;
endAwayTeam();
DebugMan.clearAllDebugChannels();
delete _rnd;
}
Common::Error UnityEngine::init() {
_rnd = new Common::RandomSource("unity");
_gfx = new Graphics(this);
_console = new UnityConsole(this);
_snd = new Sound(this);
data._data = Common::makeZipArchive("STTNG.ZIP");
if (!data._data) {
error("couldn't open data file");
}
/*Common::ArchiveMemberList list;
data->listMembers(list);
for (Common::ArchiveMemberList::const_iterator file = list.begin(); file != list.end(); ++file) {
Common::String filename = (*file)->getName();
filename.toLowercase();
if (filename.hasSuffix(".spr") || filename.hasSuffix(".spt")) {
Common::SeekableReadStream *ourstr = (*file)->createReadStream();
debugN("trying '%s'\n", filename.c_str());
Sprite spr(ourstr);
}
}*/
SearchMan.add("sttngzip", data._data);
// DOS version only
data._instData = Common::makeZipArchive("STTNGINS.ZIP");
if (data._instData) {
SearchMan.add("sttnginszip", data._instData);
}
// Mac version only
const Common::FSNode gameDataDir(ConfMan.get("path"));
SearchMan.addDirectory(".movies", gameDataDir.getPath() + "/.movies");
return Common::kNoError;
}
void UnityEngine::openLocation(unsigned int world, unsigned int screen) {
_snd->stopMusic(); // FIXME: find a better place
Common::String filename = Common::String::format("sl%03d.scr", world);
Common::SeekableReadStream *locstream = data.openFile(filename);
uint16 num_entries = locstream->readUint16LE();
Common::HashMap<uint32, uint32> offsets;
for (unsigned int i = 0; i < num_entries; i++) {
uint32 id = locstream->readUint32LE();
uint32 offset = locstream->readUint32LE();
offsets[id] = offset;
}
if (screen > num_entries) {
error("no such screen %d in world %d (only %d screens)", screen, world, num_entries);
}
locstream->seek(offsets[screen]);
// XXX: null-terminated in file?
byte length = locstream->readByte();
char *background = (char *)alloca(length + 1);
locstream->read(background, length);
background[length] = 0;
_gfx->setBackgroundImage(background);
// XXX: null-terminated in file?
length = locstream->readByte();
char *polygons = (char *)alloca(length + 1);
locstream->read(polygons, length);
polygons[length] = 0;
data.loadScreenPolys(polygons);
data._currentScreen.entrypoints.clear();
byte num_entrances = locstream->readByte();
for (unsigned int e = 0; e < num_entrances; e++) {
Common::Array<Common::Point> entrypoints;
// XXX: read this properly
uint16 unknown = locstream->readUint16BE(); // 0001, 0101, 0201, ...
(void)unknown;
entrypoints.resize(4);
for (unsigned int i = 0; i < 4; i++) {
entrypoints[i].x = locstream->readUint16LE();
entrypoints[i].y = locstream->readUint16LE();
}
data._currentScreen.entrypoints.push_back(entrypoints);
}
delete locstream;
filename = Common::String::format("w%02x%02xobj.bst", world, screen);
locstream = data.openFile(filename);
while (true) {
uint16 counter = locstream->readUint16LE();
if (locstream->eos()) break;
objectID id = readObjectID(locstream);
assert(id.id == counter);
assert(id.screen == screen);
assert(id.world == world);
char _name[30], _desc[260];
locstream->read(_name, 30);
locstream->read(_desc, 260);
debug(6, "reading obj '%s' (%s)", _name, _desc);
Object *obj = data.getObject(id);
obj->loadSprite();
data._currentScreen.objects.push_back(obj);
}
delete locstream;
filename = Common::String::format("w_%02dstrt.bst", world);
locstream = data.openFile(filename);
locstream->seek(45 * screen);
if (locstream->eos()) error("no startup entry for screen %x (world %x)", screen, world);
// TODO: use these
uint16 advice_id = locstream->readUint16LE();
uint16 advice_timer = locstream->readUint16LE();
debug(6, "openLocation() advice_id: %d advice_timer: %d", advice_id, advice_timer);
byte unknown1 = locstream->readByte();
byte unknown2 = locstream->readByte();
debug(6, "openLocation() unknown1: %d unknown2: %d", unknown1, unknown2);
uint16 startup_screen = locstream->readUint16LE();
objectID target = readObjectID(locstream);
byte action_type = locstream->readByte();
objectID who = readObjectID(locstream);
objectID other = readObjectID(locstream);
uint32 unknown3 = locstream->readUint32LE();
uint16 unknown4 = locstream->readUint16LE();
uint16 unknown5 = locstream->readUint16LE();
byte unknown6 = locstream->readByte();
debug(6, "openLocation() unknown3: %d unknown4: %d", unknown3, unknown4);
debug(6, "openLocation() unknown5: %d unknown6: %d", unknown5, unknown6);
if (startup_screen != 0xffff) {
assert(startup_screen == screen);
if (target.id != 0xff) {
// TODO: only fire this on first load
ActionType action_id;
switch (action_type) {
case 0: action_id = ACTION_USE; break;
case 1: action_id = ACTION_GET; break;
case 2: action_id = ACTION_LOOK; break;
case 3: action_id = ACTION_GET; break;
case 4: action_id = ACTION_WALK; break;
case 5: action_id = ACTION_TALK; break;
case 6: action_id = ACTION_USE; break;
default: error("unknown action type %x when starting screen %x of world %x",
action_type, screen, world);
}
// TODO: run delayed
performAction(action_id, data.getObject(target), who, other);
}
}
delete locstream;
}
#define DIRECTION(x, y, x1, y1, x2, y2) (((int)y - y1)*(x2 - x1) - ((int)x - x1)*(y2 - y1))
bool ScreenPolygon::insideTriangle(unsigned int x, unsigned int y, unsigned int &triangle) {
for (unsigned int i = 0; i < triangles.size(); i++) {
int direction = DIRECTION(x, y, triangles[i].points[0].x, triangles[i].points[0].y, triangles[i].points[1].x, triangles[i].points[1].y);
bool neg = direction < 0;
direction = DIRECTION(x, y, triangles[i].points[1].x, triangles[i].points[1].y, triangles[i].points[2].x, triangles[i].points[2].y);
if ((direction < 0) != neg) continue;
direction = DIRECTION(x, y, triangles[i].points[2].x, triangles[i].points[2].y, triangles[i].points[0].x, triangles[i].points[0].y);
if ((direction < 0) != neg) continue;
triangle = i;
return true;
}
return false;
}
struct DrawOrderComparison {
bool operator() (const Object *a, const Object *b) {
int ay = a->y, by = b->y;
if (a->y_adjust != -1) ay += a->y_adjust;
if (b->y_adjust != -1) by += b->y_adjust;
return ay < by;
}
};
Object *UnityEngine::objectAt(unsigned int x, unsigned int y) {
Common::Array<Object *> &objects = data._currentScreen.objects;
for (unsigned int i = 0; i < objects.size(); i++) {
if (!(objects[i]->flags & OBJFLAG_ACTIVE)) continue;
if (objects[i]->flags & OBJFLAG_INVENTORY) continue;
if (objects[i]->width == (unsigned int)~0) continue;
// TODO: should we be doing this like this, or keeping track of original coords, or what?
if (objects[i]->x - objects[i]->width/2 > x) continue;
if (objects[i]->x + objects[i]->width/2 < x) continue;
if (objects[i]->y - objects[i]->height > y) continue;
if (objects[i]->y < y) continue;
return objects[i];
}
return 0;
}
void UnityEngine::clearObjects() {
data._currentScreen.objects.clear();
}
void UnityEngine::removeObject(Object *obj) {
Common::Array<Object *> &objects = data._currentScreen.objects;
for (uint i = 0; i < objects.size(); i++) {
if (objects[i] != obj)
continue;
objects.remove_at(i);
return;
}
error("removeObject failed to find object");
}
void UnityEngine::startBridge() {
endAwayTeam();
changeToScreen(BridgeScreenType);
}
void UnityEngine::endAwayTeam() {
_current_away_team_member = NULL;
delete _current_away_team_icon;
_current_away_team_icon = NULL;
_away_team_members.clear();
_inventory_items.clear();
for (unsigned int i = 0; i < _inventory_icons.size(); i++) {
delete _inventory_icons[i];
}
_inventory_icons.clear();
_on_away_team = false;
}
void UnityEngine::addToInventory(Object *obj) {
assert(_inventory_items.size() == _inventory_icons.size());
if (Common::find(_inventory_items.begin(), _inventory_items.end(), obj) != _inventory_items.end()) {
error("trying to add %s to inventory but it's already there", obj->identify().c_str());
}
obj->flags |= OBJFLAG_INVENTORY;
_inventory_items.push_back(obj);
Common::String icon_sprite = data.getIconSprite(obj->id);
if (!icon_sprite.size()) {
error("couldn't find icon sprite for inventory item %s", obj->identify().c_str());
}
SpritePlayer *icon = NULL;
icon = new SpritePlayer(icon_sprite.c_str(), NULL, this);
icon->startAnim(0); // static
_inventory_icons.push_back(icon);
}
void UnityEngine::removeFromInventory(Object *obj) {
obj->flags &= ~OBJFLAG_INVENTORY;
unsigned int i;
for (i = 0; i < _inventory_items.size(); i++) {
if (_inventory_items[i] == obj) {
_inventory_items.remove_at(i);
break;
}
}
if (i == _inventory_items.size()) {
error("sync error while trying to remove %s from inventory", obj->identify().c_str());
}
delete _inventory_icons[i];
_inventory_icons.remove_at(i);
assert(_inventory_items.size() == _inventory_icons.size());
}
void UnityEngine::startAwayTeam(unsigned int world, unsigned int screen, byte entrance) {
if (_currScreen)
_currScreen->shutdown();
_currScreen = NULL;
_currScreenType = NoScreenType;
clearObjects();
data._currentScreen.world = world;
data._currentScreen.screen = screen;
if (!_away_team_members.size()) {
// TODO: move this somewhere sensible!
for (unsigned int i = 0; i < 4; i++) {
objectID objid(i, 0, 0);
Object *obj = data.getObject(objid);
obj->loadSprite();
_away_team_members.push_back(obj);
}
_current_away_team_member = _away_team_members[0];
Common::String icon_sprite = data.getIconSprite(_current_away_team_member->id);
if (!icon_sprite.size()) {
error("couldn't find icon sprite for away team member %s",
_current_away_team_member->identify().c_str());
}
_current_away_team_icon = new SpritePlayer(icon_sprite.c_str(), NULL, this);
_current_away_team_icon->startAnim(0); // static
for (unsigned int i = 0; i < 5; i++) {
if (i == 2) continue; // TODO: handle communicator
objectID objid(i, 0x70, 0);
Object *obj = data.getObject(objid);
if (!(obj->flags & OBJFLAG_ACTIVE)) continue;
addToInventory(obj);
}
} else {
assert(_current_away_team_member);
}
// beam in the away team
for (unsigned int i = 0; i < 9; i++) {
objectID objid(i, 0, 0);
Object *obj = data.getObject(objid);
if (Common::find(_away_team_members.begin(), _away_team_members.end(), obj)
== _away_team_members.end()) {
// XXX: stupid hack to make sure the rest of the away team is off-screen
obj->curr_screen = 0;
} else {
obj->curr_screen = screen;
obj->loadSprite();
obj->sprite->startAnim(26);
data._currentScreen.objects.push_back(obj);
}
}
openLocation(world, screen);
if (entrance == 0xff) {
// TODO: don't display sprites?
entrance = 0;
}
if (entrance >= data._currentScreen.entrypoints.size()) {
error("entrance %d beyond the %d entrances to screen %x/%x", entrance,
data._currentScreen.entrypoints.size(), world, screen);
}
for (unsigned int i = 0; i < 4; i++) {
data._currentScreen.objects[i]->x = data._currentScreen.entrypoints[entrance][i].x;
data._currentScreen.objects[i]->y = data._currentScreen.entrypoints[entrance][i].y;
}
_on_away_team = true;
handleAwayTeamMouseMove(Common::Point());
}
void UnityEngine::startupScreen() {
// play two animations (both logo anim followed by text) from one file
SpritePlayer *p = new SpritePlayer("legaleze.spr", 0, this);
unsigned int anim = 0;
p->startAnim(anim);
uint32 waiting = 0;
while (!shouldQuit()) {
Common::Event event;
bool escape = false;
while (_eventMan->pollEvent(event)) {
if (event.type == Common::EVENT_KEYUP) {
if (event.kbd.keycode == Common::KEYCODE_ESCAPE) {
// note that the original game didn't allow escape
// until the first animation was done
escape = true;
}
}
}
if (escape) break;
p->update();
if (!p->playing()) {
if (!waiting) {
// arbitary 4 second wait, original waits until background
// load is done (i.e. 0s on modern systems)
waiting = g_system->getMillis() + 2000;
} else if (g_system->getMillis() < waiting) {
// delay for 1/10th sec while waiting
g_system->delayMillis(100);
} else if (anim == 0) {
waiting = 0;
// play second animation
anim = 1;
p->startAnim(anim);
} else {
// all done!
break;
}
}
_gfx->drawSprite(p, 0, 0, 256);
_system->updateScreen();
}
_mixer->stopAll();
delete p;
}
void UnityEngine::processTriggers() {
for (unsigned int i = 0; i < data._triggers.size(); i++) {
if (data._triggers[i]->tick(this)) {
Object *target = data.getObject(data._triggers[i]->target);
debug(1, "running trigger %x (target %s)", data._triggers[i]->id, target->identify().c_str());
// TODO: should trigger be who?
performAction(ACTION_USE, target);
break;
}
}
}
void UnityEngine::processTimers() {
// TODO: stupid hack to only run this so often
// TODO: is it correct to do this 0x123 thing here?
static uint32 last_time = 0;
if (last_time && ((last_time + 0x123) > g_system->getMillis()))
return;
last_time = g_system->getMillis();
Common::Array<Object *> &objects = data._currentScreen.objects;
for (unsigned int i = 0; i < objects.size(); i++) {
if (!(objects[i]->flags & OBJFLAG_ACTIVE)) continue;
if (objects[i]->timer == 0xffff) continue;
if (objects[i]->timer == 0) continue;
objects[i]->timer--;
if (objects[i]->timer == 0) {
debug(1, "running timer on %s", objects[i]->identify().c_str());
// TODO: should who be the timer?
performAction(ACTION_TIMER, objects[i]);
}
}
}
void UnityEngine::setSpeaker(objectID s) {
_speaker = s;
if (_icon) {
delete _icon;
_icon = NULL;
}
Common::String icon_sprite = data.getIconSprite(_speaker);
if (!icon_sprite.size()) {
warning("couldn't find icon sprite for %02x%02x%02x", _speaker.world,
_speaker.screen, _speaker.id);
return;
}
_icon = new SpritePlayer(icon_sprite.c_str(), NULL, this);
if (_icon->getNumAnims() < 3) {
_icon->startAnim(0); // static
} else {
_icon->startAnim(2); // speaking
}
}
void UnityEngine::handleLook(Object *obj) {
// TODO: correct?
if (performAction(ACTION_LOOK, obj, _current_away_team_member->id) & RESULT_DIDSOMETHING) return;
playDescriptionFor(obj);
}
void UnityEngine::playDescriptionFor(Object *obj) {
// TODO: this is very wrong, of course :)
unsigned int i = 0;
while (i < obj->descriptions.size() &&
obj->descriptions[i].entry_id != _current_away_team_member->id.id) i++;
if (i == obj->descriptions.size()) {
// TODO: should we just run this directly?
// TODO: hard-coded :( @95,34,xy...
_next_conversation = data.getConversation(0x5f, 34);
_next_situation = 0x1; // default to failed look
return;
}
Description &desc = obj->descriptions[i];
_dialog_text = desc.text;
setSpeaker(_current_away_team_member->id);
// 'l' for look :)
Common::String file = voiceFileFor(desc.voice_group, desc.voice_subgroup, objectID(desc.entry_id, 0, 0), desc.voice_id, 'l');
_snd->playSpeech(file);
runDialog();
}
Common::String UnityEngine::voiceFileFor(byte voice_group, byte voice_subgroup, objectID speaker, byte voice_id, char type) {
if (speaker.world != 0 || speaker.screen != 0 || speaker.id > 9) {
speaker.id = 0xff;
}
if (voice_group == 0xfe) {
return Common::String::format("%02x%02x%02x%02x.vac",
voice_group, speaker.id,
voice_subgroup, voice_id);
} else if (type) {
return Common::String::format("%02x%c%1x%02x%02x.vac",
voice_group, type, voice_subgroup,
speaker.id, voice_id);
} else {
return Common::String::format("%02x%02x%02x%02x.vac",
voice_group, voice_subgroup,
speaker.id, voice_id);
}
}
void UnityEngine::changeToScreen(ScreenType screenType) {
if (screenType == _currScreenType)
return;
_currScreenType = screenType;
if (_currScreen)
_currScreen->shutdown();
_currScreen = NULL;
switch (screenType) {
case BridgeScreenType:
_currScreen = _bridgeScreen;
break;
case ComputerScreenType:
_currScreen = _computerScreen;
break;
case ViewscreenScreenType:
_currScreen = _viewscreenScreen;
break;
default:
error("changeToScreen for unimplemented screen type");
}
_snd->stopMusic();
_currScreen->start();
}
void UnityEngine::handleUse(Object *obj) {
// TODO: correct?
if (performAction(ACTION_USE, obj, _current_away_team_member->id) & RESULT_DIDSOMETHING) return;
// TODO: should we just run this directly?
// TODO: hard-coded :( @95,34,xy...
_next_conversation = data.getConversation(0x5f, 34);
_next_situation = 0x2; // failed use?
}
void UnityEngine::handleTalk(Object *obj) {
// TODO: correct?
if (performAction(ACTION_TALK, obj, _current_away_team_member->id) & RESULT_DIDSOMETHING) return;
// TODO: should we just run this directly?
// TODO: hard-coded :( @95,34,xy...
_next_conversation = data.getConversation(0x5f, 34);
_next_situation = 0x3; // failed talk?
}
void UnityEngine::handleWalk(Object *obj) {
performAction(ACTION_WALK, obj, _current_away_team_member->id);
}
// TODO
unsigned int anim = 26;
void UnityEngine::DebugNextScreen() {
unsigned int &curr_loc = data._currentScreen.world;
unsigned int &curr_screen = data._currentScreen.screen;
// start in first screen
if (curr_loc == 0x5f) { curr_loc = 2; curr_screen = 0; }
curr_screen++;
switch (curr_loc) {
case 2: if (curr_screen == 2) curr_screen++;
else if (curr_screen == 14) curr_screen++;
else if (curr_screen == 21) { curr_loc++; curr_screen = 1; }
break;
case 3: if (curr_screen == 11) { curr_loc++; curr_screen = 1; }
break;
case 4: if (curr_screen == 13) { curr_loc++; curr_screen = 1; }
break;
case 5: if (curr_screen == 11) curr_screen++;
else if (curr_screen == 14) { curr_loc++; curr_screen = 1; }
break;
case 6: if (curr_screen == 16) { curr_loc++; curr_screen = 1; }
break;
case 7: if (curr_screen == 5) { curr_loc = 2; curr_screen = 1; }
break;
default:error("huh?");
}
debugN("moving to %d/%d\n", curr_loc, curr_screen);
startAwayTeam(curr_loc, curr_screen);
}
void UnityEngine::checkEvents() {
Common::Array<Object *> &objects = data._currentScreen.objects;
Common::Event event;
while (_eventMan->pollEvent(event)) {
switch (event.type) {
case Common::EVENT_QUIT:
return;
case Common::EVENT_KEYUP:
switch (event.kbd.keycode) {
case Common::KEYCODE_n:
if (_on_away_team) {
debugN("trying anim %d\n", anim);
anim++;
anim %= objects[0]->sprite->getNumAnims();
for (unsigned int i = 0; i < 4; i++)
objects[i]->sprite->startAnim(anim);
}
break;
case Common::KEYCODE_SPACE:
DebugNextScreen();
break;
default:
break;
}
break;
case Common::EVENT_KEYDOWN:
switch (event.kbd.keycode) {
case Common::KEYCODE_d:
if (event.kbd.hasFlags(Common::KBD_CTRL)) {
this->getDebugger()->attach();
this->getDebugger()->onFrame();
}
break;
default:
break;
}
break;
case Common::EVENT_RBUTTONUP:
if (!_on_away_team)
break;
// cycle through away team modes
switch (_mode) {
case mode_Look:
_mode = mode_Use;
break;
case mode_Use:
_mode = mode_Walk;
break;
case mode_Walk:
_mode = mode_Talk;
break;
case mode_Talk:
_mode = mode_Look;
break;
}
handleAwayTeamMouseMove(Common::Point()); // TODO
break;
case Common::EVENT_MOUSEMOVE:
if (_in_dialog) {
dialogMouseMove(event.mouse);
break;
}
if (_on_away_team) {
handleAwayTeamMouseMove(event.mouse);
} else {
_currScreen->mouseMove(event.mouse);
}
break;
case Common::EVENT_LBUTTONUP:
if (_in_dialog) {
dialogMouseClick(event.mouse);
break;
}
if (_on_away_team) {
handleAwayTeamMouseClick(event.mouse);
} else {
_currScreen->mouseClick(event.mouse);
}
break;
default:
break;
}
}
_snd->updateMusic(); // TODO
}
void UnityEngine::handleAwayTeamMouseMove(const Common::Point &pos) {
Object *obj = objectAt(pos.x, pos.y);
if (obj) {
switch (_mode) {
case mode_Look:
_status_text = "Look at " + obj->name;
_gfx->setCursor(1, false);
break;
case mode_Use:
_status_text = "Use " + obj->name;
if (obj->talk_string.size() && obj->talk_string[obj->talk_string.size() - 1] == '-') {
// handle custom USE strings
// TODO: this doesn't really belong here (and isn't right anyway?)
_status_text.clear();
unsigned int i = obj->talk_string.size() - 1;
while (obj->talk_string[i] == '-' && i > 0) {
i--;
}
while (obj->talk_string[i] != '-' && obj->talk_string[i] != ' ' && i > 0) {
_status_text = obj->talk_string[i] + _status_text; i--;
}
_status_text = _status_text + " " + obj->name;
}
_gfx->setCursor(3, false);
break;
case mode_Walk:
_status_text = "Walk to " + obj->name;
if (obj->cursor_flag == 1) // TODO
_gfx->setCursor(8 + obj->cursor_id, false);
else
_gfx->setCursor(7, false);
break;
case mode_Talk:
_status_text = "Talk to " + obj->name;
_gfx->setCursor(5, false);
break;
}
} else {
_status_text.clear();
switch (_mode) {
case mode_Look:
_gfx->setCursor(0, false);
break;
case mode_Use:
_gfx->setCursor(2, false);
break;
case mode_Walk:
_gfx->setCursor(6, false);
break;
case mode_Talk:
_gfx->setCursor(4, false);
break;
}
}
}
void UnityEngine::handleAwayTeamMouseClick(const Common::Point &pos) {
Object *obj = objectAt(pos.x, pos.y);
if (!obj)
return;
switch (_mode) {
case mode_Look:
handleLook(obj);
break;
case mode_Use:
handleUse(obj);
break;
case mode_Walk:
handleWalk(obj);
break;
case mode_Talk:
handleTalk(obj);
break;
}
}
void UnityEngine::drawObjects() {
Common::Array<Object *> &objects = data._currentScreen.objects;
Common::Array<Object *> to_draw;
for (unsigned int i = 0; i < objects.size(); i++) {
if (objects[i]->sprite) {
if (!(objects[i]->flags & OBJFLAG_ACTIVE)) continue;
if (objects[i]->flags & OBJFLAG_INVENTORY) continue;
objects[i]->sprite->update();
// TODO
if (!objects[i]->sprite->valid()) { debug(2, "invalid sprite?"); continue; }
to_draw.push_back(objects[i]);
}
}
Common::sort(to_draw.begin(), to_draw.end(), DrawOrderComparison());
for (unsigned int i = 0; i < to_draw.size(); i++) {
// XXX: this is obviously a temporary hack
unsigned int scale = 256;
if (to_draw[i]->objwalktype == OBJWALKTYPE_SCALED) {
unsigned int j;
unsigned int x = to_draw[i]->x, y = to_draw[i]->y;
for (j = 0; j < data._currentScreen.polygons.size(); j++) {
ScreenPolygon &poly = data._currentScreen.polygons[j];
if (poly.type != 1) continue;
unsigned int triangle;
if (poly.insideTriangle(x, y, triangle)) {
// TODO: interpolation
scale = poly.triangles[triangle].distances[0];
break;
}
}
if (j == data._currentScreen.polygons.size())
debug(2, "couldn't find poly for walkable at (%d, %d)", x, y);
}
_gfx->drawSprite(to_draw[i]->sprite, to_draw[i]->x, to_draw[i]->y, scale);
}
}
void UnityEngine::drawDialogFrameAround(unsigned int x, unsigned int y, unsigned int width,
unsigned int height, bool use_thick_frame, bool with_icon, bool with_buttons) {
// dialog.mrg
MRGFile mrg;
Common::Array<uint16> &widths = mrg.widths;
Common::Array<uint16> &heights = mrg.heights;
_gfx->loadMRG("dialog.mrg", &mrg);
assert(widths.size() == 31);
unsigned int base = (use_thick_frame ? 17 : 0);
unsigned int real_x1 = x - 10;
unsigned int real_x2 = x + width + 10;
unsigned int real_y1 = y - 20;
unsigned int real_y2 = y + height + 20;
real_y1 -= widths[base+4];
real_y2 += widths[base+5];
real_x1 -= widths[base+6];
real_x2 += widths[base+7];
// icon frame goes on LEFT side, up/down buttons on RIGHT side, both in centre
if (with_icon) {
// text border is pretty small if there's no icon frame, otherwise includes that space..
real_x1 -= widths[8] / 2;
real_x1 += 8;
} else if (with_buttons) {
// spacing for up/down buttons
real_x2 += widths[11] / 2;
real_x2 -= 8;
}
// black background inside the border
_gfx->fillRect(0, real_x1 + widths[base+4], real_y1 + heights[base+6], real_x2 - widths[base+5], real_y2 - heights[base+7]);
// top
for (unsigned int border_x = real_x1 + widths[base+0]; border_x + widths[base+4] < real_x2 - widths[base+1]; border_x += widths[base+4])
_gfx->drawMRG(&mrg, base+4, border_x, real_y1);
_gfx->drawMRG(&mrg, base+4, real_x2 - widths[base+1] - widths[base+4], real_y1);
// bottom
for (unsigned int border_x = real_x1 + widths[base+2]; border_x + widths[base+5] < real_x2 - widths[base+3]; border_x += widths[base+5])
_gfx->drawMRG(&mrg, base+5, border_x, real_y2 - heights[base+5]);
_gfx->drawMRG(&mrg, base+5, real_x2 - widths[base+3] - widths[base+5], real_y2 - heights[base+5]);
// left
for (unsigned int border_y = real_y1 + heights[base+0]; border_y + heights[base+6] < real_y2 - heights[base+2]; border_y += heights[base+6])
_gfx->drawMRG(&mrg, base+6, real_x1, border_y);
_gfx->drawMRG(&mrg, base+6, real_x1, real_y2 - heights[base+2] - heights[base+6]);
// right
for (unsigned int border_y = real_y1 + heights[base+1]; border_y + heights[base+7] < real_y2 - heights[base+3]; border_y += heights[base+7])
_gfx->drawMRG(&mrg, base+7, real_x2 - widths[base+7], border_y);
_gfx->drawMRG(&mrg, base+7, real_x2 - widths[base+7], real_y2 - heights[base+3] - heights[base+7]);
// corners
_gfx->drawMRG(&mrg, base+0, real_x1, real_y1);
_gfx->drawMRG(&mrg, base+1, real_x2 - widths[base+1], real_y1);
_gfx->drawMRG(&mrg, base+2, real_x1, real_y2 - heights[base+2]);
_gfx->drawMRG(&mrg, base+3, real_x2 - widths[base+3], real_y2 - heights[base+3]);
if (with_icon && _icon) {
_gfx->drawMRG(&mrg, 8, real_x1 - (widths[8] / 2) + (widths[base+6]/2), (real_y1+real_y2)/2 - (heights[8]/2));
_icon->update();
_gfx->drawSprite(_icon, real_x1 + (widths[base+6]/2) + 1, (real_y1+real_y2)/2 + (heights[8]/2) - 4);
} else if (with_buttons) {
// TODO: 11/12/13 up buttons (normal/highlight/grayed), 14/15/16 down buttons
uint up_button = 13;
_gfx->drawMRG(&mrg, up_button, real_x2 - (widths[up_button] / 2) - (widths[base+7]/2), (real_y1+real_y2)/2 - heights[up_button]);
uint down_button = 16;
_gfx->drawMRG(&mrg, down_button, real_x2 - (widths[down_button] / 2) - (widths[base+7]/2), (real_y1+real_y2)/2);
}
}
void UnityEngine::initDialog() {
const uint maxWidth = 313; // TODO: probably should be calculated
_dialogSelected = (uint)-1;
_dialogStartLine = 0;
_dialogLines.clear();
_dialogWidth = 0;
::Graphics::Font *font = _gfx->getFont(2);
if (!_choice_list.size()) {
_dialogLines.resize(1);
_dialogWidth = font->wordWrapText(_dialog_text, maxWidth, _dialogLines[0]);
} else {
_dialogLines.resize(_choice_list.size());
for (uint i = 0; i < _choice_list.size(); i++) {
Common::String our_str = _choice_list[i];
Common::Array<Common::String> &lines = _dialogLines[i];
uint newWidth = font->wordWrapText(our_str, maxWidth, lines);
if (newWidth > _dialogWidth)
_dialogWidth = newWidth;