aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/adrift/scinterf.cpp
blob: cc8e1235d720e4b7558fb69190670700efab1be9 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/adrift/adrift.h"
#include "glk/adrift/scprotos.h"
#include "glk/adrift/scgamest.h"
#include "common/file.h"
#include "common/system.h"
#include "common/savefile.h"

namespace Glk {
namespace Adrift {

/* Assorted definitions and constants. */
static const sc_char NEWLINE = '\n';
static const sc_char CARRIAGE_RETURN = '\r';
static const sc_char NUL = '\0';

/* Global tracing flags. */
static sc_uint if_trace_flags = 0;


/*
 * if_initialize()
 *
 * First-time runtime checks for the overall interpreter.  This function
 * tries to ensure correct compile options.
 */
static void
if_initialize (void)
{
  static sc_bool initialized = FALSE;

  /* Only do checks on the first call. */
  if (!initialized)
    {
      /* Make a few quick checks on types and type sizes. */
      if (sizeof (sc_byte) != 1 || sizeof (sc_char) != 1)
        {
          sc_error ("if_initialize: sizeof sc_byte or sc_char"
                    " is not 1, check compile options\n");
        }
      else if (sizeof (sc_uint) < 4 || sizeof (sc_int) < 4)
        {
          sc_error ("if_initialize: sizeof sc_uint or sc_int"
                    " is not at least 4, check compile options\n");
        }
      else if (sizeof (sc_uint) > 8 || sizeof (sc_int) > 8)
        {
          sc_error ("if_initialize: sizeof sc_uint or sc_int"
                    " is more than 8, check compile options\n");
        }
      else if (!((sc_uint) -1 > 0))
        {
          sc_error ("if_initialize: sc_uint appears not to be unsigned,"
                    " check compile options\n");
        }

      initialized = TRUE;
    }
}


/*
 * if_bool()
 * sc_set_trace_flags()
 * if_get_trace_flag()
 *
 * Set and retrieve tracing flags.  Setting new values propagates the new
 * tracing setting to all modules that support it.
 */
static sc_bool
if_bool (sc_uint flag)
{
  return flag ? TRUE : FALSE;
}

void
sc_set_trace_flags (sc_uint trace_flags)
{
  if_initialize ();

  /* Save the value for queries. */
  if_trace_flags = trace_flags;

  /* Propagate tracing to modules that support it. */
  parse_debug_trace (if_bool (trace_flags & SC_TRACE_PARSE));
  prop_debug_trace (if_bool (trace_flags & SC_TRACE_PROPERTIES));
  var_debug_trace (if_bool (trace_flags & SC_TRACE_VARIABLES));
  uip_debug_trace (if_bool (trace_flags & SC_TRACE_PARSER));
  lib_debug_trace (if_bool (trace_flags & SC_TRACE_LIBRARY));
  evt_debug_trace (if_bool (trace_flags & SC_TRACE_EVENTS));
  npc_debug_trace (if_bool (trace_flags & SC_TRACE_NPCS));
  obj_debug_trace (if_bool (trace_flags & SC_TRACE_OBJECTS));
  task_debug_trace (if_bool (trace_flags & SC_TRACE_TASKS));
  restr_debug_trace (if_bool (trace_flags & SC_TRACE_TASKS));
  pf_debug_trace (if_bool (trace_flags & SC_TRACE_PRINTFILTER));
}

sc_bool
if_get_trace_flag (sc_uint bitmask)
{
  return if_bool (if_trace_flags & bitmask);
}


/*
 * if_print_string_common()
 * if_print_string()
 * if_print_debug()
 * if_print_character_common()
 * if_print_character()
 * if_print_debug_character()
 * if_print_tag()
 *
 * Call OS-specific print function for the given arguments.
 */
static void
if_print_string_common (const sc_char *string,
                        void (*print_string_function) (const sc_char *))
{
  assert (string);

  if (string[0] != NUL)
    print_string_function (string);
}

void
if_print_string (const sc_char *string)
{
  if_print_string_common (string, os_print_string);
}

void
if_print_debug (const sc_char *string)
{
  if_print_string_common (string, os_print_string_debug);
}

static void
if_print_character_common (sc_char character,
                           void (*print_string_function) (const sc_char *))
{
  if (character != NUL)
    {
      sc_char buffer[2];

      buffer[0] = character;
      buffer[1] = NUL;
      print_string_function (buffer);
    }
}

void
if_print_character (sc_char character)
{
  if_print_character_common (character, os_print_string);
}

void
if_print_debug_character (sc_char character)
{
  if_print_character_common (character, os_print_string_debug);
}

void
if_print_tag (sc_int tag, const sc_char *arg)
{
  assert (arg);

  os_print_tag (tag, arg);
}


/*
 * if_read_line_common()
 * if_read_line()
 * if_read_debug()
 *
 * Call OS-specific line read function.  Clean up any read data a little
 * before returning it to the caller.
 */
static void
if_read_line_common (sc_char *buffer, sc_int length,
                     sc_bool (*read_line_function) (sc_char *, sc_int))
{
  sc_bool is_line_available;
  sc_int last;
  assert (buffer && length > 0);

  /* Loop until valid player input is available. */
  do
    {
      /* Space first with a blank line, and clear the buffer. */
      if_print_character ('\n');
      memset (buffer, NUL, length);

      is_line_available = read_line_function (buffer, length);
	  if (g_vm->shouldQuit())
		  return;
    }
  while (!is_line_available);

  /* Drop any trailing newline/return. */
  last = strlen (buffer) - 1;
  while (last >= 0
         && (buffer[last] == CARRIAGE_RETURN || buffer[last] == NEWLINE))
    buffer[last--] = NUL;
}

void
if_read_line (sc_char *buffer, sc_int length)
{
  if_read_line_common (buffer, length, os_read_line);
}

void
if_read_debug (sc_char *buffer, sc_int length)
{
  if_read_line_common (buffer, length, os_read_line_debug);
}


/*
 * if_confirm()
 *
 * Call OS-specific confirm function.
 */
sc_bool
if_confirm (sc_int type)
{
  return os_confirm (type);
}


/*
 * if_open_saved_game()
 * if_write_saved_game()
 * if_read_saved_game()
 * if_close_saved_game()
 *
 * Call OS-specific functions for saving and restoring games.
 */
void *
if_open_saved_game (sc_bool is_save)
{
  return os_open_file (is_save);
}

void
if_write_saved_game (void *opaque, const sc_byte *buffer, sc_int length)
{
  assert (buffer);

  os_write_file (opaque, buffer, length);
}

sc_int
if_read_saved_game (void *opaque, sc_byte *buffer, sc_int length)
{
  assert (buffer);

  return os_read_file (opaque, buffer, length);
}

void
if_close_saved_game (void *opaque)
{
  os_close_file (opaque);
}


/*
 * if_display_hints()
 *
 * Call OS-specific hint display function.
 */
void
if_display_hints (sc_gameref_t game)
{
  assert (gs_is_game_valid (game));

  os_display_hints ((sc_game) game);
}


/*
 * if_update_sound()
 * if_update_graphic()
 *
 * Call OS-specific sound and graphic handler functions.
 */
void
if_update_sound (const sc_char *filename,
                 sc_int sound_offset, sc_int sound_length,
                 sc_bool is_looping)
{
  if (strlen (filename) > 0)
    os_play_sound (filename, sound_offset, sound_length, is_looping);
  else
    os_stop_sound ();
}

void
if_update_graphic (const sc_char *filename,
                   sc_int graphic_offset, sc_int graphic_length)
{
  os_show_graphic (filename, graphic_offset, graphic_length);
}


/*
 * sc_scare_version()
 * sc_scare_emulation()
 *
 * Return a version string and Adrift emulation level.
 */
const sc_char *
sc_scare_version (void)
{
  if_initialize ();
  return "SCARE " SCARE_VERSION SCARE_PATCH_LEVEL;
}

sc_int
sc_scare_emulation (void)
{
  if_initialize ();
  return SCARE_EMULATION;
}


/*
 * if_file_read_callback()
 * if_file_write_callback()
 *
 * Standard FILE* reader and writer callback for constructing callback-style
 * calls from filename and stream variants.
 */
static sc_int
if_file_read_callback (void *opaque, sc_byte *buffer, sc_int length) {
  Common::SeekableReadStream *stream = (Common::SeekableReadStream *)opaque;
  sc_int bytes;

  bytes = stream->read(buffer, length);
  if (stream->err())
    sc_error ("if_file_read_callback: warning: read error\n");

  return bytes;
}

static void if_file_write_callback (void *opaque, const sc_byte *buffer, sc_int length)
{
  Common::WriteStream *stream = (Common::WriteStream *) opaque;

  stream->write(buffer, length);
  if (stream->err())
    sc_error ("if_file_write_callback: warning: write error\n");
}


/*
 * sc_game_from_filename()
 * sc_game_from_stream()
 * sc_game_from_callback()
 *
 * Called by the OS-specific layer to create a run context.  The _filename()
 * and _stream() variants are adapters for run_create().
 */
sc_game
sc_game_from_filename (const sc_char *filename) {
  Common::File *stream;
  sc_game game;

  if_initialize ();
  if (!filename)
    {
      sc_error ("sc_game_from_filename: nullptr filename\n");
      return nullptr;
    }

  stream = new Common::File();
  if (!stream->open(filename)) {
		  delete stream;
      sc_error ("sc_game_from_filename: fopen error\n");
      return nullptr;
    }

  game = run_create (if_file_read_callback, stream);
  delete stream;

  return game;
}

sc_game sc_game_from_stream (Common::SeekableReadStream *stream) {
  if_initialize ();
  if (!stream)
    {
      sc_error ("sc_game_from_stream: nullptr stream\n");
      return nullptr;
    }

  return run_create (if_file_read_callback, stream);
}

sc_game
sc_game_from_callback (sc_int (*callback) (void *, sc_byte *, sc_int),
                       void *opaque)
{
  if_initialize ();
  if (!callback)
    {
      sc_error ("sc_game_from_callback: nullptr callback\n");
      return nullptr;
    }

  return run_create (callback, opaque);
}


/*
 * if_game_error()
 *
 * Common function to verify that the game passed in to functions below
 * is a valid game.  Returns TRUE on game error, FALSE if okay.
 */
static sc_bool
if_game_error (sc_gameref_t game, const sc_char *function_name)
{
  /* Check for invalid game -- null pointer or bad magic. */
  if (!gs_is_game_valid (game))
    {
      if (game)
        sc_error ("%s: invalid game\n", function_name);
      else
        sc_error ("%s: nullptr game\n", function_name);
      return TRUE;
    }

  /* No game error. */
  return FALSE;
}


/*
 * sc_interpret_game()
 * sc_restart_game()
 * sc_save_game()
 * sc_load_game()
 * sc_undo_game_turn()
 * sc_quit_game()
 *
 * Called by the OS-specific layer to run a game loaded into a run context,
 * and to quit the interpreter on demand, if required.  sc_quit_game()
 * is implemented as a longjmp(), so never returns to the caller --
 * instead, the program behaves as if sc_interpret_game() had returned.
 * sc_load_game() will longjmp() if the restore is successful (thus
 * behaving like sc_restart_game()), but will return if the game could not
 * be restored.  sc_undo_game_turn() behaves like sc_load_game().
 */
void
sc_interpret_game (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_interpret_game"))
    return;

  run_interpret (game_);
}

void
sc_restart_game (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_restart_game"))
    return;

  run_restart (game_);
}

sc_bool
sc_save_game (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_save_game"))
    return FALSE;

  return run_save_prompted (game_);
}

sc_bool
sc_load_game (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_load_game"))
    return FALSE;

  return run_restore_prompted (game_);
}

sc_bool
sc_undo_game_turn (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_undo_game_turn"))
    return FALSE;

  return run_undo (game_);
}

void
sc_quit_game (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_quit_game"))
    return;

  run_quit (game_);
}


/*
 * sc_save_game_to_filename()
 * sc_save_game_to_stream()
 * sc_save_game_to_callback()
 * sc_load_game_from_filename()
 * sc_load_game_from_stream()
 * sc_load_game_from_callback()
 *
 * Low level game saving and loading functions.  The normal sc_save_game()
 * and sc_load_game() functions act exactly as the "save" and "restore"
 * game commands, in that they prompt the user for a stream to write or read.
 * These alternative forms allow the caller to directly specify the data
 * streams.
 */
sc_bool
sc_save_game_to_filename (sc_game game, const sc_char *filename)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  Common::OutSaveFile *sf;

