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
/
gamewin.cc
3271 lines (2995 loc) · 90.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-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 <cstring>
#include <cstdarg>
#include <cstdio>
#include "Astar.h"
#include "Audio.h"
#include "Configuration.h"
#include "chunks.h"
#include "gamemap.h"
#include "Face_stats.h"
#include "Flex.h"
#include "Gump.h"
#include "Gump_manager.h"
#include "ShortcutBar_gump.h"
#include "actions.h"
#include "monsters.h"
#include "animate.h"
#include "barge.h"
#include "cheat.h"
#include "chunkter.h"
#include "combat_opts.h"
#include "delobjs.h"
#include "dir.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 "gamewin.h"
#include "gameclk.h"
#include "gamerend.h"
#include "items.h"
//#include "jawbone.h" // CHECKME: this doesn't seem to be needed
#include "keys.h"
#include "mouse.h"
#include "npcnear.h"
#include "objiter.h"
#include "paths.h"
#include "schedule.h"
#include "spellbook.h"
#include "ucmachine.h"
#include "ucsched.h" /* Only used to flush objects. */
#include "utils.h"
#include "virstone.h"
#include "mappatch.h"
#include "version.h"
#include "drag.h"
#include "glshape.h"
#include "party.h"
#include "Notebook_gump.h"
#include "AudioMixer.h"
#include "combat.h"
#include "keyactions.h"
#include "monstinf.h"
#include "usefuns.h"
#include "audio/midi_drivers/XMidiFile.h"
#include "array_size.h"
#ifdef USE_EXULTSTUDIO
#include "server.h"
#include "servemsg.h"
#endif
#ifdef __IPHONEOS__
#include "iphone_gumps.h"
#endif
using std::cerr;
using std::cout;
using std::endl;
using std::istream;
using std::ifstream;
using std::ios;
using std::memset;
using std::ofstream;
using std::rand;
using std::string;
using std::srand;
using std::vector;
// THE game window:
Game_window *Game_window::game_window = 0;
/*
* 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);
}
virtual ~Background_noise() {
gwin->get_tqueue()->remove(this);
}
virtual void handle_event(unsigned long curtime, uintptr udata);
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;
int bghour = gwin->get_clock()->get_hour();
int weather = gwin->get_effects()->get_weather();
bool nighttime = bghour < 6 || bghour > 20;
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; //Night time
else
currentstate = Outside;
MyMidiPlayer *player = Audio::get_ptr()->get_midi();
// Lets allow this for Digital Muisc and MT32Emu only,
// for MT32/FakeMT32 conversion as well.
// if (player) {
//if (player && player->get_ogg_enabled()){
if (player && (player->get_ogg_enabled() || player->is_mt32() || player->is_adlib())) {
delay = 1000; //Quickly get back to this function check
//We've got OGG so play the background SFX tracks
int curr_track = player->get_current_track();
// Testing. Original seems to allow crickets for all songs at night,
// except when in a dungeon. Also, only do it sometimes.
if (nighttime && currentstate != Dungeon && rand() % 6 == 0) {
//Play the cricket sounds at night
Audio::get_ptr()->play_sound_effect(Audio::game_sfx(61),
AUDIO_MAX_VOLUME - 30);
}
if ((curr_track == -1 || laststate != currentstate) &&
Audio::get_ptr()->is_music_enabled()) {
// Don't override bee cave music with dungeon music.
bool notbees = !GAME_BG || curr_track != 54;
// ++++ TODO: Need to come up with a way to replace repeating songs
// here, just so they don't loop forever.
// Conditions: not playing music, playing a background music
if (curr_track == -1 || gwin->is_bg_track(curr_track) ||
(((currentstate == Dungeon && notbees) ||
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()) {
//Start the SFX music track then
tracknum = Audio::game_music(52);
laststate = Dungeon;
} else if (weather == 1) { // Snowstorm
tracknum = Audio::game_music(5);
laststate = SnowStorm;
} else if (weather == 2) { // Rainstorm
tracknum = Audio::game_music(4);
laststate = RainStorm;
} else if (bghour < 6 || bghour > 20) {
tracknum = Audio::game_music(7);
laststate = Nighttime;
} else {
//Start the SFX music track then
tracknum = Audio::game_music(6);
laststate = Outside;
}
Audio::get_ptr()->start_music(tracknum, true);
}
}
}else {
Main_actor *ava = gwin->get_main_actor();
//Tests to see if track is playing the SFX tracks, possible
//when the game has been restored
//and the Audio option was changed from OGG to something else
if (player && player->get_current_track() >= Audio::game_music(4) &&
player->get_current_track() <= Audio::game_music(8))
player->stop_music();
//Not OGG so play the SFX sounds manually
// Only if outside.
if (ava && !gwin->is_main_actor_inside() &&
// +++++SI SFX's don't sound right.
Game::get_game_type() == BLACK_GATE) {
int sound; // BG SFX #.
static unsigned char bgnight[] = {61, 61, 255},
bgday[] = {82, 85, 85};
if (repeats > 0) // Repeating?
sound = last_sound;
else {
int hour = gwin->get_clock()->get_hour();
if (hour < 6 || hour > 20)
sound = bgnight[rand() % sizeof(bgnight)];
else
sound = bgday[rand() % sizeof(bgday)];
// Translate BG to SI #'s.
sound = Audio::game_sfx(sound);
last_sound = sound;
}
Audio::get_ptr()->play_sound_effect(sound);
repeats++; // Count it.
if (rand() % (repeats + 1) == 0)
// Repeat.
delay = 500 + rand() % 1000;
else {
delay = 4000 + rand() % 3000;
repeats = 0;
}
}
}
gwin->get_tqueue()->add(curtime + delay, this, udata);
#endif
}
/*
* Set renderer (OpenGL or normal SDL).
*/
void Set_renderer(
Image_window8 *win,
Palette *pal,
bool resize
) {
GL_manager *glman = GL_manager::get_instance();
#ifdef HAVE_OPENGL
delete glman;
glman = 0;
if (win->get_scaler() == Image_window::OpenGL) {
glman = new GL_manager();
glman->set_palette(pal);
if (resize)
glman->resized(win->get_full_width(), win->get_full_height(),
win->get_scale_factor());
}
#else
ignore_unused_variable_warning(pal, resize);
#endif
// Tell shapes how to render.
Shape_frame::set_to_render(win->get_ib8(), glman);
}
#ifdef HAVE_OPENGL
/*
* Set palette and reset all textures. If given null palette, uses current
* game window palette.
*/
void GL_manager::set_palette(Palette *pal, bool rotation) {
Chunk_terrain::clear_glflats(rotation);
if (rotation) {
// Free only those that rotate.
GL_texshape *next = shapes;
while (next) {
GL_texshape *tex = next;
// Point to next element to be safe.
next = next->lru_next;
if (tex->has_palette_rotation()) {
// Unlink.
if (shapes == tex)
shapes = next;
if (tex->lru_next)
tex->lru_next->lru_prev = tex->lru_prev;
if (tex->lru_prev)
tex->lru_prev->lru_next = tex->lru_next;
delete tex;
}
}
} else // Kill them all.
while (shapes) {
GL_texshape *next = shapes->lru_next;
delete shapes;
shapes = next;
}
if (!palette)
palette = new unsigned char[768];
if (pal) {
for (int i = 0; i < 256; i++) {
// Palette colors are 0-63.
palette[3 * i] = 4 * pal->get_red(i);
palette[3 * i + 1] = 4 * pal->get_green(i);
palette[3 * i + 2] = 4 * pal->get_blue(i);
}
} else {
const unsigned char *cpal =
Game_window::get_instance()->get_win()->get_palette();
std::memcpy(palette, cpal, 768);
}
}
#endif
/*
* Set palette and reset all textures. If given null palette, uses current
* game window palette.
*/
bool Set_glpalette(Palette *pal, bool rotation) {
#ifdef HAVE_OPENGL
if (GL_manager::get_instance()) {
GL_manager::get_instance()->set_palette(pal, rotation);
return true;
}
#else
ignore_unused_variable_warning(pal, rotation);
#endif
return false;
}
/*
* 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(0), 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(0),
npc_prox(new Npc_proximity_handler(this)), pal(0),
tqueue(new Time_queue()), background_noise(new Background_noise(this)),
usecode(0), combat(false), focus(true), ice_dungeon(false),
painted(false), ambient_light(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(0), main_actor(0), camera_actor(0), npcs(0), bodies(0),
removed(new Deleted_objects()), scrolltx(0), scrollty(0), dirty(0, 0, 0, 0),
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),
#ifdef RED_PLASMA
load_palette_timer(0), plasma_start_color(0), plasma_cycle_range(0),
#endif
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) {
memset(save_names, 0, sizeof(save_names));
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 Ultima7 Engine");
pal = new Palette();
Game_singletons::init(this); // Everything but 'usecode' exists.
Set_renderer(win, pal, true);
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" ? true : false);
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);
// For now this is being set to default enabled because everyone wants it...
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);
config->value("config/gameplay/scroll_with_mouse", str, "no");
#ifdef __IPHONEOS__
scroll_with_mouse = str == "no";
#else
scroll_with_mouse = str == "yes";
#endif
config->set("config/gameplay/scroll_with_mouse",
scroll_with_mouse ? "yes" : "no", false);
#ifdef __IPHONEOS__
config->value("config/iphoneos/item_menu", str, "yes");
item_menu = str == "yes";
config->set("config/iphoneos/item_menu", item_menu ? "yes" : "no", false);
config->value("config/iphoneos/dpad_location", str, "right");
if (str == "no")
dpad_location = 0;
else if (str == "left")
dpad_location = 1;
else {
str = "right";
dpad_location = 2;
}
config->set("config/iphoneos/dpad_location", str, false);
config->value("config/shortcutbar/use_shortcutbar", str, "translucent");
config->value("config/iphoneos/touch_pathfind", str, "yes");
touch_pathfind = str == "yes";
config->set("config/iphoneos/touch_pathfind", touch_pathfind ? "yes" : "no", false);
#else
config->value("config/shortcutbar/use_shortcutbar", str, "no");
#endif
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);
#ifdef __IPHONEOS__
config->value("config/shortcutbar/use_outline_color", str, "black");
#else
config->value("config/shortcutbar/use_outline_color", str, "no");
#endif
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();
}
/*
* 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(1);
}
/*
* Deleting game window.
*/
Game_window::~Game_window(
) {
gump_man->close_all_gumps(true);
clear_world(false); // Delete all objects, chunks.
for (size_t i = 0; i < array_size(save_names); i++)
delete [] save_names[i];
delete shape_man;
delete gump_man;
delete party_man;
delete background_noise;
delete tqueue;
delete win;
delete dragging;
delete pal;
for (vector<Game_map *>::iterator it = maps.begin();
it != maps.end(); ++it)
delete *it;
delete usecode;
delete removed;
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];
vsprintf(buf, msg, ap); // Format the message.
cerr << "Exult (fatal): " << buf << endl;
delete this;
throw quit_exception(-1);
}
bool Game_window::is_bg_track(int num) { // ripped out of Background_noise
// Have to do it this way because of SI.
if (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))
return true;
else
return false;
}
#if 0
#define BLEND(alpha, newc, oldc) ((newc*255L) - (oldc*(255L-alpha)))/alpha
void Analyze_xform(unsigned char *xform, int alpha, Palette *pal) {
long br = 0, bg = 0, bb = 0; // Trying to figure out blend color.
for (int i = 0; i < 256; i++) {
int xi = xform[i]; // Index of color mapped to.
br += BLEND(alpha, pal->get_red(xi), pal->get_red(i));
bg += BLEND(alpha, pal->get_green(xi), pal->get_green(i));
bb += BLEND(alpha, pal->get_blue(xi), pal->get_blue(i));
}
br /= 256; // Take average.
bg /= 256;
bb /= 256;
cout << "Blend (r,g,b) = " << br << ',' << bg << ',' << bb << endl;
}
#endif
void Game_window::init_files(bool cycle) {
#ifdef RED_PLASMA
// Display red plasma during load...
if (cycle)
setup_load_palette();
#endif
usecode = Usecode_machine::create();
Game_singletons::init(this); // Everything should exist here.
CYCLE_RED_PLASMA();
shape_man->load(); // All the .vga files!
CYCLE_RED_PLASMA();
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, 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 &err) {
cerr << "Key mappings file '" << keyfilename << "' not found, falling back to default mappings." << endl;
keybinder->LoadDefaults();
}
}
keybinder->LoadFromPatch();
CYCLE_RED_PLASMA();
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] == 0) {
Game_map *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(
) {
return moving_barge ? moving_barge->is_moving()
: main_actor->is_moving();
}
/*
* Are we in dont_move mode?
*/
bool Game_window::main_actor_dont_move() {
return !cheat.in_map_editor() && main_actor != 0 && // Not if map-editing.
((main_actor->get_flag(Obj_flags::dont_move) != 0) ||
(main_actor->get_flag(Obj_flags::dont_render) != 0));
}
/*
* Are we in dont_move mode?
*/
bool Game_window::main_actor_can_act() {
return main_actor->can_act();
}
bool Game_window::main_actor_can_act_charmed() {
return main_actor->can_act_charmed();
}
/*
* 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 {
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;
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;
int cnt = party_man->get_count();
for (int i = 0; i < cnt; i++) {
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;
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(0); // And get out of barge mode.
Actor *all[9];
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(0);
}
} 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(npc);
else {
// Better be unused.
assert(!npcs[num] || npcs[num]->is_unused());
delete npcs[num];
npcs[num] = npc;
}
}
/*
* Show desired NPC.
*/
void Game_window::locate_npc(
int npc_num
) {
char msg[80];
Actor *npc = get_npc(npc_num);
if (!npc) {
snprintf(msg, 80, "NPC %d does not exist.", npc_num);
effects->center_text(msg);
} else if (npc->is_pos_invalid()) {
snprintf(msg, 80, "NPC %d is not on the map.", npc_num);
effects->center_text(msg);
} else {
// ++++WHAT IF on a different map???
Tile_coord pos = npc->get_tile();
center_view(pos);
cheat.clear_selected();
cheat.append_selected(npc);
snprintf(msg, 80, "NPC %d: '%s'.", npc_num,
npc->get_npc_name().c_str());
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())
break;
}
if (i >= 356 && i <= 359) {
// Special, 'reserved' cases.
i = 360;
do {
npcs.push_back(new Npc_actor("Reserved", 0));
npcs[cnt]->set_schedule_type(Schedule::wait);
npcs[cnt]->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);
Set_renderer(win, pal, true);
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, 80, "%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
) {
bool edit = cheat.in_map_editor();
cheat.set_map_editor(false);
Combat::resume();
tqueue->clear(); // Remove all entries.
clear_dirty();
removed->flush(); // Delete.
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 (vector<Actor *>::iterator it = npcs.begin();
it != npcs.end(); ++it)
if ((*it)->is_unused())
delete *it;
for (vector<Game_map *>::iterator it = maps.begin();
it != maps.end(); ++it)
(*it)->clear();
set_map(0); // Back to main map.
Monster_actor::delete_all(); // To be safe, del. any still around.