forked from exult/exult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exult.cc
3010 lines (2798 loc) · 89.4 KB
/
exult.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
/**
** Exult.cc - Multiplatform Ultima 7 game engine
**
** 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 "exult.h"
#include "Audio.h"
#include "AudioMixer.h"
#include "Configuration.h"
#include "Gump_button.h"
#include "Gump_manager.h"
#include "Scroll_gump.h"
#include "ShortcutBar_gump.h"
#include "U7file.h"
#include "U7fileman.h"
#include "VideoOptions_gump.h"
#include "actors.h"
#include "args.h"
#include "cheat.h"
#include "combat_opts.h"
#include "crc.h"
#include "drag.h"
#include "effects.h"
#include "exult_bg_flx.h"
#include "exult_flx.h"
#include "exult_si_flx.h"
#include "exultmenu.h"
#include "fnames.h"
#include "font.h"
#include "game.h"
#include "gamemap.h"
#include "gamemgr/modmgr.h"
#include "gamewin.h"
#include "gump_utils.h"
#include "ignore_unused_variable_warning.h"
#include "items.h"
#include "keyactions.h"
#include "keys.h"
#include "mouse.h"
#include "palette.h"
#include "party.h"
#include "sdlrwopsistream.h"
#include "sdlrwopsostream.h"
#include "touchui.h"
#include "u7drag.h"
#include "ucmachine.h"
#include "utils.h"
#include "verify.h"
#include "version.h"
#include <cctype>
#include <cmath>
#include <cstdlib>
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wold-style-cast"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif // __GNUC__
#include <SDL.h>
static const Uint32 EXSDL_TOUCH_MOUSEID = SDL_TOUCH_MOUSEID;
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif // __GNUC__
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wvariadic-macros"
#endif // __GNUC__
#define Font _XFont_
#include <SDL_syswm.h>
#undef Font
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif // __GNUC__
#ifdef USE_EXULTSTUDIO /* Only needed for communication with exult studio */
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# endif
# ifdef _WIN32
# include "windrag.h"
# endif
# include "chunks.h"
# include "chunkter.h"
# include "objserial.h"
# include "servemsg.h"
# include "server.h"
#endif
#if (defined(USECODE_DEBUGGER) && defined(XWIN))
# include <csignal>
#endif
using namespace Pentagram;
#ifdef __IPHONEOS__
# include "ios_utils.h"
#elif defined(ANDROID)
# include "TouchUI_Android.h"
# include <SDL_system.h>
#endif
using std::atexit;
using std::atof;
using std::cerr;
using std::cout;
using std::endl;
using std::exit;
using std::string;
using std::toupper;
using std::vector;
#if (defined(_WIN32) \
|| (defined(MACOSX) && defined(XWIN) && defined(USE_EXULTSTUDIO)))
static int SDLCALL SDL_putenv(const char* _var) {
char* ptr = nullptr;
char* var = SDL_strdup(_var);
if (var == nullptr) {
return -1; /* we don't set errno. */
}
ptr = SDL_strchr(var, '=');
if (ptr == nullptr) {
SDL_free(var);
return -1;
}
*ptr = '\0'; /* split the string into name and value. */
SDL_setenv(var, ptr + 1, 1);
SDL_free(var);
return 0;
}
#endif
Configuration* config = nullptr;
KeyBinder* keybinder = nullptr;
GameManager* gamemanager = nullptr;
/*
* Globals:
*/
Game_window* gwin = nullptr;
quitting_time_enum quitting_time = QUIT_TIME_NO;
bool intrinsic_trace = false; // Do we trace Usecode-intrinsics?
int usecode_trace = 0; // Do we trace Usecode-instructions?
// 0 = no, 1 = short, 2 = long
bool combat_trace = false; // show combat messages?
// Save game compression level
int save_compression = 1;
bool ignore_crc = false;
TouchUI* touchui = nullptr;
bool g_waiting_for_click = false;
ShortcutBar_gump* g_shortcutBar = nullptr;
#ifdef USECODE_DEBUGGER
bool usecode_debugging = false; // Do we enable the usecode debugger?
extern void initialise_usecode_debugger();
#endif
int current_scaleval = 1;
#if defined(_WIN32) && defined(USE_EXULTSTUDIO)
static HWND hgwin;
static Windnd* windnd = nullptr;
#endif
/*
* Local functions:
*/
static int exult_main(const char* runpath);
static void Init();
static int Play();
static bool Get_click(
int& x, int& y, char* chr, bool drag_ok, bool rotate_colors = false);
static void set_scaleval(int new_scaleval);
#ifdef USE_EXULTSTUDIO
static void Move_dragged_shape(
int shape, int frame, int x, int y, int prevx, int prevy, bool show);
# ifdef _WIN32
static void Move_dragged_combo(
int xtiles, int ytiles, int tiles_right, int tiles_below, int x, int y,
int prevx, int prevy, bool show);
# endif
static void Drop_dragged_shape(int shape, int frame, int x, int y);
static void Drop_dragged_chunk(int chunknum, int x, int y);
static void Drop_dragged_npc(int npcnum, int x, int y);
static void Drop_dragged_combo(int cnt, U7_combo_data* combo, int x, int y);
#endif
static void BuildGameMap(BaseGameInfo* game, int mapnum);
static void Handle_events();
static void Handle_event(SDL_Event& event);
/*
* Statics:
*/
static bool run_bg = false; // skip menu and run bg
static bool run_si = false; // skip menu and run si
static bool run_fov = false; // skip menu and run fov
static bool run_ss = false; // skip menu and run ss
static bool run_sib = false; // skip menu and run sib
static string arg_gamename = "default"; // cmdline arguments
static string arg_modname = "default"; // cmdline arguments
static string arg_configfile;
static int arg_buildmap = -1;
static int arg_mapnum = -1;
static bool arg_nomenu = false;
static bool arg_edit_mode = false; // Start up ExultStudio.
static bool arg_write_xml = false; // Write out game's config. as XML.
static bool arg_reset_video = false; // Resets the video setings.
static bool arg_verify_files = false; // Verify a game's files.
static bool dragging = false; // Object or gump being moved.
static bool dragged = false; // Flag for when obj. moved.
static bool right_on_gump = false; // Right clicked on gump?
static int show_items_x = 0, show_items_y = 0;
static unsigned int show_items_time = 0;
static bool show_items_clicked = false;
static int left_down_x = 0, left_down_y = 0;
static int joy_aim_x = 0, joy_aim_y = 0;
Mouse::Avatar_Speed_Factors joy_speed_factor = Mouse::medium_speed_factor;
static Uint32 last_speed_cursor = 0; // When we last updated the mouse cursor
#if defined _WIN32
void do_cleanup_output() {
cleanup_output("std");
}
#endif
/*
* Main program.
*/
#ifdef HAVE_PNG_H
# define EXULT_IMAGE_SUFFIX "png"
#else
# define EXULT_IMAGE_SUFFIX "pcx"
#endif // HAVE_PNG_H
int main(int argc, char* argv[]) {
bool needhelp = false;
bool showversion = false;
int result;
Args parameters;
// Use SDL_RWops for file I/O in the main game engine for better
// cross-platform support. Standalone utilities continue to default
// to the default std::fstream-based file I/O to avoid taking an SDL
// dependency.
U7set_istream_factory(
[](const char* s,
std::ios_base::openmode mode) -> std::unique_ptr<std::istream> {
auto file = std::make_unique<std::ifstream>(s, mode);
if (file->good()) {
return file;
}
return std::make_unique<SdlRwopsIstream>(s, mode);
});
U7set_ostream_factory(
[](const char* s,
std::ios_base::openmode mode) -> std::unique_ptr<std::ostream> {
auto file = std::make_unique<std::ofstream>(s, mode);
if (file->good()) {
return file;
}
return std::make_unique<SdlRwopsOstream>(s, mode);
});
#ifdef ANDROID
// Doing this ifdef here rather than in utils.cc so that it doesn't have to
// take a dependency on SDL.
U7set_home(SDL_AndroidGetInternalStoragePath());
#endif
// Declare everything from the commandline that we're interested in.
parameters.declare("-h", &needhelp, true);
parameters.declare("--help", &needhelp, true);
parameters.declare("/?", &needhelp, true);
parameters.declare("/h", &needhelp, true);
parameters.declare("--bg", &run_bg, true);
parameters.declare("--si", &run_si, true);
parameters.declare("--fov", &run_fov, true);
parameters.declare("--ss", &run_ss, true);
parameters.declare("--sib", &run_sib, true);
parameters.declare("--nomenu", &arg_nomenu, true);
parameters.declare("-v", &showversion, true);
parameters.declare("--version", &showversion, true);
parameters.declare("--game", &arg_gamename, "default");
parameters.declare("--mod", &arg_modname, "default");
parameters.declare("--buildmap", &arg_buildmap, -1);
parameters.declare("--mapnum", &arg_mapnum, -1);
parameters.declare("--nocrc", &ignore_crc, true);
parameters.declare("-c", &arg_configfile, "");
parameters.declare("--edit", &arg_edit_mode, true);
parameters.declare("--write-xml", &arg_write_xml, true);
parameters.declare("--reset-video", &arg_reset_video, true);
parameters.declare("--verify-files", &arg_verify_files, true);
#if defined _WIN32
bool portable = false;
parameters.declare("-p", &portable, true);
#endif
// Process the args
parameters.process(argc, argv);
add_system_path("<alt_cfg>", arg_configfile);
#if defined _WIN32
if (portable) {
add_system_path("<HOME>", ".");
}
setup_program_paths();
redirect_output("std");
std::atexit(do_cleanup_output);
#endif
if (needhelp) {
cerr << "Usage: exult [--help|-h] [-v|--version] [-c configfile]"
<< endl
<< " [--bg|--fov|--si|--ss|--sib|--game <game>] "
"[--mod <mod>]"
<< endl
<< " [--nomenu] [--buildmap 0|1|2] [--mapnum <num>]"
<< endl
<< " [--nocrc] [--edit] [--write-xml] [--reset-video]"
<< endl
<< "--help\t\tShow this information" << endl
<< "--version\tShow version info" << endl
<< " -c configfile\tSpecify alternate config file" << endl
<< "--bg\t\tSkip menu and run Black Gate (prefers original game)"
<< endl
<< "--fov\t\tSkip menu and run Black Gate with Forge of Virtue "
"expansion"
<< endl
<< "--si\t\tSkip menu and run Serpent Isle (prefers original game)"
<< endl
<< "--ss\t\tSkip menu and run Serpent Isle with Silver Seed "
"expansion"
<< endl
<< "--sib\t\tSkip menu and run Serpent Isle Beta" << endl
<< "--nomenu\tSkip BG/SI game menu" << endl
<< "--game <game>\tSkip menu and run the game with key '<game>' "
"in Exult.cfg"
<< endl
<< "--mod <mod>\tMust be used together with '--bg', '--fov', "
"'--si', '--ss', '--sib' or"
<< endl
<< "\t\t'--game <game>'; runs the specified game using the mod "
"with"
<< endl
<< "\t\ttitle equal to '<mod>'" << endl
<< "--buildmap <N>\tCreate a fullsize map of the game world in "
"u7map??."
<< EXULT_IMAGE_SUFFIX << endl
<< "\t\t(N=0: all roofs, 1: no level 2 roofs, 2: no roofs)" << endl
<< "\t\tOnly valid if used together with '--bg', '--fov', '--si', "
"'--ss', '--sib'"
<< endl
<< "\t\tor '--game <game>'; you may optionally specify a mod with"
<< endl
<< "\t\t'--mod <mod>' (WARNING: requires big amounts of RAM, HD"
<< endl
<< "\t\tspace and time!)" << endl
<< "--mapnum <N>\tThis must be used with '--buildmap'. Selects "
"which map"
<< endl
<< "\t\t(for multimap games or mods) whose map is desired" << endl
<< "--nocrc\t\tDon't check crc's of .flx files" << endl
<< "--verify-files\tVerifies that the files in static dir are not "
"corrupt"
<< endl
<< "\t\tOnly valid if used together with '--bg', '--fov', '--si', "
"'--ss', '--sib'"
<< endl
<< "\t\tor '--game <game>'; you cannot specify a mod with this "
"flag"
<< endl
<< "--edit\t\tStart in map-edit mode" << endl;
#if defined _WIN32
cerr << " -p\t\tMakes the home path the Exult directory (old Windows "
"way)"
<< endl;
#endif
cerr << "--write-xml\tWrite 'patch/exultgame.xml'" << endl
<< "--reset-video\tResets to the default video settings" << endl;
exit(1);
}
const unsigned gameparam
= static_cast<unsigned>(run_bg) + static_cast<unsigned>(run_si)
+ static_cast<unsigned>(run_fov) + static_cast<unsigned>(run_ss)
+ static_cast<unsigned>(run_sib)
+ static_cast<unsigned>(arg_gamename != "default");
if (gameparam > 1) {
cerr << "Error: You may only specify one of --bg, --fov, --si, --ss, "
"--sib or --game!"
<< endl;
exit(1);
} else if (arg_buildmap >= 0 && gameparam == 0) {
cerr << "Error: --buildmap requires one of --bg, --fov, --si, --ss, "
"--sib or --game!"
<< endl;
exit(1);
} else if (arg_verify_files && gameparam == 0) {
cerr << "Error: --verify-files requires one of --bg, --fov, --si, "
"--ss, --sib or --game!"
<< endl;
exit(1);
}
if (arg_mapnum >= 0 && arg_buildmap < 0) {
cerr << "Error: '--mapnum' requires '--buildmap'!" << endl;
exit(1);
} else if (arg_mapnum < 0) {
arg_mapnum = 0; // Sane default.
}
if (arg_modname != "default" && !gameparam) {
cerr << "Error: You must also specify the game to be used!" << endl;
exit(1);
} else if (arg_verify_files && arg_modname != "default") {
cerr << "Error: You cannot combine --mod with --verify-files!" << endl;
exit(1);
}
if (showversion) {
getVersionInfo(cerr);
return 0;
}
try {
result = exult_main(argv[0]);
} catch (const quit_exception& /*e*/) {
// Your basic "shutup Valgrind" code.
Free_text();
fontManager.reset();
delete gamemanager;
delete Game_window::get_instance();
delete game;
Audio::Destroy(); // Deinit the sound system.
delete config;
SDL_VideoQuit();
SDL_Quit();
result = 0;
} catch (const exult_exception& e) {
cerr << "============================" << endl
<< "An exception occured: " << endl
<< e.what() << endl
<< "errno: " << e.get_errno() << endl;
if (e.get_errno() != 0) {
perror("Error Description");
}
cerr << "============================" << endl;
result = e.get_errno();
}
return result;
}
/*
* Main program.
*/
int exult_main(const char* runpath) {
string music_path;
// output version info
getVersionInfo(cout);
#ifndef _WIN32
setup_program_paths();
#endif
// Read in configuration file
config = new Configuration;
if (!arg_configfile.empty()) {
config->read_abs_config_file(arg_configfile);
} else {
config->read_config_file(USER_CONFIGURATION_FILE);
}
// reset-video command line option
if (arg_reset_video) {
config->set("config/video/display/width", 1024, false);
config->set("config/video/display/height", 768, false);
config->set("config/video/game/width", 320, false);
config->set("config/video/game/height", 200, false);
config->set("config/video/scale", 2, false);
config->set("config/video/scale_method", "point", false);
config->set("config/video/fill_mode", "fit", false);
config->set("config/video/fill_scaler", "point", false);
config->set("config/video/share_video_settings", "yes", false);
config->set("config/video/fullscreen", "no", false);
config->set("config/video/force_bpp", 0, false);
config->write_back();
}
if (config->key_exists("config/gameplay/allow_double_right_move")) {
string str;
config->value("config/gameplay/allow_double_right_move", str, "yes");
if (str == "no") {
config->value("config/gameplay/allow_right_pathfind", str, "no");
config->set("config/gameplay/allow_right_pathfind", str, false);
}
config->remove("config/gameplay/allow_double_right_move", false);
}
// Setup virtual directories
string data_path;
config->value("config/disk/data_path", data_path, EXULT_DATADIR);
setup_data_dir(data_path, runpath);
const std::string default_music = get_system_path("<DATA>/music");
config->value("config/disk/music_path", music_path, default_music.c_str());
add_system_path("<MUSIC>", music_path);
add_system_path("<STATIC>", "static");
add_system_path("<GAMEDAT>", "gamedat");
add_system_path("<PATCH>", "patch");
// add_system_path("<SAVEGAME>", "savegame");
add_system_path("<SAVEGAME>", ".");
add_system_path("<MODS>", "mods");
std::cout << "Exult path settings:" << std::endl;
#if defined(MACOSX) || defined(__IPHONEOS__)
if (is_system_path_defined("<APP_BUNDLE_RES>")) {
std::cout << "Bundled Data : " << get_system_path("<BUNDLE>")
<< std::endl;
}
#endif
std::cout << "Data : " << get_system_path("<DATA>") << std::endl;
std::cout << "Digital music : " << get_system_path("<MUSIC>") << std::endl;
std::cout << std::endl;
// Check CRCs of our .flx files
bool crc_ok = true;
const char* flexname = BUNDLE_CHECK(BUNDLE_EXULT_FLX, EXULT_FLX);
const uint32 crc = crc32(flexname);
if (crc != EXULT_FLX_CRC32) {
crc_ok = false;
cerr << "exult.flx has a wrong checksum!" << endl;
}
flexname = BUNDLE_CHECK(BUNDLE_EXULT_BG_FLX, EXULT_BG_FLX);
if (crc32(flexname) != EXULT_BG_FLX_CRC32) {
crc_ok = false;
cerr << "exult_bg.flx has a wrong checksum!" << endl;
}
flexname = BUNDLE_CHECK(BUNDLE_EXULT_SI_FLX, EXULT_SI_FLX);
if (crc32(flexname) != EXULT_SI_FLX_CRC32) {
crc_ok = false;
cerr << "exult_si.flx has a wrong checksum!" << endl;
}
bool config_ignore_crc;
config->value("config/disk/no_crc", config_ignore_crc);
ignore_crc |= config_ignore_crc;
if (!ignore_crc && !crc_ok) {
cerr << "This usually means the file(s) mentioned above are "
<< "from a different version" << endl
<< "of Exult than this one. Please re-install Exult" << endl
<< endl
<< "(Note: if you modified the .flx files yourself, "
<< "you can skip this check" << endl
<< "by passing the --nocrc parameter.)" << endl;
return 1;
}
// Convert from old format if needed
const vector<string> vs = config->listkeys("config/disk/game", false);
if (vs.empty() && config->key_exists("config/disk/u7path")) {
// Convert from the older format
string data_directory;
config->value("config/disk/u7path", data_directory, "./blackgate");
config->remove("config/disk/u7path", false);
config->set("config/disk/game/blackgate/path", data_directory, true);
}
// Enable tracing of intrinsics?
config->value("config/debug/trace/intrinsics", intrinsic_trace);
// Enable tracing of UC-instructions?
string uctrace;
config->value("config/debug/trace/usecode", uctrace, "no");
to_uppercase(uctrace);
if (uctrace == "YES") {
usecode_trace = 1;
} else if (uctrace == "VERBOSE") {
usecode_trace = 2;
} else {
usecode_trace = 0;
}
config->value("config/debug/trace/combat", combat_trace);
// Save game compression level
config->value("config/disk/save_compression_level", save_compression, 1);
if (save_compression < 0 || save_compression > 2) {
save_compression = 1;
}
config->set("config/disk/save_compression_level", save_compression, false);
#ifdef USECODE_DEBUGGER
// Enable usecode debugger
config->value("config/debug/debugger/enable", usecode_debugging);
initialise_usecode_debugger();
#endif
#if (defined(USECODE_DEBUGGER) && defined(XWIN))
signal(SIGUSR1, SIG_IGN);
#endif
cheat.init();
#ifdef __IPHONEOS__
touchui = new TouchUI_iOS();
#elif defined(ANDROID)
touchui = new TouchUI_Android();
#endif
Init(); // Create main window.
cheat.finish_init();
cheat.set_map_editor(arg_edit_mode); // Start in map-edit mode?
if (arg_write_xml) {
game->write_game_xml();
}
Mouse::mouse = new Mouse(gwin);
Mouse::mouse->set_shape(Mouse::hand);
if (touchui != nullptr) {
touchui->showButtonControls();
// TODO(Marzo): This is probably the wrong place for this
Usecode_machine* usecode = Game_window::get_instance()->get_usecode();
if (!usecode->get_global_flag(Usecode_machine::did_first_scene)
&& GAME_BG) {
touchui->hideGameControls();
} else {
touchui->showGameControls();
}
}
const int result = Play(); // start game
#ifdef USE_EXULTSTUDIO
// Currently, leaving the game results in destruction of the window.
// Maybe sometime in the future, there is an option like "return to
// main menu and select another scenario". Becaule DnD isn't registered
// until you really enter the game, we remove it here to prevent possible
// bugs invilved with registering DnD a second time over an old variable.
# if defined(_WIN32)
RevokeDragDrop(hgwin);
windnd->Release();
# endif
Server_close();
#endif
return result;
}
namespace ExultIcon {
#include "exulticon.h"
}
static void SetIcon() {
#ifndef MACOSX // Don't set icon on OS X; the external icon is *much* nicer
SDL_Color iconpal[256];
for (int i = 0; i < 256; ++i) {
iconpal[i].r = ExultIcon::header_data_cmap[i][0];
iconpal[i].g = ExultIcon::header_data_cmap[i][1];
iconpal[i].b = ExultIcon::header_data_cmap[i][2];
}
SDL_Surface* iconsurface = SDL_CreateRGBSurface(
0, ExultIcon::width, ExultIcon::height, 32, 0, 0, 0, 0);
if (iconsurface == nullptr) {
cout << "Error creating icon surface: " << SDL_GetError() << std::endl;
return;
}
for (int y = 0; y < static_cast<int>(ExultIcon::height); ++y) {
for (int x = 0; x < static_cast<int>(ExultIcon::width); ++x) {
const int idx = ExultIcon::header_data[(y * ExultIcon::height) + x];
const Uint32 pix = SDL_MapRGB(
iconsurface->format, iconpal[idx].r, iconpal[idx].g,
iconpal[idx].b);
const SDL_Rect destRect = {x, y, 1, 1};
SDL_FillRect(iconsurface, &destRect, pix);
}
}
SDL_SetColorKey(
iconsurface, SDL_TRUE,
SDL_MapRGB(
iconsurface->format, iconpal[0].r, iconpal[0].g,
iconpal[0].b));
SDL_SetWindowIcon(gwin->get_win()->get_screen_window(), iconsurface);
SDL_FreeSurface(iconsurface);
#endif
}
void Open_game_controller(int joystick_index) {
SDL_GameController* input_device = SDL_GameControllerOpen(joystick_index);
if (input_device) {
SDL_GameControllerGetJoystick(input_device);
std::cout << "Game controller attached and open: \""
<< SDL_GameControllerName(input_device) << '"' << std::endl;
} else {
std::cout
<< "Game controller attached, but it failed to open. Error: \""
<< SDL_GetError() << '"' << std::endl;
}
}
int Handle_device_connection_event(void* userdata, SDL_Event* event) {
ignore_unused_variable_warning(userdata);
// Make sure that game-controllers are opened and closed, as they
// become connected or disconnected.
switch (event->type) {
case SDL_CONTROLLERDEVICEADDED: {
const SDL_JoystickID joystick_id
= SDL_JoystickGetDeviceInstanceID(event->cdevice.which);
if (!SDL_GameControllerFromInstanceID(joystick_id)) {
Open_game_controller(event->cdevice.which);
}
break;
}
case SDL_CONTROLLERDEVICEREMOVED: {
SDL_GameController* input_device
= SDL_GameControllerFromInstanceID(event->cdevice.which);
if (input_device) {
SDL_GameControllerClose(input_device);
input_device = nullptr;
std::cout << "Game controller detached and closed." << std::endl;
}
break;
}
}
// Returning 1 will tell SDL2, which can invoke this via a callback
// setup through SDL_AddEventWatch, to make sure the event gets posted
// to its event-queue (rather than dropping it).
return 1;
}
/*
* Initialize and create main window.
*/
static void Init() {
const Uint32 init_flags
= SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER;
#ifdef NO_SDL_PARACHUTE
const Uint32 parachute = SDL_INIT_NOPARACHUTE;
#else
const Uint32 parachute = 0;
#endif
#ifdef _WIN32
SDL_putenv("SDL_AUDIODRIVER=DirectSound");
#elif defined(MACOSX) && defined(XWIN) && defined(USE_EXULTSTUDIO)
// Exult Studio drag'n'drop with SDL2 < 2.0.15 requires Exult
// to use X11. Hence, we force the issue.
SDL_putenv("SDL_VIDEODRIVER=x11");
#elif defined(MACOSX)
SDL_SetHint(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, "0");
#endif
SDL_SetHint(SDL_HINT_ORIENTATIONS, "Landscape");
#if 0
const Uint32 joyinit = SDL_INIT_JOYSTICK;
#else
const Uint32 joyinit = 0;
#endif
#if defined(__IPHONEOS__) || defined(ANDROID)
Mouse::use_touch_input = true;
#endif
#ifdef __IPHONEOS__
SDL_SetHint(SDL_HINT_IOS_HIDE_HOME_INDICATOR, "2");
#endif
if (SDL_Init(init_flags | parachute | joyinit) < 0) {
cerr << "Unable to initialize SDL: " << SDL_GetError() << endl;
exit(-1);
}
std::atexit(SDL_Quit);
SDL_SysWMinfo info; // Get system info.
// KBD repeat should be nice.
SDL_ShowCursor(0);
SDL_VERSION(&info.version)
// Open any connected game controllers.
for (int i = 0, n = SDL_NumJoysticks(); i < n; ++i) {
if (SDL_IsGameController(i)) {
Open_game_controller(i);
}
}
// Listen for game controller device connection and disconnection
// events. Registering a listener allows these events to be received
// and processed via any event-processing loop, of which Exult has
// many, without needing to modify each individual loop, and to
// make sure that SDL_GameController objects are always ready.
SDL_AddEventWatch(Handle_device_connection_event, nullptr);
// Load games and mods; also stores system paths:
gamemanager = new GameManager();
if (arg_buildmap < 0 && !arg_verify_files) {
string gr;
string gg;
string gb;
config->value("config/video/gamma/red", gr, "1.0");
config->value("config/video/gamma/green", gg, "1.0");
config->value("config/video/gamma/blue", gb, "1.0");
Image_window8::set_gamma(
atof(gr.c_str()), atof(gg.c_str()), atof(gb.c_str()));
string fullscreenstr; // Check config. for fullscreen mode.
config->value("config/video/fullscreen", fullscreenstr, "no");
const bool fullscreen = (fullscreenstr == "yes");
config->set(
"config/video/fullscreen", fullscreen ? "yes" : "no", false);
int border_red;
int border_green;
int border_blue;
config->value("config/video/game/border/red", border_red, 0);
if (border_red < 0) {
border_red = 0;
} else if (border_red > 255) {
border_red = 255;
}
config->set("config/video/game/border/red", border_red, false);
config->value("config/video/game/border/green", border_green, 0);
if (border_green < 0) {
border_green = 0;
} else if (border_green > 255) {
border_green = 255;
}
config->set("config/video/game/border/green", border_green, false);
config->value("config/video/game/border/blue", border_blue, 0);
if (border_blue < 0) {
border_blue = 0;
} else if (border_blue > 255) {
border_blue = 255;
}
config->set("config/video/game/border/blue", border_blue, false);
Palette::set_border(border_red, border_green, border_blue);
bool disable_fades;
config->value("config/video/disable_fades", disable_fades, false);
setup_video(fullscreen, VIDEO_INIT);
SetIcon();
Audio::Init();
gwin->get_pal()->set_fades_enabled(!disable_fades);
gwin->set_in_exult_menu(false);
}
SDL_SetEventFilter(nullptr, nullptr);
// Show the banner
game = nullptr;
do {
reset_system_paths();
fontManager.reset();
U7FileManager::get_ptr()->reset();
if (game) {
delete game;
game = nullptr;
}
ModManager* basegame = nullptr;
if (run_bg) {
basegame = gamemanager->get_bg();
arg_gamename = CFG_BG_NAME;
run_bg = false;
} else if (run_fov) {
basegame = gamemanager->get_fov();
arg_gamename = CFG_FOV_NAME;
run_fov = false;
} else if (run_si) {
basegame = gamemanager->get_si();
arg_gamename = CFG_SI_NAME;
run_si = false;
} else if (run_ss) {
basegame = gamemanager->get_ss();
arg_gamename = CFG_SS_NAME;
run_ss = false;
} else if (run_sib) {
basegame = gamemanager->get_sib();
arg_gamename = CFG_SIB_NAME;
run_sib = false;
}
BaseGameInfo* newgame = nullptr;
if (basegame || arg_gamename != "default") {
if (!basegame) {
basegame = gamemanager->find_game(arg_gamename);
}
if (basegame) {
if (arg_modname != "default") {
// Prints error messages:
newgame = basegame->get_mod(arg_modname);
} else {
newgame = basegame;
}
arg_modname = "default";
} else {
cerr << "Game '" << arg_gamename << "' not found." << endl;
newgame = nullptr;
}
// Prevent game from being reloaded in case the player
// tries to return to the main menu:
arg_gamename = "default";
}
if (!newgame) {
ExultMenu exult_menu(gwin);
newgame = exult_menu.run();
}
assert(newgame != nullptr);
if (arg_buildmap >= 0) {
BuildGameMap(newgame, arg_mapnum);
exit(0);
}
if (arg_verify_files) {
newgame->setup_game_paths();
exit(verify_files(newgame));
}
Game::create_game(newgame);
Audio* audio = Audio::get_ptr();
audio->Init_sfx();
MyMidiPlayer* midi = audio->get_midi();
Setup_text(GAME_SI, Game::has_expansion(), GAME_SIB);
// Skip splash screen?
bool skip_splash;
config->value("config/gameplay/skip_splash", skip_splash);
// Make sure we have a proper palette before playing the intro.
gwin->get_pal()->load(
BUNDLE_CHECK(BUNDLE_EXULT_FLX, EXULT_FLX),
EXULT_FLX_EXULT0_PAL);
gwin->get_pal()->apply();
if (!skip_splash
&& (Game::get_game_type() != EXULT_DEVEL_GAME
|| U7exists(INTRO_DAT))) {
if (midi) {
midi->set_timbre_lib(MyMidiPlayer::TIMBRE_LIB_INTRO);
}
game->play_intro();
std::cout << "played intro" << std::endl;
}
if (midi) {
if (arg_nomenu) {
midi->set_timbre_lib(MyMidiPlayer::TIMBRE_LIB_INTRO);
} else {
midi->set_timbre_lib(MyMidiPlayer::TIMBRE_LIB_MAINMENU);
}
}
} while (!game || !game->show_menu(arg_nomenu));
// Should not be needed anymore:
delete gamemanager;
gamemanager = nullptr;
Audio* audio = Audio::get_ptr();
MyMidiPlayer* midi = audio->get_midi();
if (midi) {
midi->set_timbre_lib(MyMidiPlayer::TIMBRE_LIB_GAME);
}
gwin->init_files();
gwin->read_gwin();
gwin->setup_game(arg_edit_mode); // This will start the scene.