  if (if_game_error (game_, "sc_save_game_to_filename"))
    return FALSE;

  if (!filename)
    {
      sc_error ("sc_save_game_to_filename: nullptr filename\n");
      return FALSE;
    }

  sf = g_system->getSavefileManager()->openForSaving(filename);
  if (!sf) {
      sc_error ("sc_save_game_to_filename: fopen error\n");
      return FALSE;
    }

  run_save(game_, if_file_write_callback, sf);
  sf->finalize();
  delete sf;

  return TRUE;
}

void
sc_save_game_to_stream (sc_game game, Common::SeekableReadStream *stream)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_save_game_to_stream"))
    return;

  if (!stream)
    {
      sc_error ("sc_save_game_to_stream: nullptr stream\n");
      return;
    }

  run_save (game_, if_file_write_callback, stream);
}

void
sc_save_game_to_callback (sc_game game,
                          void (*callback) (void *, const sc_byte *, sc_int),
                          void *opaque)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_save_game_to_callback"))
    return;

  if (!callback)
    {
      sc_error ("sc_save_game_to_callback: nullptr callback\n");
      return;
    }

  run_save (game_, callback, opaque);
}

sc_bool
sc_load_game_from_filename (sc_game game, const sc_char *filename)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  Common::InSaveFile *sf;
  sc_bool status;

  if (if_game_error (game_, "sc_load_game_from_filename"))
    return FALSE;

  if (!filename)
    {
      sc_error ("sc_load_game_from_filename: nullptr filename\n");
      return FALSE;
    }

  sf = g_system->getSavefileManager()->openForLoading(filename);
  if (!sf)
    {
      sc_error ("sc_load_game_from_filename: fopen error\n");
      return FALSE;
    }

  status = run_restore (game_, if_file_read_callback, sf);
  delete sf;

  return status;
}

