-
Notifications
You must be signed in to change notification settings - Fork 61
/
common.cpp
1404 lines (1274 loc) · 32.7 KB
/
common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// compile with make LOAD_FROM_FILES=1 DUMP=1
// to dump Wavs
#ifdef __LIBSDL__
#define NO_NETWORK
#endif
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <string.h>
#include <file/file_path.h>
#ifndef NO_NETWORK
extern "C"
{
#include <net/net_http.h>
#include <net/net_compat.h>
}
#endif
#include "mrboom.h"
#include "common.hpp"
#include "MrboomHelper.hpp"
#include "BotTree.hpp"
#include <string.h>
#include "images_patching_data.h"
#define NB_WAV 21
#define NB_VOICES 28
#define keyboardCodeOffset 32
#define keyboardReturnKey 28
#define keyboardExitKey 1
#define keyboardDataSize 8
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define keyboardExtraSelectStartKeysSize 2
#define offsetExtraKeys keyboardDataSize *nb_dyna + keyboardCodeOffset
#pragma GCC diagnostic ignored "-Woverlength-strings"
#pragma GCC diagnostic ignored "-Warray-bounds"
#define NB_CHIPTUNES 11
#ifdef __LIBSDL__
#define UNZIP_DATA
#endif
#ifdef LOAD_FROM_FILES
#include <streams/file_stream.h>
#define UNZIP_DATA
#endif
#ifdef UNZIP_DATA
#include <minizip/unzip.h>
static char romPath[PATH_MAX_LENGTH];
static char dataPath[PATH_MAX_LENGTH];
static char extractPath[PATH_MAX_LENGTH];
#endif
#ifdef __LIBRETRO__
#include <audio/audio_mixer.h>
#include <audio/conversion/float_to_s16.h>
static float *fbuf = NULL;
static int16_t *ibuf = NULL;
static audio_mixer_sound_t *musics[NB_CHIPTUNES];
#ifndef LOAD_FROM_FILES
#include "retro_data.h"
#include "retro_music_data.h"
#endif
#include "retro.hpp"
#ifdef LOAD_FROM_FILES
#include <audio/audio_mix.h>
static audio_chunk_t *wave[NB_WAV];
#endif
static size_t frames_left[NB_WAV];
#ifndef INT16_MAX
#define INT16_MAX 0x7fff
#endif
#ifndef INT16_MIN
#define INT16_MIN (-INT16_MAX - 1)
#endif
#define CLAMP_I16(x) (x > INT16_MAX ? INT16_MAX : x < INT16_MIN ? INT16_MIN \
: x)
#endif
#ifndef PLATFORM
#define PLATFORM "unknown"
#endif
bool music = true;
static int musics_index = 0;
#ifndef PADDING_FALCON
#define PADDING_FALCON 0
#endif
const char *musics_filenames[NB_CHIPTUNES + PADDING_FALCON] = {
"DEADFEEL.XM", // Carter (for menu + replay)
"WTH6.MOD", // parsec
"CHIPTUNE.MOD", // 4-mat
"MATKAMIE.MOD", // heatbeat
"CHIPMUNK.MOD", // jester
"UNREEEAL.XM", // rez+kenet
"ANAR11.MOD", // 4-mat
"EXTERNAL.XM", // Quazar
"ESTRAYK.MOD", // Estrayk
"HAPPY.XM", // Ultrasyd
"ASPARTAME.MOD" // Maf
};
#ifdef __LIBSDL2__
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#endif
#ifdef __LIBSDL__
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#endif
#if defined(__LIBSDL2__) || defined(__LIBSDL__)
int sdl2_fx_volume = DEFAULT_SDL2_FX_VOLUME;
static Mix_Chunk *wave[NB_WAV];
static Mix_Music *musics[NB_CHIPTUNES];
#define DEFAULT_VOLUME MIX_MAX_VOLUME / 2
#define MATKAMIE_VOLUME MIX_MAX_VOLUME
#define LOWER_VOLUME MIX_MAX_VOLUME / 3
const int musics_volume[NB_CHIPTUNES] = {
DEFAULT_VOLUME,
MIX_MAX_VOLUME,
DEFAULT_VOLUME,
MATKAMIE_VOLUME,
LOWER_VOLUME,
DEFAULT_VOLUME,
LOWER_VOLUME,
DEFAULT_VOLUME,
LOWER_VOLUME,
DEFAULT_VOLUME,
MIX_MAX_VOLUME};
#endif
#ifdef UNZIP_DATA
#ifdef __LIBSDL__
#include "sdl/sdl_data.h"
#else
#include "sdl2/sdl2_data.h"
#endif
#endif
static int ignoreForAbit[NB_WAV];
static int ignoreForAbitFlag[NB_WAV];
int traceMask = DEFAULT_TRACE_MAX;
static bool pic_timeExit = false; // hack in case input is too early in the intro pic
#ifdef __LIBRETRO__
#include <libretro.h>
int16_t *frame_sample_buf;
uint32_t num_samples_per_frame;
retro_audio_sample_batch_t audio_batch_cb;
#endif
bool audio = true;
bool cheatMode = false;
static bool fxTraces = false;
BotTree *tree[nb_dyna];
#ifdef DEBUG
int walkingToCell[nb_dyna];
#endif
#ifdef UNZIP_DATA
int rom_unzip(const char *path, const char *extraction_directory)
{
#ifndef __LIBSDL__
path_mkdir(extraction_directory);
#endif
unzFile *zipfile = (unzFile *)unzOpen(path);
if (zipfile == NULL)
{
log_error("<%s> not found\n", path);
return (-1);
}
unz_global_info global_info;
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
{
log_error("could not read file global info\n");
unzClose(zipfile);
return (-1);
}
char read_buffer[8192];
uLong i;
for (i = 0; i < global_info.number_entry; ++i)
{
unz_file_info file_info;
char filename[PATH_MAX_LENGTH];
if (unzGetCurrentFileInfo(zipfile, &file_info, filename, PATH_MAX_LENGTH,
NULL, 0, NULL, 0) != UNZ_OK)
{
log_error("could not read file info\n");
unzClose(zipfile);
return (-1);
}
const size_t filename_length = strlen(filename);
#ifndef __LIBSDL__
if (filename[filename_length - 1] == '/')
{
log_debug("dir:%s\n", filename);
char abs_path[PATH_MAX_LENGTH];
fill_pathname_join(abs_path,
extraction_directory, filename, sizeof(abs_path));
path_mkdir(abs_path);
}
else
#endif
{
log_debug("file:%s\n", filename);
if (unzOpenCurrentFile(zipfile) != UNZ_OK)
{
log_error("could not open file\n");
unzClose(zipfile);
return (-1);
}
#ifndef __LIBSDL__
char abs_path[PATH_MAX_LENGTH];
fill_pathname_join(abs_path,
extraction_directory, filename, sizeof(abs_path));
FILE *out = fopen(abs_path, "wb");
#else
FILE *out = fopen(filename, "wb");
#endif
if (out == NULL)
{
log_error("could not open destination file\n");
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return (-1);
}
int error = UNZ_OK;
do
{
error = unzReadCurrentFile(zipfile, read_buffer, 8192);
if (error < 0)
{
log_error("error %d\n", error);
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return (-1);
}
if (error > 0)
{
fwrite(read_buffer, error, 1, out);
}
} while (error > 0);
fclose(out);
}
unzCloseCurrentFile(zipfile);
if (i + 1 < global_info.number_entry)
{
if (unzGoToNextFile(zipfile) != UNZ_OK)
{
log_error("cound not read next file\n");
unzClose(zipfile);
return (-1);
}
}
}
unzClose(zipfile);
#ifndef __LIBSDL__
unlink(path);
#endif
return (0);
}
#endif
bool mrboom_debug_state_failed()
{
static db *saveState = NULL;
unsigned int i = 0;
bool failed = false;
if (saveState == NULL)
{
saveState = (uint8_t *)calloc(SIZE_RO_SEGMENT, 1);
memcpy(saveState, &m.FIRST_RO_VARIABLE, SIZE_RO_SEGMENT);
}
else
{
db *currentMem = &m.FIRST_RO_VARIABLE;
for (i = 0; i < SIZE_RO_SEGMENT; i++)
{
if (saveState[i] != currentMem[i])
{
log_error("RO variable changed at %x\n", i + (unsigned int)offsetof(struct Mem, FIRST_RO_VARIABLE));
memcpy(saveState, &m.FIRST_RO_VARIABLE, SIZE_RO_SEGMENT);
failed = true;
}
}
}
return (failed);
}
#ifndef LOAD_FROM_FILES
static unsigned short crc16(const unsigned char *data_p, int length)
{
unsigned char x;
unsigned short crc = 0xFFFF;
while (length--)
{
x = crc >> 8 ^ *data_p++;
x ^= x >> 4;
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x << 5)) ^ ((unsigned short)x);
}
return (crc);
}
#endif
#ifdef DUMP_HEAP
void dump_heap()
{
FILE *file;
char path[PATH_MAX_LENGTH];
sprintf(path, "/tmp/mrboom.heap");
printf("fopen %s\n", path);
file = fopen(path, "w");
fprintf(file, "#ifndef LOAD_FROM_FILES\n{\n");
int bytesDumped = 0;
while (bytesDumped < HEAP_SIZE)
{
if (bytesDumped % 16 == 0)
{
fprintf(file, " ");
}
fprintf(file, "0x%02X,", m.heap[bytesDumped]);
bytesDumped++;
if (bytesDumped % 16 == 0)
{
fprintf(file, "\n");
}
}
fprintf(file, "},\n#else\n{0},\n#endif\n");
fclose(file);
}
#endif
#ifdef DUMP_GFX
void write_gfx(char *name, int addr)
{
FILE *fp = fopen(name, "wb");
fwrite(m.heap + addr, 320 * 200, 1, fp);
fclose(fp);
}
void dump_gfx()
{
write_gfx("/tmp/game1.pic", 0x000000);
// fa00 = blank
write_gfx("/tmp/game2.pic", 0x01f400);
write_gfx("/tmp/game3.pic", 0x02ee00);
write_gfx("/tmp/menu.pic", 0x03e800);
write_gfx("/tmp/draw.pic", 0x04e200);
write_gfx("/tmp/draw2.pic", 0x05dc00);
write_gfx("/tmp/med.pic", 0x06d600);
write_gfx("/tmp/med3.pic", 0x07d000);
write_gfx("/tmp/sprite.pic", 0x08ca00);
write_gfx("/tmp/sprite3.pic", 0x09c400);
write_gfx("/tmp/vic1.pic", 0x0abe00);
write_gfx("/tmp/vic2.pic", 0x0bb800);
write_gfx("/tmp/vic3.pic", 0x0cb200);
write_gfx("/tmp/vic4.pic", 0x0dac00);
write_gfx("/tmp/pic.pic", 0x0ea600);
write_gfx("/tmp/neige2.pic", 0x0fa000);
write_gfx("/tmp/neige1.pic", 0x109a00);
// 119400 = blank
write_gfx("/tmp/micro.pic", 0x124800);
write_gfx("/tmp/nuage1.pic", 0x134200);
write_gfx("/tmp/nuage2.pic", 0x143c00);
write_gfx("/tmp/foret.pic", 0x153600);
write_gfx("/tmp/feuille.pic", 0x163000);
write_gfx("/tmp/neige3.pic", 0x172a00);
write_gfx("/tmp/pause.pic", 0x182400);
write_gfx("/tmp/medc.pic", 0x191e00);
write_gfx("/tmp/medg.pic", 0x1a1800);
write_gfx("/tmp/crayon.pic", 0x1b1200);
write_gfx("/tmp/crayon2.pic", 0x1c0c00);
write_gfx("/tmp/sprite2.pic", 0x1d0600);
// 1e0000 = blank
write_gfx("/tmp/neige1.mrb", 0x1efa00);
write_gfx("/tmp/micro1.mrb", 0x1ff400);
write_gfx("/tmp/jungle1.mrb", 0x20ee00);
write_gfx("/tmp/rose1.mrb", 0x21e800);
write_gfx("/tmp/fete1.mrb", 0x22e200);
write_gfx("/tmp/nuage1.mrb", 0x23dc00);
write_gfx("/tmp/lapin1.pic", 0x24d600);
write_gfx("/tmp/mort.pic", 0x25d000);
write_gfx("/tmp/lapin2.pic", 0x26ca00);
write_gfx("/tmp/lapin3.pic", 0x27c400);
write_gfx("/tmp/lapin4.pic", 0x28be00);
write_gfx("/tmp/foot.pic", 0x29b800);
write_gfx("/tmp/foot1.mrb", 0x2ab200);
write_gfx("/tmp/foot2.mrb", 0x2bac00);
write_gfx("/tmp/fete2.mrb", 0x2ca600);
write_gfx("/tmp/neige2.mrb", 0x2da000);
write_gfx("/tmp/rose2.pic", 0x2e9a00);
write_gfx("/tmp/jungle2.mrb", 0x2f9400);
write_gfx("/tmp/micro2.mrb", 0x308e00);
write_gfx("/tmp/nuage2.mrb", 0x318800);
write_gfx("/tmp/mrfond.pic", 0x328200);
write_gfx("/tmp/soucoupe.pic", 0x337c00);
write_gfx("/tmp/soccer.pic", 0x347600);
write_gfx("/tmp/footanim.pic", 0x357000);
write_gfx("/tmp/lune1.mrb", 0x366a00);
write_gfx("/tmp/lune2.mrb", 0x376400);
// 385e00 = blank
}
#endif
bool mrboom_load()
{
#ifdef FALCON
audio = 1;
music = false;
#endif
#ifdef UNZIP_DATA
char tmpDir[PATH_MAX_LENGTH];
#ifndef __LIBSDL__
snprintf(tmpDir, sizeof(tmpDir), "%s", "/tmp");
#ifndef __APPLE__
if (getenv("HOME") != NULL)
{
snprintf(tmpDir, sizeof(tmpDir), "%s", getenv("HOME"));
}
#endif
if (getenv("TMP") != NULL)
{
snprintf(tmpDir, sizeof(tmpDir), "%s", getenv("TMP"));
}
if (getenv("TEMP") != NULL)
{
snprintf(tmpDir, sizeof(tmpDir), "%s", getenv("TEMP"));
}
snprintf(romPath, sizeof(romPath), "%s/mrboom.rom", tmpDir);
snprintf(extractPath, sizeof(extractPath), "%s/mrboom", tmpDir);
// log_debug("romPath: %s\n", romPath);
if (filestream_write_file(romPath, dataRom, sizeof(dataRom)) == false)
{
log_error("Error writing %s\n", romPath);
return (false);
}
#else
sprintf(romPath, "C:\\ROM.DAT");
sprintf(extractPath, "");
FILE *fp;
fp = fopen(romPath, "wb");
fwrite(dataRom, 1, sizeof(dataRom), fp);
fclose(fp);
#endif
log_debug("romPath: %s\n", romPath);
log_debug("extractPath: %s\n", extractPath);
rom_unzip(romPath, extractPath);
unlink(romPath);
#ifdef LOAD_FROM_FILES
m.path = strdup(extractPath);
#endif
char filePath[PATH_MAX_LENGTH];
#endif
for (int i = 0; i < NB_WAV; i++)
{
#ifdef UNZIP_DATA
#ifdef __LIBSDL__
snprintf(filePath, sizeof(filePath), "%d.WAV", i);
#else
snprintf(filePath, sizeof(filePath), "%s/%d.WAV", extractPath, i);
#endif
#ifdef __LIBRETRO__
wave[i] = audio_mix_load_wav_file(&filePath[0], SAMPLE_RATE);
#endif
#if defined __LIBSDL2__ || defined(__LIBSDL__)
if (audio)
{
wave[i] = Mix_LoadWAV(filePath);
if (wave[i] == NULL)
{
log_error("Couldn't load %s\n", filePath);
// TOFIX exit(1);
}
else
{
Mix_VolumeChunk(wave[i], MIX_MAX_VOLUME * sdl2_fx_volume / 10);
}
if (wave[i] == NULL)
{
log_error("cant load %s\n", filePath);
}
}
unlink(filePath);
#endif
#endif
ignoreForAbit[i] = 0;
ignoreForAbitFlag[i] = 5;
}
#if defined(__LIBSDL2__) || defined(__LIBSDL__)
for (int i = 0; i < NB_CHIPTUNES; i++)
{
#ifdef __LIBSDL__
snprintf(filePath, sizeof(filePath), "%s", musics_filenames[i]);
#else
snprintf(filePath, sizeof(filePath), "%s/%s", extractPath, musics_filenames[i]);
#endif
if (audio)
{
musics[i] = Mix_LoadMUS(filePath);
if (!musics[i])
{
log_error("Mix_LoadMUS(\"%s\"): %s: please check SDL2_mixer is compiled --with-libmodplug\n", musics_filenames[i], Mix_GetError());
// return(false);
}
}
unlink(filePath);
}
#endif
#ifdef __LIBRETRO__
#ifdef LOAD_FROM_FILES
for (int i = 0; i < NB_CHIPTUNES; i++)
{
sprintf(filePath, "%s/%s", extractPath, musics_filenames[i]);
int64_t len = 0;
void *buf = NULL;
if (!filestream_read_file(filePath, &buf, &len))
{
log_error("Could not load %s\n", filePath);
musics[i] = NULL;
}
else
{
musics[i] = audio_mixer_load_mod(buf, len);
}
}
#else
musics[0] = audio_mixer_load_mod(SOUND_DEADFEEL_XM, SOUND_DEADFEEL_XM_len);
musics[1] = audio_mixer_load_mod(SOUND_WTH6_MOD, SOUND_WTH6_MOD_len);
musics[2] = audio_mixer_load_mod(SOUND_CHIPTUNE_MOD, SOUND_CHIPTUNE_MOD_len);
musics[3] = audio_mixer_load_mod(SOUND_MATKAMIE_MOD, SOUND_MATKAMIE_MOD_len);
musics[4] = audio_mixer_load_mod(SOUND_CHIPMUNK_MOD, SOUND_CHIPMUNK_MOD_len);
musics[5] = audio_mixer_load_mod(SOUND_UNREEEAL_XM, SOUND_UNREEEAL_XM_len);
musics[6] = audio_mixer_load_mod(SOUND_ANAR11_MOD, SOUND_ANAR11_MOD_len);
musics[7] = audio_mixer_load_mod(SOUND_EXTERNAL_XM, SOUND_EXTERNAL_XM_len);
musics[8] = audio_mixer_load_mod(SOUND_ESTRAYK_MOD, SOUND_ESTRAYK_MOD_len);
musics[9] = audio_mixer_load_mod(SOUND_HAPPY_XM, SOUND_HAPPY_XM_len);
#endif
#endif
return (true);
}
bool mrboom_init()
{
asm2C_init();
//
if (m.isLittle)
{
m.isbigendian = 0;
}
else
{
m.isbigendian = 1;
}
m.differentesply2 = 4; // sky is for the first demo
strcpy((char *)&m.iff_file_name, "mrboom.dat");
m.taille_exe_gonfle = 0;
#ifdef __LIBRETRO__
fbuf = (float *)malloc(num_samples_per_frame * 2 * sizeof(float));
ibuf = (int16_t *)malloc(num_samples_per_frame * 2 * sizeof(int16_t));
#endif
#ifdef __LIBSDL2__
/* Initialize SDL. */
if (SDL_Init(SDL_INIT_AUDIO) < 0)
{
log_error("Error SDL_Init\n");
}
/* Initialize SDL_mixer */
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 512) == -1)
{
log_error("Error Mix_OpenAudio\n");
audio = false;
}
#endif
m.tected[20] = GAME_VERSION[0];
m.tected[21] = GAME_VERSION[1];
m.tected[22] = GAME_VERSION[2];
#ifndef LOAD_FROM_FILES
m.dataloaded = 1;
log_debug("Mrboom: Crc16 heap: %d\n", crc16(m.heap, HEAP_SIZE));
#else
m.dataloaded = 0;
#endif
#ifndef FALCON
mrboom_load();
#endif
ignoreForAbitFlag[0] = 30;
ignoreForAbitFlag[10] = 30; // Kangaroo jump
ignoreForAbitFlag[13] = 30;
ignoreForAbitFlag[14] = 30;
for (int i = 0; i < keyboardDataSize * nb_dyna; i++)
{
if (!((i + 1) % keyboardDataSize))
{
m.touches_[i] = -1;
}
else
{
m.touches_[i] = i + keyboardCodeOffset;
}
}
program();
#ifdef DUMP_HEAP
dump_heap();
#endif
#ifdef DUMP_GFX
dump_gfx();
#endif
m.nosetjmp = 1; // will go to menu, except if state loaded after
#ifdef LOAD_FROM_FILES
snprintf(dataPath, sizeof(dataPath), "%s/mrboom.dat", extractPath);
log_debug("dataPath = %s \n", dataPath);
unlink(dataPath);
log_debug("extractPath = %s \n", extractPath);
rmdir(extractPath);
#endif
#ifdef DEBUG
asm2C_printOffsets(offsetof(struct Mem, FIRST_RW_VARIABLE));
#endif
for (int i = 0; i < nb_dyna; i++)
{
tree[i] = new BotTree(i);
}
#ifndef NO_NETWORK
network_init();
#endif
return (true);
}
void mrboom_deinit()
{
#ifdef __LIBRETRO__
for (int i = 0; i < NB_CHIPTUNES; i++)
{
#ifdef LOAD_FROM_FILES
audio_mixer_destroy(musics[i]);
#else
free(musics[i]);
#endif
}
#endif
#ifdef LOAD_FROM_FILES
/* free WAV */
for (int i = 0; i < NB_WAV; i++)
{
#ifdef __LIBRETRO__
audio_mix_free_chunk(wave[i]);
#endif
#ifdef __LIBSDL2__
Mix_FreeChunk(wave[i]);
#endif
}
#endif
#ifdef __LIBRETRO__
free(fbuf);
free(ibuf);
audio_mixer_done();
#endif
}
static void mrboom_api()
{
#ifndef NO_NETWORK
static struct http_connection_t *conn = NULL;
static struct http_t *http = NULL;
static int api_state = 0;
static int say_hello = 1;
if ((!say_hello) && (!api_state))
{
return;
}
switch (api_state)
{
case 0:
{
char body[1024];
#ifdef __LIBRETRO__
sprintf(body, "{\n\"platform\":\"");
#else
snprintf(body, sizeof(body), "{\n\"platform\":\"");
#endif
strcat(body, PLATFORM);
#ifdef __LIBSDL2__
strcat(body, "\",\n\"version\":\"SDL2 ");
#else
strcat(body, "\",\n\"version\":\"libretro ");
#endif
strcat(body, GAME_VERSION);
strcat(body, GIT_VERSION);
strcat(body, "\"\n}\n");
log_debug("body:%s\n",body);
api_state = 1;
say_hello = 0;
#ifdef DEBUG
conn = net_http_connection_new("http://localhost:4004/hello", "POST", body);
#else
conn = net_http_connection_new("http://api.mumblecore.org/hello", "POST", body);
#endif
break;
}
case 1:
if (net_http_connection_iterate(conn))
{
if (net_http_connection_done(conn))
{
api_state = 2;
http = net_http_new(conn);
}
else
{
net_http_connection_free(conn);
conn = NULL;
api_state = 0;
}
}
break;
case 2:
if (net_http_update(http, NULL, NULL))
{
net_http_connection_free(conn);
conn = NULL;
net_http_delete(http);
http = NULL;
api_state = 0;
}
break;
}
#endif
}
#if defined __LIBSDL2__ || __LIBSDL__
#define play(b) Mix_PlayChannel(-1, wave[b], 0)
#else
#ifdef LOAD_FROM_FILES
#define play(b) \
if (wave[b] != NULL) \
{ \
frames_left[b] = audio_mix_get_chunk_num_samples(wave[b]); \
}
#else
#define play(b) \
if (wave[b].samples != NULL) \
{ \
frames_left[b] = wave[b].num_samples; \
}
#endif
#endif
#define fxSound(a, b) \
static bool a##b = false; \
if (a() && !a##b) \
{ \
a##b = true; \
play(b); \
if (fxTraces) \
{ \
log_debug("fxSound " #a "\n"); \
} \
} \
a##b = a();
void mrboom_sound(void)
{
if (!audio)
{
return;
}
fxSound(isDrawGame, 16)
fxSound(won, 17)
fxSound(isApocalypseSoon, 18)
fxSound(isGamePaused, 19)
fxSound(isGameUnPaused, 5)
fxSound(playerGotDisease, 20)
#ifdef __LIBRETRO__
static audio_mixer_voice_t *voice = NULL;
static bool mixer_init = false;
if (mixer_init == false)
{
audio_mixer_init(SAMPLE_RATE);
mixer_init = true;
}
#endif
static int last_voice = 0;
for (int i = 0; i < NB_WAV; i++)
{
if (ignoreForAbit[i])
{
ignoreForAbit[i]--;
}
}
#ifdef DUMP
static bool play_once = true;
if (play_once)
{
play(16);
play(17);
play(18);
play(20);
play_once = false;
}
#endif
while (m.last_voice != (unsigned)last_voice)
{
db a = *(((db *)&m.blow_what2[last_voice / 2]));
db a1 = a & 0xf;
if (fxTraces)
{
log_debug("blow what: sample = %d / panning %d, note: %d ignoreForAbit[%d]\n", a1, (db)a >> 4, (db)(*(((db *)&m.blow_what2[last_voice / 2]) + 1)), ignoreForAbit[a1]);
}
last_voice = (last_voice + 2) % NB_VOICES;
#if defined LOAD_FROM_FILES
if ((a1 >= 0) && (a1 < NB_WAV) && (wave[a1] != NULL))
#else
if ((a1 >= 0) && (a1 < NB_WAV) && (wave[a1].samples != NULL))
#endif
{
bool dontPlay = 0;
if (ignoreForAbit[a1])
{
if (fxTraces)
{
log_debug("Ignore sample id %d\n", a1);
}
dontPlay = 1;
}
if (dontPlay == 0)
{
#ifdef __LIBRETRO__
#ifdef LOAD_FROM_FILES
frames_left[a1] = audio_mix_get_chunk_num_samples(wave[a1]);
#else
frames_left[a1] = wave[a1].num_samples;
#endif
#endif
#if defined __LIBSDL2__ || __LIBSDL__
if (Mix_PlayChannel(-1, wave[a1], 0) == -1)
{
if (fxTraces)
{
log_error("Error playing sample id %d.<%s> Mix_AllocateChannels=%d\n", a1, Mix_GetError(), Mix_AllocateChannels(-1));
}
}
#endif
ignoreForAbit[a1] = ignoreForAbitFlag[a1];
}
}
else
{
log_error("Wrong sample id %d or NULL.\n", a1);
}
}
if (music)
{
static int currentLevel = -2;
#ifdef __LIBRETRO__
if (voice)
{
audio_mixer_voice_set_volume(voice, libretro_music_volume);
}
#endif
if (level() != currentLevel)
{
int index = 0; // default menu song
currentLevel = level();
if (currentLevel == -1)
{
if (isXmasPeriod())
{
index = 1;
}
}
else
{
musics_index = (musics_index + 1) % (NB_CHIPTUNES);
if (musics_index < 2)
{
musics_index = 2;
}
index = musics_index;
}
#if defined __LIBSDL2__ || __LIBSDL__
Mix_VolumeMusic(musics_volume[index]);
log_debug("Playing %s volume:%d\n", musics_filenames[index], Mix_VolumeMusic(-1));
if (Mix_PlayMusic(musics[index], -1) == -1)
{
log_error("error playing music %d\n", musics[0]);
}
#else
if (voice)
{
audio_mixer_stop(voice);
}
voice = audio_mixer_play(musics[index], true, libretro_music_volume, NULL, RESAMPLER_QUALITY_DONTCARE, NULL);
#endif
}
}
}
#ifdef __LIBRETRO__
void stop_cb(audio_mixer_sound_t *sound, unsigned reason)
{
}
#endif
void mrboom_reset_special_keys()
{
db *keys = m.total_t;
for (int i = 0; i < nb_dyna; i++)
{
int pointeurSelect = 64 + 5 + i * 7;
*(keys + pointeurSelect) = 0;
}
*(keys + 8 * 7) = 0;
*(keys + 8 * 7 + 1) = 0;
*(keys + 8 * 7 + 2) = 0; // une_touche_a_telle_ete_pressee
if ((pic_timeExit) && (m.pic_time))
{
*(keys + 8 * 7 + 2) = 1;
}
}
#ifdef __LIBSDL__
int lastDirection[nb_dyna];
void mrboom_autopilot_1_button_joysticks(int player)
{
int input = getInputForPlayer(player);
int x = xPlayer(player);
int addX = 0;
int y = yPlayer(player);
int addY = 0;
mrboom_update_input(button_x, input, 0, false); // also jump 1st player
if (isInMiddleOfCell(player))
{
switch (lastDirection[input])
{
case button_down:
addY = 1;