This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
forked from litchie/exult-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exult.cc
2888 lines (2655 loc) · 87.1 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-2013 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 <cstdlib>
#include <cctype>
#include "sdl-compat.h"
#include <SDL.h>
#define Font _XFont_
#include <SDL_syswm.h>
#undef Font
#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"
#elif defined(XWIN)
#include "xdrag.h"
#endif
#include "servemsg.h"
#include "objserial.h"
#include "server.h"
#include "chunks.h"
#include "chunkter.h"
#endif
#if (defined(USECODE_DEBUGGER) && defined(XWIN))
#include <csignal>
#endif
#include "Audio.h"
#include "Configuration.h"
#include "Gump_manager.h"
#include "Scroll_gump.h"
#include "actors.h"
#include "args.h"
#include "cheat.h"
#include "effects.h"
#include "exult.h"
#include "exultmenu.h"
#include "fnames.h"
#include "font.h"
#include "game.h"
#include "gamewin.h"
#include "gamemap.h"
#include "gump_utils.h"
#include "keyactions.h"
#include "keys.h"
#include "mouse.h"
#include "ucmachine.h"
#include "utils.h"
#include "version.h"
#include "u7drag.h"
#include "drag.h"
#include "palette.h"
#include "glshape.h"
#include "combat_opts.h"
#include "U7file.h"
#include "U7fileman.h"
#include "party.h"
#include "exult_flx.h"
#include "exult_bg_flx.h"
#include "exult_si_flx.h"
#include "crc.h"
#include "items.h"
#include "gamemgr/modmgr.h"
#include "AudioMixer.h"
#include "VideoOptions_gump.h"
#include "Gump_button.h"
#include "ShortcutBar_gump.h"
#include "ignore_unused_variable_warning.h"
using namespace Pentagram;
#ifdef __IPHONEOS__
# include "iphone_gumps.h"
# include "touchui.h"
# include "ios_utils.h"
#endif
using std::atof;
using std::cerr;
using std::cout;
using std::endl;
using std::atexit;
using std::exit;
using std::toupper;
using std::string;
using std::vector;
#if SDL_VERSION_ATLEAST(2, 0, 0) && (defined(WIN32) || (defined(MACOSX) && defined(USE_EXULTSTUDIO)))
static int SDLCALL SDL_putenv(const char *_var) {
char *ptr = NULL;
char *var = SDL_strdup(_var);
if (var == NULL) {
return -1; /* we don't set errno. */
}
ptr = SDL_strchr(var, '=');
if (ptr == NULL) {
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 = 0;
KeyBinder *keybinder = 0;
GameManager *gamemanager = 0;
/*
* Globals:
*/
Game_window *gwin = 0;
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;
const std::string c_empty_string;
#ifdef __IPHONEOS__
SDL_Joystick *sdl_joy;
TouchUI *touchui;
#endif
bool g_waiting_for_click = false;
ShortcutBar_gump *g_shortcutBar = NULL;
#ifdef USECODE_DEBUGGER
bool usecode_debugging = false; // Do we enable the usecode debugger?
extern void initialise_usecode_debugger(void);
#endif
struct resolution {
int x;
int y;
int scale;
} res_list[] = {
{ 320, 200, 1 },
{ 320, 240, 1 },
{ 400, 300, 1 },
{ 320, 200, 2 },
{ 320, 240, 2 },
{ 400, 300, 2 },
{ 512, 384, 1 },
{ 640, 480, 1 },
{ 800, 600, 1 }
};
int num_res = sizeof(res_list) / sizeof(struct resolution);
int current_res = 0;
int current_scaleval = 1;
#ifdef XWIN
# ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wold-style-cast"
# endif // __GNUC__
int xfd = 0; // X connection #.
static inline int WrappedConnectionNumber(Display *display) {
return ConnectionNumber(display);
}
#ifdef USE_EXULTSTUDIO
static class Xdnd *xdnd = 0;
#endif
# ifdef __GNUC__
# pragma GCC diagnostic pop
# endif // __GNUC__
#elif defined(WIN32)
static HWND hgwin;
static class Windnd *windnd = 0;
#endif
/*
* Local functions:
*/
static int exult_main(const char * runpath);
static void Init();
static int Play();
static int Get_click(int &x, int &y, char *chr, bool drag_ok, Paintable *p, bool rotate_colors = false);
static int find_resolution(int w, int h, int s);
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);
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);
static void Drop_dragged_shape(int shape, int frame, int x, int y, void *d);
static void Drop_dragged_chunk(int chunknum, int x, int y, void *d);
static void Drop_dragged_npc(int npcnum, int x, int y, void *d);
static void Drop_dragged_combo(int cnt, U7_combo_data *combo,
int x, int y, void *d);
#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 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;
#if (defined(XWIN) && HAVE_SIGNAL_H && HAVE_SYS_WAIT_H)
// a SIGCHLD handler to properly clean up forked playmidi processes (if any)
#include <sys/wait.h>
#include <signal.h>
void sigchld_handler(int sig) {
ignore_unused_variable_warning(sig);
waitpid(-1, 0, WNOHANG);
}
#endif
#if defined(_WIN32) && defined(main) && defined(_MSC_VER)
#undef main
#endif
/*
* Main program.
*/
int main(
int argc,
char *argv[]
) {
#if (defined(XWIN) && HAVE_SIGNAL_H && HAVE_SYS_WAIT_H)
signal(SIGCHLD, sigchld_handler);
#endif
bool needhelp = false;
bool showversion = false;
int result;
Args parameters;
// 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);
#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");
#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>\tRun original game" << 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??.pcx" << 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
<< "--edit\t\tStart in map-edit mode" << endl
#if defined WIN32
<< " -p\t\tMakes the home path the Exult directory (old Windows way)" << endl
#endif
<< "--write-xml\tWrite 'patch/exultgame.xml'" << endl
<< "--reset-video\tResets to the default video settings" << endl;
exit(1);
}
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);
}
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);
}
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;
}
#ifdef _WIN32
#include <windows.h>
#include <cstdio>
// From Pentagram:
PCHAR *CommandLineToArgvA(
PCHAR CmdLine,
int *_argc
) {
PCHAR *argv;
PCHAR _argv;
ULONG len;
ULONG argc;
CHAR a;
ULONG i, j;
BOOLEAN in_QM;
BOOLEAN in_TEXT;
BOOLEAN in_SPACE;
len = strlen(CmdLine);
i = ((len + 2) / 2) * sizeof(PVOID) + sizeof(PVOID);
argv = reinterpret_cast<PCHAR *>(GlobalAlloc(GMEM_FIXED, i + (len + 2) * sizeof(CHAR)));
_argv = reinterpret_cast<PCHAR>(reinterpret_cast<PUCHAR>(argv) + i);
argc = 0;
argv[argc] = _argv;
in_QM = FALSE;
in_TEXT = FALSE;
in_SPACE = TRUE;
i = 0;
j = 0;
while ((a = CmdLine[i]) != 0) {
if (in_QM) {
if (a == '\"')
in_QM = FALSE;
else {
_argv[j] = a;
j++;
}
} else {
switch (a) {
case '\"':
in_QM = TRUE;
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
in_SPACE = FALSE;
break;
case ' ':
case '\t':
case '\n':
case '\r':
if (in_TEXT) {
_argv[j] = '\0';
j++;
}
in_TEXT = FALSE;
in_SPACE = TRUE;
break;
default:
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = FALSE;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
(*_argc) = argc;
return argv;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
/* Pulled from SDL_win32_main.c; this comment is also theirs:
Start up DDHELP.EXE before opening any files, so DDHELP doesn't
keep them open. This is a hack.. hopefully it will be fixed
someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
*/
HMODULE hLib = LoadLibrary(TEXT("DDRAW.DLL"));
if (hLib != NULL)
FreeLibrary(hLib);
int argc;
char **argv = CommandLineToArgvA(GetCommandLineA(), &argc);
int res = main(argc, argv);
cleanup_output("std");
GlobalFree(reinterpret_cast<HGLOBAL>(argv));
ignore_unused_variable_warning(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
return res;
}
#endif
/*
* 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", 640, false);
config->set("config/video/display/height", 480, 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", "2xSaI" , false);
config->set("config/video/fill_mode", "center", false);
config->set("config/video/fill_scaler", "Bilinear", 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);
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;
#ifdef __IPHONEOS__
std::cout << "Bundle : " << 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);
uint32 crc = crc32_syspath(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 (U7exists(flexname)) {
if (crc32_syspath(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 (U7exists(flexname)) {
if (crc32_syspath(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
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();
#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);
#ifdef __IPHONEOS__
touchui->showButtonControls();
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();
#endif
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);
delete windnd;
#else
delete xdnd;
#endif
#endif
return result;
}
namespace ExultIcon {
#include "exulticon.h"
}
#ifndef MACOSX // Don't set icon on OS X; the external icon is *much* nicer
static void SetIcon() {
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];
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_Surface *iconsurface = SDL_CreateRGBSurface(0,
ExultIcon::width,
ExultIcon::height,
32,
0, 0, 0, 0);
if (iconsurface == NULL)
cout << "Error creating icon surface: " << SDL_GetError() << std::endl;
for (int y = 0; y < static_cast<int>(ExultIcon::height); ++y)
{
for (int x = 0; x < static_cast<int>(ExultIcon::width); ++x)
{
int idx = ExultIcon::header_data[(y*ExultIcon::height)+x];
Uint32 pix = SDL_MapRGB(iconsurface->format,
iconpal[idx].r,
iconpal[idx].g,
iconpal[idx].b);
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);
#else
SDL_Surface *iconsurface = SDL_CreateRGBSurfaceFrom(ExultIcon::header_data,
ExultIcon::width,
ExultIcon::height,
8,
ExultIcon::width,
0, 0, 0, 0);
SDL_SetPalette(iconsurface, SDL_LOGPAL, iconpal, 0, 256);
SDL_SetColorKey(iconsurface, SDL_SRCCOLORKEY, 0);
SDL_WM_SetIcon(iconsurface, 0);
#endif
SDL_FreeSurface(iconsurface);
}
#endif
/*
* Initialize and create main window.
*/
static void Init(
) {
Uint32 init_flags = SDL_INIT_VIDEO | SDL_INIT_TIMER;
#ifdef NO_SDL_PARACHUTE
init_flags |= SDL_INIT_NOPARACHUTE;
#endif
#ifdef WIN32
// Due to the new menu size, the window can sometimes
// be partially offscreen in Windows. SDL currently
// offers no better way of doing this, so...
// I don't know if this will be problem in other OSes,
// so I leave it Windows-specific for now.
// Maybe it would be best to use SDL_putenv instead?
SDL_putenv(const_cast<char *>("SDL_VIDEO_CENTERED=center"));
// Yes, SDL_putenv is GOOD. putenv does NOT work with wince. -PTG 2007/06/11
#elif defined(MACOSX) && defined(USE_EXULTSTUDIO)
// Just in case:
#ifndef XWIN
#error "Incompatible preprocessor definitions: simultaneous use of \"MACOSX\" and \"USE_EXULTSTUDIO\" require \"XWIN\" to be defined."
#endif
// Exult Studio support in Mac OS X is experimental and requires
// SDL to use X11. Hence, we force the issue.
SDL_putenv(const_cast<char *>("SDL_VIDEODRIVER=x11"));
#endif
#ifdef __IPHONEOS__
init_flags |= SDL_INIT_JOYSTICK;
SDL_SetHint(SDL_HINT_ORIENTATIONS, "Landscape");
SDL_SetHint(SDL_HINT_IOS_HIDE_HOME_INDICATOR, "2");
#endif
if (SDL_Init(init_flags) < 0) {
cerr << "Unable to initialize SDL: " << SDL_GetError() << endl;
exit(-1);
}
std::atexit(SDL_Quit);
#ifdef __IPHONEOS__
#if 0 // FIXME: temporarily disabled
std::cout << "There are " << SDL_NumJoysticks() << " joystick(s) available" << std::endl;
std::cout << "Default joystick (index 0) is " << SDL_JoystickName(0) << std::endl;
sdl_joy = SDL_JoystickOpen(0);
if (sdl_joy == NULL)
std::cout << "Error: could not open joystick" << std::endl;
std::cout << "joystick number of axis: " << SDL_JoystickNumAxes(sdl_joy) << ", number of hats: " << SDL_JoystickNumHats(sdl_joy) << ", number of balls: " << SDL_JoystickNumBalls(sdl_joy) << ", number of buttons: " << SDL_JoystickNumButtons(sdl_joy) << std::endl;
#endif
#endif
SDL_SysWMinfo info; // Get system info.
#if SDL_VERSION_ATLEAST(2, 0, 0)
// Doesn't look like info is used for anything... let's try not getting it
//SDL_GetWindowWMInfo(gwin->get_win()->get_screen_window(), &info);
#else
SDL_GetWMInfo(&info);
#endif
#ifdef USE_EXULTSTUDIO
// Want drag-and-drop events.
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
#endif
// KBD repeat should be nice.
#if !(SDL_VERSION_ATLEAST(2, 0, 0)) // Not seeing the keyboard repeat options in SDL2
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
SDL_DEFAULT_REPEAT_INTERVAL);
#endif
SDL_ShowCursor(0);
SDL_VERSION(&info.version);
// Load games and mods; also stores system paths:
gamemanager = new GameManager();
if (arg_buildmap < 0) {
string gr, gg, 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(static_cast<float>(atof(gr.c_str())),
static_cast<float>(atof(gg.c_str())),
static_cast<float>(atof(gb.c_str())));
string fullscreenstr; // Check config. for fullscreen mode.
config->value("config/video/fullscreen", fullscreenstr, "no");
bool fullscreen = (fullscreenstr == "yes");
config->set("config/video/fullscreen", fullscreen ? "yes" : "no", false);
int border_red, border_green, 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);
#ifndef MACOSX // Don't set icon on OS X; the external icon is *much* nicer
SetIcon();
#endif
Audio::Init();
gwin->get_pal()->set_fades_enabled(!disable_fades);
gwin->set_in_exult_menu(false);
}
SDL_SETEVENTFILTER(0);
// Show the banner
game = 0;
do {
reset_system_paths();
fontManager.reset();
U7FileManager::get_ptr()->reset();
if (game) {
delete game;
game = 0;
}
ModManager *basegame = 0;
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 = 0;
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 = 0;
}
// Prevent game from being reloaded in case the player
// tries to return to the main menu:
arg_gamename = "default";
} else {
ExultMenu exult_menu(gwin);
newgame = exult_menu.run();
}
if (!newgame) {
// Should never happen... maybe display the exult menu instead?
cerr << "Could not find any games to run; leaving." << endl;
exit(1);
}
if (arg_buildmap >= 0) {
BuildGameMap(newgame, arg_mapnum);
exit(0);
}
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;
}