sc_bool
sc_load_game_from_stream (sc_game game, Common::SeekableReadStream *stream)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_load_game_from_stream"))
    return FALSE;

  if (!stream)
    {
      sc_error ("sc_load_game_from_stream: nullptr stream\n");
      return FALSE;
    }

  return run_restore (game_, if_file_read_callback, stream);
}

sc_bool
sc_load_game_from_callback (sc_game game,
                            sc_int (*callback) (void *, sc_byte *, sc_int),
                            void *opaque)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_load_game_from_callback"))
    return FALSE;

  if (!callback)
    {
      sc_error ("sc_load_game_from_callback: nullptr callback\n");
      return FALSE;
    }

  return run_restore (game_, callback, opaque);
}


/*
 * sc_free_game()
 *
 * Called by the OS-specific layer to free run context memory.
 */
void
sc_free_game (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_free_game"))
    return;

  run_destroy (game_);
}


/*
 * sc_is_game_running()
 * sc_get_game_name()
 * sc_get_game_author()
 * sc_get_game_compile_date()
 * sc_get_game_turns()
 * sc_get_game_score()
 * sc_get_game_max_score()
 * sc_get_game_room ()
 * sc_get_game_status_line ()
 * sc_get_game_preferred_font ()
 * sc_get_game_bold_room_names()
 * sc_get_game_verbose()
 * sc_get_game_notify_score_change()
 * sc_has_game_completed()
 * sc_is_game_undo_available()
 *
 * Return a few attributes of a game.
 */
