forked from exult/exult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamewin.cc
3229 lines (2955 loc) · 87.7 KB
/
gamewin.cc
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
/**
** Gamewin.cc - X-windows Ultima7 map browser.
**
** Written: 7/22/98 - JSF
**/
/*
*
* Copyright (C) 1998-1999 Jeffrey S. Freedman
* Copyright (C) 2000-2022 The Exult Team
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "gamewin.h"
#include "Astar.h"
#include "Audio.h"
#include "AudioMixer.h"
#include "Configuration.h"
#include "Face_stats.h"
#include "Flex.h"
#include "Gump.h"
#include "Gump_manager.h"
#include "ItemMenu_gump.h"
#include "Notebook_gump.h"
#include "ShortcutBar_gump.h"
#include "actions.h"
#include "animate.h"
#include "audio/midi_drivers/XMidiFile.h"
#include "barge.h"
#include "cheat.h"
#include "chunks.h"
#include "chunkter.h"
#include "combat.h"
#include "combat_opts.h"
#include "dir.h"
#include "drag.h"
#include "effects.h"
#include "egg.h"
#include "exult.h"
#include "files/U7file.h"
#include "flic/playfli.h"
#include "fnames.h"
#include "game.h"
#include "gameclk.h"
#include "gamemap.h"
#include "gamerend.h"
#include "items.h"
#include "keyactions.h"
#include "keys.h"
#include "mappatch.h"
#include "monsters.h"
#include "monstinf.h"
#include "mouse.h"
#include "npcnear.h"
#include "objiter.h"
#include "party.h"
#include "paths.h"
#include "schedule.h"
#include "spellbook.h"
#include "ucmachine.h"
#include "ucsched.h" /* Only used to flush objects. */
#include "usefuns.h"
#include "utils.h"
#include "version.h"
#include "virstone.h"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#ifdef USE_EXULTSTUDIO
# include "servemsg.h"
# include "server.h"
#endif
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
using std::ios;
using std::istream;
using std::make_unique;
using std::ofstream;
using std::rand;
using std::srand;
using std::string;
using std::unique_ptr;
using std::vector;
// THE game window:
Game_window* Game_window::game_window = nullptr;
/*
* Provide chirping birds.
*/
class Background_noise : public Time_sensitive {
int repeats; // Repeats in quick succession.
int last_sound; // # of last sound played.
Game_window* gwin;
enum Song_states {
Invalid,
Outside,
Dungeon,
Nighttime,
RainStorm,
SnowStorm,
DangerNear
} laststate; // Last state for SFX music tracks,
// 1 outside, 2 dungeon, 3 nighttime, 4 rainstorm,
// 5 snowstorm, 6 for danger nearby
public:
Background_noise(Game_window* gw)
: repeats(0), last_sound(-1), gwin(gw), laststate(Invalid) {
gwin->get_tqueue()->add(5000, this);
}
~Background_noise() override {
gwin->get_tqueue()->remove(this);
}
void handle_event(unsigned long curtime, uintptr udata) override;
static bool is_combat_music(int num) {
// Lumping music 16 as if it were a combat music in order to simplify
// the check.
return (num >= Audio::game_music(9) && num <= Audio::game_music(12))
|| (num >= Audio::game_music(15)
&& num <= Audio::game_music(18));
}
};
/*
* Play background sound.
*/
void Background_noise::handle_event(unsigned long curtime, uintptr udata) {
#ifndef COLOURLESS_REALLY_HATES_THE_BG_SFX
unsigned long delay = 8000;
Song_states currentstate;
const int bghour = gwin->get_clock()->get_hour();
const int weather = gwin->get_effects()->get_weather();
const bool nighttime = bghour < 6 || bghour > 20;
const bool nearby_hostile = gwin->is_hostile_nearby();
if (nearby_hostile && !gwin->in_combat()) {
currentstate = DangerNear;
} else if (gwin->is_in_dungeon()) {
currentstate = Dungeon;
} else if (weather == 2) {
currentstate = RainStorm;
} else if (weather == 1) {
currentstate = SnowStorm;
} else if (nighttime) {
currentstate = Nighttime;
} else {
currentstate = Outside;
}
MyMidiPlayer* player = Audio::get_ptr()->get_midi();
// The sfx tracks only play for Digital Music, MT32emu, MT32/FakeMT32
// FMOpl is not sounding acceptable even though the original used it.
const bool play_bg_tracks
= player && (player->get_ogg_enabled() || player->is_mt32());
if (player && play_bg_tracks) {
delay = 1000; // Quickly get back to this function check
const int curr_track = player->get_current_track();
if ((curr_track == -1 || laststate != currentstate)
&& Audio::get_ptr()->is_music_enabled()) {
// Conditions: not playing music, playing a background music
if (curr_track == -1 || gwin->is_background_track(curr_track)
|| (((currentstate == Dungeon) || currentstate == DangerNear)
&& !is_combat_music(curr_track))) {
// Not already playing music
int tracknum = 255;
// Get the relevant track number.
if (nearby_hostile && !gwin->in_combat()) {
tracknum = Audio::game_music(10);
laststate = DangerNear;
} else if (gwin->is_in_dungeon()) {
if (play_bg_tracks) {
tracknum = Audio::game_music(52);
}
laststate = Dungeon;
} else if (weather == 1) { // Snowstorm
if (play_bg_tracks) {
tracknum = Audio::game_music(5);
}
laststate = SnowStorm;
} else if (weather == 2) { // Rainstorm
if (play_bg_tracks) {
tracknum = Audio::game_music(4);
}
laststate = RainStorm;
} else if (nighttime) {
if (play_bg_tracks) {
tracknum = Audio::game_music(7);
}
laststate = Nighttime;
} else {
if (play_bg_tracks) {
tracknum = Audio::game_music(6);
}
laststate = Outside;
}
Audio::get_ptr()->start_music(tracknum, true);
}
}
}
// Tests to see if Outside/Nighttime sfx tracks are playing,
// possible when the game has been restored
// and the Audio option was changed from OGG/MT32 to something else
if (player && !play_bg_tracks
&& (player->get_current_track() == 6
|| player->get_current_track() == 7)) {
player->stop_music();
}
Main_actor* ava = gwin->get_main_actor();
// Testing. When outside play birds during daytime or crickets at night
if (ava && !gwin->is_main_actor_inside() && currentstate != Dungeon) {
int sound = -1; // SFX #.
static const unsigned char bgnight[] = {61, 61, 255};
static const unsigned char bgday[] = {82, 85, 85};
if (repeats > 0) { // Repeating?
sound = last_sound;
} else {
if (nighttime) {
sound = bgnight[rand() % sizeof(bgnight)];
// only play daytime sfx when no music track is playing
} else if (
!play_bg_tracks
&& (!player || player->get_current_track() == -1)) {
sound = bgday[rand() % sizeof(bgday)];
}
last_sound = sound;
}
if (sound >= 0) {
Audio::get_ptr()->play_sound_effect(
Audio::game_sfx(sound), AUDIO_MAX_VOLUME - 64);
repeats++; // Count it.
if (rand() % (repeats + 1) == 0) {
// Repeat.
delay = 500 + rand() % 1000;
} else {
delay = 4000 + rand() % 3000;
repeats = 0;
}
} else {
repeats = 0;
}
}
gwin->get_tqueue()->add(curtime + delay, this, udata);
#endif
}
/*
* Create game window.
*/
Game_window::Game_window(
int width, int height, bool fullscreen, int gwidth, int gheight,
int scale, int scaler, Image_window::FillMode fillmode,
unsigned int fillsclr // Window dimensions.
)
: dragging(nullptr), effects(new Effects_manager(this)),
map(new Game_map(0)), render(new Game_render),
gump_man(new Gump_manager), party_man(new Party_manager),
win(nullptr), npc_prox(new Npc_proximity_handler(this)), pal(nullptr),
tqueue(new Time_queue()),
background_noise(new Background_noise(this)), usecode(nullptr),
combat(false), focus(true), ice_dungeon(false), painted(false),
ambient_light(false), infravision_active(false), skip_above_actor(31),
in_dungeon(0), num_npcs1(0), std_delay(c_std_delay), time_stopped(0),
special_light(0), theft_warnings(0), theft_cx(255), theft_cy(255),
moving_barge(nullptr), main_actor(nullptr), camera_actor(nullptr),
npcs(0), bodies(0), scrolltx(0), scrollty(0), dirty(0, 0, 0, 0),
save_names{}, mouse3rd(false), fastmouse(false),
double_click_closes_gumps(false), text_bg(false), step_tile_delta(8),
allow_right_pathfind(2), scroll_with_mouse(false),
alternate_drop(false), allow_autonotes(false), in_exult_menu(false),
extended_intro(false), load_palette_timer(0), plasma_start_color(0),
plasma_cycle_range(0), skip_lift(255), paint_eggs(false),
armageddon(false), walk_in_formation(false), debug(0), blits(0),
scrolltx_l(0), scrollty_l(0), scrolltx_lp(0), scrollty_lp(0),
scrolltx_lo(0), scrollty_lo(0), avposx_ld(0), avposy_ld(0),
lerping_enabled(0) {
game_window = this; // Set static ->.
clock = new Game_clock(tqueue);
shape_man = new Shape_manager(); // Create the single instance.
maps.push_back(map); // Map #0.
// Create window.
win = new Image_window8(
width, height, gwidth, gheight, scale, fullscreen, scaler, fillmode,
fillsclr);
win->set_title("Exult Ultima VII Engine");
pal = new Palette();
Game_singletons::init(this); // Everything but 'usecode' exists.
Shape_frame::set_to_render(win->get_ib8());
string str;
config->value("config/gameplay/textbackground", text_bg, -1);
config->value("config/gameplay/mouse3rd", str, "no");
if (str == "yes") {
mouse3rd = true;
}
config->set("config/gameplay/mouse3rd", str, false);
config->value("config/gameplay/fastmouse", str, "no");
if (str == "yes") {
fastmouse = true;
}
config->set("config/gameplay/fastmouse", str, false);
config->value("config/gameplay/double_click_closes_gumps", str, "no");
if (str == "yes") {
double_click_closes_gumps = true;
}
config->set("config/gameplay/double_click_closes_gumps", str, false);
config->value("config/gameplay/combat/difficulty", Combat::difficulty, 0);
config->set("config/gameplay/combat/difficulty", Combat::difficulty, false);
config->value("config/gameplay/combat/charmDifficulty", str, "normal");
Combat::charmed_more_difficult = (str == "hard");
config->set("config/gameplay/combat/charmDifficulty", str, false);
config->value("config/gameplay/combat/mode", str, "original");
if (str == "keypause") {
Combat::mode = Combat::keypause;
} else {
Combat::mode = Combat::original;
}
config->set("config/gameplay/combat/mode", str, false);
config->value("config/gameplay/combat/show_hits", str, "no");
Combat::show_hits = (str == "yes");
config->set("config/gameplay/combat/show_hits", str, false);
config->value("config/audio/disablepause", str, "no");
config->set("config/audio/disablepause", str, false);
config->value("config/gameplay/step_tile_delta", step_tile_delta, 8);
if (step_tile_delta < 1) {
step_tile_delta = 1;
}
config->set("config/gameplay/step_tile_delta", step_tile_delta, false);
config->value("config/gameplay/allow_right_pathfind", str, "double");
if (str == "no") {
allow_right_pathfind = 0;
} else if (str == "single") {
allow_right_pathfind = 1;
}
config->set("config/gameplay/allow_right_pathfind", str, false);
// New 'formation' walking?
config->value("config/gameplay/formation", str, "yes");
// Assume "yes" on anything but "no".
walk_in_formation = str != "no";
config->set(
"config/gameplay/formation", walk_in_formation ? "yes" : "no",
false);
config->value("config/gameplay/smooth_scrolling", lerping_enabled, 0);
config->set("config/gameplay/smooth_scrolling", lerping_enabled, false);
config->value("config/gameplay/alternate_drop", str, "no");
alternate_drop = str == "yes";
config->set(
"config/gameplay/alternate_drop", alternate_drop ? "yes" : "no",
false);
config->value("config/gameplay/allow_autonotes", str, "no");
allow_autonotes = str == "yes";
config->set(
"config/gameplay/allow_autonotes", allow_autonotes ? "yes" : "no",
false);
#if defined(__IPHONEOS__) || defined(ANDROID)
const string default_scroll_with_mouse = "no";
const string default_item_menu = "yes";
const string default_dpad_location = "right";
const string default_touch_pathfind = "yes";
const string default_shortcutbar = "translucent";
const string default_outline = "black";
#else
const string default_scroll_with_mouse = "yes";
const string default_item_menu = "no";
const string default_dpad_location = "no";
const string default_touch_pathfind = "no";
const string default_shortcutbar = "no";
const string default_outline = "no";
#endif
// scroll with mouse
config->value(
"config/gameplay/scroll_with_mouse", str,
default_scroll_with_mouse);
scroll_with_mouse = str == "yes";
config->set(
"config/gameplay/scroll_with_mouse",
scroll_with_mouse ? "yes" : "no", false);
// Item menu
config->value("config/touch/item_menu", str, default_item_menu);
item_menu = str == "yes";
config->set("config/touch/item_menu", item_menu ? "yes" : "no", false);
// DPad location
config->value("config/touch/dpad_location", str, default_dpad_location);
if (str == "no") {
dpad_location = 0;
} else if (str == "left") {
dpad_location = 1;
} else {
str = "right";
dpad_location = 2;
}
config->set("config/touch/dpad_location", str, false);
// Touch pathfind
config->value("config/touch/touch_pathfind", str, default_touch_pathfind);
touch_pathfind = str == "yes";
config->set(
"config/touch/touch_pathfind", touch_pathfind ? "yes" : "no",
false);
// Shortcut bar
config->value(
"config/shortcutbar/use_shortcutbar", str, default_shortcutbar);
if (str == "no") {
use_shortcutbar = 0;
} else if (str == "yes") {
use_shortcutbar = 2;
} else {
str = "translucent";
use_shortcutbar = 1;
}
config->set("config/shortcutbar/use_shortcutbar", str, false);
config->value("config/shortcutbar/use_outline_color", str, default_outline);
if (str == "no") {
outline_color = NPIXCOLORS;
} else if (str == "green") {
outline_color = POISON_PIXEL;
} else if (str == "white") {
outline_color = PROTECT_PIXEL;
} else if (str == "yellow") {
outline_color = CURSED_PIXEL;
} else if (str == "blue") {
outline_color = CHARMED_PIXEL;
} else if (str == "red") {
outline_color = HIT_PIXEL;
} else if (str == "purple") {
outline_color = PARALYZE_PIXEL;
} else {
str = "black";
outline_color = BLACK_PIXEL;
}
config->set("config/shortcutbar/use_outline_color", str, false);
config->value("config/shortcutbar/hide_missing_items", str, "yes");
sb_hide_missing = str != "no";
config->set(
"config/shortcutbar/hide_missing_items",
sb_hide_missing ? "yes" : "no", false);
config->write_back();
// default to SI extended intro
config->value("config/gameplay/extended_intro", str, "yes");
extended_intro = str == "yes";
config->set(
"config/gameplay/extended_intro", extended_intro ? "yes" : "no",
false);
}
/*
* Blank out screen.
*/
void Game_window::clear_screen(bool update) {
win->fill8(
0, win->get_full_width(), win->get_full_height(),
win->get_start_x(), win->get_start_y());
// update screen
if (update) {
show(true);
}
}
/*
* Deleting game window.
*/
Game_window::~Game_window() {
gump_man->close_all_gumps(true);
clear_world(false); // Delete all objects, chunks.
delete shape_man;
delete gump_man;
delete party_man;
delete background_noise;
delete tqueue;
tqueue = nullptr;
delete win;
delete dragging;
delete pal;
for (auto* map : maps) {
delete map;
}
delete usecode;
delete clock;
delete npc_prox;
delete effects;
delete render;
}
/*
* Abort.
*/
void Game_window::abort(const char* msg, ...) {
std::va_list ap;
va_start(ap, msg);
char buf[512];
vsnprintf(buf, sizeof(buf), msg, ap); // Format the message.
cerr << "Exult (fatal): " << buf << endl;
delete this;
throw quit_exception(-1);
}
bool Game_window::is_background_track(
int num) const { // ripped out of Background_noise
// Have to do it this way because of SI.
return num == Audio::game_music(4) || num == Audio::game_music(5)
|| num == Audio::game_music(6) || num == Audio::game_music(7)
|| num == Audio::game_music(8) || num == Audio::game_music(52);
}
void Game_window::init_files(bool cycle) {
// Display red plasma during load...
if (cycle) {
setup_load_palette();
}
usecode = Usecode_machine::create();
Game_singletons::init(this); // Everything should exist here.
cycle_load_palette();
shape_man->load(); // All the .vga files!
cycle_load_palette();
const unsigned long timer = SDL_GetTicks();
srand(timer); // Use time to seed rand. generator.
// Force clock to start.
tqueue->add(timer, clock, this);
// Go to starting chunk
scrolltx_lp = scrolltx_l = scrolltx = game->get_start_tile_x();
scrollty_lp = scrollty_l = scrollty = game->get_start_tile_y();
scrolltx_lo = scrollty_lo = 0;
avposx_ld = avposy_ld = 0;
// initialize keybinder
delete keybinder;
keybinder = new KeyBinder();
std::string d;
std::string keyfilename;
d = "config/disk/game/" + Game::get_gametitle() + "/keys";
config->value(d.c_str(), keyfilename, "(default)");
if (keyfilename == "(default)") {
config->set(d.c_str(), keyfilename, true);
keybinder->LoadDefaults();
} else {
try {
keybinder->LoadFromFile(keyfilename.c_str());
} catch (file_open_exception&) {
cerr << "Key mappings file '" << keyfilename
<< "' not found, falling back to default mappings." << endl;
keybinder->LoadDefaults();
}
}
keybinder->LoadFromPatch();
cycle_load_palette();
int fps; // Init. animation speed.
config->value("config/video/fps", fps, 5);
if (fps <= 0) {
fps = 5;
}
std_delay = 1000 / fps; // Convert to msecs. between frames.
}
/*
* Read any map. (This is for "multimap" games, not U7.)
*/
Game_map* Game_window::get_map(int num // Should be > 0.
) {
if (num >= static_cast<int>(maps.size())) {
maps.resize(num + 1);
}
if (maps[num] == nullptr) {
auto* newmap = new Game_map(num);
maps[num] = newmap;
maps[num]->init();
}
return maps[num];
}
/*
* Set current map to given #.
*/
void Game_window::set_map(int num) {
map = get_map(num);
if (!map) {
abort("Map %d doesn't exist", num);
}
Game_singletons::gmap = map;
}
/*
* Get map patch list.
*/
Map_patch_collection& Game_window::get_map_patches() {
return map->get_map_patches();
}
/*
* Set/unset barge mode.
*/
void Game_window::set_moving_barge(Barge_object* b) {
if (b && b != moving_barge) {
b->gather(); // Will 'gather' on next move.
if (!b->contains(main_actor)) {
b->set_to_gather();
}
} else if (!b && moving_barge) {
moving_barge->done(); // No longer 'barging'.
}
moving_barge = b;
}
/*
* Is character moving?
*/
bool Game_window::is_moving() const {
return moving_barge ? moving_barge->is_moving() : main_actor->is_moving();
}
/*
* Are we in dont_move mode?
*/
bool Game_window::main_actor_dont_move() const {
return !cheat.in_map_editor() && main_actor != nullptr
&& // Not if map-editing.
((main_actor->get_flag(Obj_flags::dont_move))
|| (main_actor->get_flag(Obj_flags::dont_render)));
}
/*
* Are we in dont_move mode?
*/
bool Game_window::main_actor_can_act() const {
return main_actor->can_act();
}
bool Game_window::main_actor_can_act_charmed() const {
return main_actor->can_act_charmed();
}
bool Game_window::in_infravision() const {
return infravision_active || cheat.in_infravision();
}
/*
* Add time for a light spell.
*/
void Game_window::add_special_light(int units // Light=500, GreatLight=5000.
) {
if (!special_light) { // Nothing in effect now?
special_light = clock->get_total_minutes();
clock->set_palette();
}
special_light += units / 20; // Figure ending time.
}
/*
* Set 'stop time' value.
*/
void Game_window::set_time_stopped(long delay // Delay in ticks (1/1000
// secs.), -1 to stop
// indefinitely, or 0 to end.
) {
if (delay == -1) {
time_stopped = -1;
} else if (!delay) {
time_stopped = 0;
} else {
const long new_expire = Game::get_ticks() + delay;
if (new_expire > time_stopped) { // Set expiration time.
time_stopped = new_expire;
}
}
}
/*
* Return delay to expiration (or 3000 if indefinite).
*/
long Game_window::check_time_stopped() {
if (time_stopped == -1) {
return 3000;
}
const long delay = time_stopped - Game::get_ticks();
if (delay > 0) {
return delay;
}
time_stopped = 0; // Done.
return 0;
}
/*
* Toggle combat mode.
*/
void Game_window::toggle_combat() {
combat = !combat;
// Change party member's schedules.
int newsched = combat ? Schedule::combat : Schedule::follow_avatar;
const int cnt = party_man->get_count();
for (int i = 0; i < cnt; i++) {
const int party_member = party_man->get_member(i);
Actor* person = get_npc(party_member);
if (!person) {
continue;
}
if (!person->can_act_charmed()) {
newsched = Schedule::combat;
}
const int sched = person->get_schedule_type();
if (sched != newsched && sched != Schedule::wait
&& sched != Schedule::loiter) {
person->set_schedule_type(newsched);
}
}
if (!main_actor_can_act_charmed()) {
newsched = Schedule::combat;
}
if (main_actor->get_schedule_type() != newsched) {
main_actor->set_schedule_type(newsched);
}
if (combat) { // Get rid of flee modes.
main_actor->ready_best_weapon();
set_moving_barge(nullptr); // And get out of barge mode.
Actor* all[9];
const int cnt = get_party(all, 1);
for (int i = 0; i < cnt; i++) {
// Did Usecode set to flee?
Actor* act = all[i];
if (act->get_attack_mode() == Actor::flee
&& !act->did_user_set_attack()) {
act->set_attack_mode(Actor::nearest);
}
// And avoid attacking party members,
// in case of Usecode bug.
const Game_object* targ = act->get_target();
if (targ && targ->get_flag(Obj_flags::in_party)) {
act->set_target(nullptr);
}
}
} else { // Ending combat.
Combat::resume(); // Make sure not still paused.
}
if (g_shortcutBar) {
g_shortcutBar->set_changed();
}
}
/*
* Add an NPC.
*/
void Game_window::add_npc(
Actor* npc,
int num // Number. Has to match npc->num.
) {
assert(num == npc->get_npc_num());
assert(num <= static_cast<int>(npcs.size()));
if (num == static_cast<int>(npcs.size())) { // Add at end.
npcs.push_back(
std::static_pointer_cast<Actor>(npc->shared_from_this()));
} else {
// Better be unused.
assert(!npcs[num] || npcs[num]->is_unused());
npcs[num] = std::static_pointer_cast<Actor>(npc->shared_from_this());
}
}
/* Get desired NPD.
*/
Actor* Game_window::get_npc(long npc_num) const {
if (npc_num < 0 || npc_num >= static_cast<int>(npcs.size())) {
return nullptr;
} else {
return npcs[npc_num].get();
}
}
/*
* Show desired NPC.
*/
void Game_window::locate_npc(int npc_num) {
char msg[80];
Actor* npc = get_npc(npc_num);
if (!npc) {
snprintf(msg, sizeof(msg), "NPC %d does not exist.", npc_num);
effects->center_text(msg);
} else if (npc->is_pos_invalid()) {
snprintf(msg, sizeof(msg), "NPC %d is not on the map.", npc_num);
effects->center_text(msg);
} else {
// ++++WHAT IF on a different map???
const Tile_coord pos = npc->get_tile();
center_view(pos);
cheat.clear_selected();
cheat.append_selected(npc);
snprintf(
msg, sizeof(msg), "NPC %d: '%s'.", npc_num,
npc->get_npc_name().c_str());
const int above = pos.tz + npc->get_info().get_3d_height() - 1;
if (skip_above_actor > above) {
set_above_main_actor(above);
}
effects->center_text(msg);
}
}
/*
* Find first unused NPC #.
*/
int Game_window::get_unused_npc() {
int cnt = npcs.size(); // Get # in list.
int i = 0;
for (; i < cnt; i++) {
if (i >= 356 && i <= 359) {
continue; // Never return these.
}
if (!npcs[i] || npcs[i]->is_unused()) {
return i;
}
}
if (i >= 356 && i <= 359) {
// Special, 'reserved' cases.
i = 360;
do {
npcs.push_back(std::make_shared<Npc_actor>("Reserved", 0));
auto& npc = npcs[cnt];
npc->set_schedule_type(Schedule::wait);
npc->set_unused(true);
} while (++cnt < 360);
}
return i; // First free one is past the end.
}
/*
* Resize event occurred.
*/
void Game_window::resized(
unsigned int neww, unsigned int newh, bool newfs, unsigned int newgw,
unsigned int newgh, unsigned int newsc, unsigned int newsclr,
Image_window::FillMode newfill, unsigned int newfillsclr) {
win->resized(
neww, newh, newfs, newgw, newgh, newsc, newsclr, newfill,
newfillsclr);
pal->apply(false);
Shape_frame::set_to_render(win->get_ib8());
if (!main_actor) { // In case we're before start.
return;
}
center_view(main_actor->get_tile());
paint();
// Do the following only if in game (not for menus)
if (!gump_man->gump_mode()) {
char msg[80];
snprintf(msg, sizeof(msg), "%ux%ux%u", neww, newh, newsc);
effects->center_text(msg);
}
if (g_shortcutBar) {
g_shortcutBar->set_changed();
}
Face_stats::UpdateButtons();
}
/*
* Clear out world's contents. Should be used during a 'restore'.
*/
void Game_window::clear_world(bool restoremapedit) {
const bool edit = cheat.in_map_editor();
cheat.set_map_editor(false);
Combat::resume();
tqueue->clear(); // Remove all entries.
clear_dirty();
Usecode_script::clear(); // Clear out all scheduled usecode.
// Most NPCs were deleted when the map is cleared; we have to deal with some
// stragglers.
for (auto& npc : npcs) {
if (npc && npc->is_unused()) {
npc.reset();
}
}
for (auto* map : maps) {
map->clear();
}
try {
set_map(0); // Back to main map.
} catch (const quit_exception& /*f*/) {
// Lets not allow this to go up.
}
Monster_actor::delete_all(); // To be safe, del. any still around.
Notebook_gump::clear();
main_actor = nullptr;
camera_actor = nullptr;
num_npcs1 = 0;
theft_cx = theft_cy = -1;
combat = false;
npcs.resize(0); // NPC's already deleted above.
bodies.resize(0);
moving_barge = nullptr; // Get out of barge mode.
special_light = 0; // Clear out light spells.
ambient_light = false; // And ambient lighting.
infravision_active = false;
effects->remove_all_effects(false);
Schedule_change::clear();
cheat.set_map_editor(edit && restoremapedit);
}
/*
* Look throughout the map for a given shape. The search starts at
* the first currently-selected shape, if possible.
*
* Output: true if found, else 0.
*/
bool Game_window::locate_shape(
int shapenum, // Desired shape.
bool upwards, // If true, search upwards.
int frnum, // Frame.
int qual // Quality/quantity.
) {
// Get (first) selected object.
const std::vector<Game_object_shared>& sel = cheat.get_selected();
Game_object* start = !sel.empty() ? (sel[0]).get() : nullptr;
char msg[80];
snprintf(msg, sizeof(msg), "Searching for shape %d", shapenum);
effects->center_text(msg);
paint();
show();
Game_object* obj = map->locate_shape(shapenum, upwards, start, frnum, qual);
if (!obj) {
effects->center_text("Not found");
return false; // Not found.
}
effects->remove_text_effects();
cheat.clear_selected(); // Select obj.
cheat.append_selected(obj);
//++++++++Got to show it.
Game_object* owner = obj->get_outermost(); //+++++TESTING
if (owner != obj) {
cheat.append_selected(owner);
}
center_view(owner->get_tile());
return true;
}