sc_bool
sc_is_game_running (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_is_game_running"))
    return FALSE;

  return run_is_running (game_);
}

const sc_char *
sc_get_game_name (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_char *retval;

  if (if_game_error (game_, "sc_get_game_name"))
    return "[invalid game]";

  run_get_attributes (game_, &retval,
                      nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, nullptr);
  return retval;
}

const sc_char *
sc_get_game_author (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_char *retval;

  if (if_game_error (game_, "sc_get_game_author"))
    return "[invalid game]";

  run_get_attributes (game_, nullptr, &retval,
                      nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr);
  return retval;
}

const sc_char *
sc_get_game_compile_date (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_char *retval;

  if (if_game_error (game_, "sc_get_game_compile_date"))
    return "[invalid game]";

  run_get_attributes (game_, nullptr, nullptr, &retval,
                      nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  return retval;
}

sc_int
sc_get_game_turns (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_int retval;

  if (if_game_error (game_, "sc_get_game_turns"))
    return 0;

  run_get_attributes (game_, nullptr, nullptr, nullptr, &retval,
                      nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  return retval;
}

sc_int
sc_get_game_score (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_int retval;

  if (if_game_error (game_, "sc_get_game_score"))
    return 0;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, &retval,
                      nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  return retval;
}

sc_int
sc_get_game_max_score (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_int retval;

  if (if_game_error (game_, "sc_get_game_max_score"))
    return 0;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, &retval,
                      nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  return retval;
}

const sc_char *
sc_get_game_room (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_char *retval;

  if (if_game_error (game_, "sc_get_game_room"))
    return "[invalid game]";

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &retval,
                      nullptr, nullptr, nullptr, nullptr, nullptr);
  return retval;
}

const sc_char *
sc_get_game_status_line (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_char *retval;

  if (if_game_error (game_, "sc_get_game_status_line"))
    return "[invalid game]";

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      &retval, nullptr, nullptr, nullptr, nullptr);
  return retval;
}

const sc_char *
sc_get_game_preferred_font (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_char *retval;

  if (if_game_error (game_, "sc_get_game_preferred_font"))
    return "[invalid game]";

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, &retval, nullptr, nullptr, nullptr);
  return retval;
}

sc_bool
sc_get_game_bold_room_names (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_bool retval;

  if (if_game_error (game_, "sc_get_game_bold_room_names"))
    return FALSE;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, nullptr, &retval, nullptr, nullptr);
  return retval;
}

sc_bool
sc_get_game_verbose (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_bool retval;

  if (if_game_error (game_, "sc_get_game_verbose"))
    return FALSE;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, nullptr, nullptr, &retval, nullptr);
  return retval;
}

sc_bool
sc_get_game_notify_score_change (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_bool retval;

  if (if_game_error (game_, "sc_get_game_notify_score_change"))
    return FALSE;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, nullptr, nullptr, nullptr, &retval);
  return retval;
}

sc_bool
sc_has_game_completed (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_has_game_completed"))
    return FALSE;

  return run_has_completed (game_);
}

sc_bool
sc_is_game_undo_available (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_is_game_undo_available"))
    return FALSE;

  return run_is_undo_available (game_);
}


/*
 * sc_set_game_bold_room_names()
 * sc_set_game_verbose()
 * sc_set_game_notify_score_change()
 *
 * Set a few attributes of a game.
 */
void
sc_set_game_bold_room_names (sc_game game, sc_bool flag)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_bool bold, verbose, notify;

  if (if_game_error (game_, "sc_set_game_bold_room_names"))
    return;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, nullptr, &bold, &verbose, &notify);
  run_set_attributes (game_, flag, verbose, notify);
}

void
sc_set_game_verbose (sc_game game, sc_bool flag)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_bool bold, verbose, notify;

  if (if_game_error (game_, "sc_set_game_verbose"))
    return;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, nullptr, &bold, &verbose, &notify);
  run_set_attributes (game_, bold, flag, notify);
}

void
sc_set_game_notify_score_change (sc_game game, sc_bool flag)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  sc_bool bold, verbose, notify;

  if (if_game_error (game_, "sc_set_game_notify_score_change"))
    return;

  run_get_attributes (game_, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
                      nullptr, nullptr, &bold, &verbose, &notify);
  run_set_attributes (game_, bold, verbose, flag);
}


/*
 * sc_does_game_use_sounds()
 * sc_does_game_use_graphics()
 *
 * Indicate the game's use of resources.
 */
sc_bool
sc_does_game_use_sounds (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_does_game_use_sounds"))
    return FALSE;

  return res_has_sound (game_);
}

sc_bool
sc_does_game_use_graphics (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_does_game_use_graphics"))
    return FALSE;

  return res_has_graphics (game_);
}


/*
 * sc_get_first_game_hint()
 * sc_get_next_game_hint()
 * sc_get_game_hint_question()
 * sc_get_game_subtle_hint()
 * sc_get_game_sledgehammer_hint()
 *
 * Iterate currently available hints, and return strings for a hint.
 */
sc_game_hint
sc_get_first_game_hint (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_get_first_game_hint"))
    return nullptr;

  return run_hint_iterate (game_, nullptr);
}

sc_game_hint
sc_get_next_game_hint (sc_game game, sc_game_hint hint)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_hintref_t hint_ = (const sc_hintref_t)hint;

  if (if_game_error (game_, "sc_get_next_game_hint"))
    return nullptr;
  if (!hint_)
    {
      sc_error ("sc_get_next_game_hint: nullptr hint\n");
      return nullptr;
    }

  return run_hint_iterate (game_, hint_);
}

const sc_char *
sc_get_game_hint_question (sc_game game, sc_game_hint hint)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_hintref_t hint_ = (const sc_hintref_t)hint;

  if (if_game_error (game_, "sc_get_game_hint_question"))
    return nullptr;
  if (!hint_)
    {
      sc_error ("sc_get_game_hint_question: nullptr hint\n");
      return nullptr;
    }

  return run_get_hint_question (game_, hint_);
}

const sc_char *
sc_get_game_subtle_hint (sc_game game, sc_game_hint hint)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_hintref_t hint_ = (const sc_hintref_t)hint;

  if (if_game_error (game_, "sc_get_game_subtle_hint"))
    return nullptr;
  if (!hint_)
    {
      sc_error ("sc_get_game_subtle_hint: nullptr hint\n");
      return nullptr;
    }

  return run_get_subtle_hint (game_, hint_);
}

const sc_char *
sc_get_game_unsubtle_hint (sc_game game, sc_game_hint hint)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;
  const sc_hintref_t hint_ = (const sc_hintref_t)hint;

  if (if_game_error (game_, "sc_get_game_unsubtle_hint"))
    return nullptr;
  if (!hint_)
    {
      sc_error ("sc_get_game_unsubtle_hint: nullptr hint\n");
      return nullptr;
    }

  return run_get_unsubtle_hint (game_, hint_);
}


/*
 * sc_set_game_debugger_enabled()
 * sc_is_game_debugger_enabled()
 * sc_run_game_debugger_command()
 *
 * Enable, disable, and query game debugging, and run a single debug command.
 */
void
sc_set_game_debugger_enabled (sc_game game, sc_bool flag)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_set_game_debugger_enabled"))
    return;

  debug_set_enabled (game_, flag);
}

sc_bool
sc_get_game_debugger_enabled (sc_game game)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_get_game_debugger_enabled"))
    return FALSE;

  return debug_get_enabled (game_);
}

sc_bool
sc_run_game_debugger_command (sc_game game, const sc_char *debug_command)
{
  const sc_gameref_t game_ = (const sc_gameref_t)game;

  if (if_game_error (game_, "sc_run_game_debugger_command"))
    return FALSE;

  return debug_run_command (game_, debug_command);
}


/*
 * sc_set_locale()
 * sc_get_locale()
 *
 * Set the interpreter locale, and get the currently set locale.
 */
sc_bool
sc_set_locale (const sc_char *name)
{
  if (!name)
    {
      sc_error ("sc_set_locale: nullptr name\n");
      return FALSE;
    }

  return loc_set_locale (name);
}

const sc_char *
sc_get_locale (void)
{
  return loc_get_locale ();
}


/*
 * sc_set_portable_random()
 * sc_reseed_random_sequence()
 *
 * Turn portable random number generation on and off, and supply a new seed
 * for random number generators.
 */
void
sc_set_portable_random (sc_bool flag)
{
  if (flag)
    sc_set_congruential_random ();
  else
    sc_set_platform_random ();
}

void
sc_reseed_random_sequence (sc_uint new_seed)
{
  if (new_seed == 0)
    {
      sc_error ("sc_reseed_random_sequence: new_seed may not be 0\n");
      return;
    }

  sc_seed_random (new_seed);
}

} // End of namespace Adrift
} // End of namespace Glk