-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaws2c.c
executable file
·4438 lines (4111 loc) · 139 KB
/
aws2c.c
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
/*
AWS to C converter by Davide Bucci
AWS stands for Adventure Writing System and is a program developed by
Aristide Torrelli to write interactive fiction games. Here is a link to his
blog, in Italian language:
http://www.aristidetorrelli.it/aws3/AWS.html
The game developed with AWS is described by a compact file that contains the
vocabulary, the messages, the descriptions and all the logic needed for the
game.
Its structure of AWS is relatively simple yet powerful and I decided to write
this little converter that automagically generates C code that implements the
logic described by the game.
If you need an English description of AWS, I wrote this:
https://github.com/DarwinNE/aws2c/blob/master/AWS_description.md
The parser and some input/output functions are contained in an external file
called inout.c that can be customised to the target machine.
I tested this system with gcc and the generated files can be compiled with
any reasonably standard C compiler. I tested them with cc65 to target
old Commodore machines.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "aws_c.h"
#include "compress.h"
#define VERSION "1.9.6, September 2018 - April 2021"
#define AREYOUSURE "Are you sure? Type 'Y' and return if yes."
#define EXITRESTART "'E' and return to exit, anything else to restart."
/* TO DO
- Finish implementing the remaining actions, functions and decisions.
- Free the allocated memory before quitting the program.
- Test, test!
*/
#define BUFFERSIZE 16000
char buffer[BUFFERSIZE];
char function_res[BUFFERSIZE];
char *config_file=NULL;
#define TAB " "
room* world;
unsigned int rsize;
#define start_function() function_res[0]='\0';
typedef struct localc_t {
unsigned int room;
char *condition;
} localc;
unsigned int no_of_errors;
boolean verbose=false;
boolean convert_utf8=false;
boolean convert_accents=false;
boolean convert_accent_alt=false;
boolean compress_messages=false;
boolean compress_descriptions=false;
boolean use_6_directions=false;
boolean shortcuts=true;
boolean hardcoded_messages=false;
boolean add_clrscr=true;
boolean dont_care_size_weight=false;
boolean no_obj_long_desc=true;
boolean strip_empty_messages=false;
boolean no_header=false;
boolean strip_automatic_counters=false;
boolean compress5bit_dict=false;
boolean dont_use_light=false;
boolean compress_hash_dict=false;
boolean actor_as_byte=true;
boolean adjective_as_byte=true;
boolean adverb_as_byte=true;
boolean use_adverbs=false;
boolean use_adjectives=false;
boolean complete_shortcut=false;
boolean checked_noun1_greater_zero=false;
boolean dont_issue_message=false;
boolean no_header_description=false;
/* Some functions are included in the code only if necessary. Those flags
take care of them so they can be included in the code if they have been
used.
*/
boolean need_searchw=false;
boolean need_vov=false;
boolean need_vovn=false;
boolean need_non1=false;
boolean need_cvn=false;
boolean need_check_verb_actor=false;
boolean need_cv=false;
boolean need_hold=false;
boolean need_cvna=false;
boolean need_sendallroom=false;
boolean need_unwear=false;
boolean need_iscarrsome=false;
boolean need_iswearsome=false;
boolean need_checkexit=true;
boolean need_amsm=false;
boolean need_as=false;
boolean need_ar=false;
boolean need_ams=false;
boolean need_cpi=false;
boolean need_ok=false;
/* Statistics for optimizations.
*/
unsigned int number_of_jumps;
unsigned int number_of_drops;
/* Those are flags than in principle can be turned on and off to optimize if
a particular function should be defined check_position_marker_on a macro instead. This is useful
check_position_marker_on if it is called only once, the overhead of the function calling
mechanism can do more harm than good. Aws2c will determine
alone if those options must be turned on or off by checking the statistics.
*/
boolean jump_as_function=false;
boolean drop_as_function=true; // It must be true check_position_marker_on function returns a value.
typedef struct conv_t {
char *orig;
char *conv;
char accent;
char accent_alt;
} conv;
/* This is a minimal conversion that should work for the Italian language
(at least). It is used to the conversion between UTF-8 chars and standard
ASCII characters, plus the accents. */
#define CONVSIZE 26
char apostrophe[]={0xE2, 0x80, 0x99,0x0};
char ellips[]={0xE2, 0x80, 0xA6,0x0};
char quotel[]={0xE2, 0x80, 0x9C,0x0};
char quoter[]={0xE2, 0x80, 0x9D,0x0};
conv conversion[CONVSIZE] = {
{"à","a",'`','\''},
{"è","e",'`','\''},
{"ì","i",'`','\''},
{"ò","o",'`','\''},
{"ù","u",'`','\''},
{"á","a",'\'','\''},
{"é","e",'\'','\''},
{"í","i",'\'','\''},
{"ó","o",'\'','\''},
{"ô","o",'\0','\0'},
{"ú","u",'\'','\''},
{"À","A",'`','\''},
{"È","E",'`','\''},
{"Ì","I",'`','\''},
{"Ò","O",'`','\''},
{"Ù","U",'`','\''},
{"Á","A",'\'','\''},
{"É","E",'\'','\''},
{"Í","I",'\'','\''},
{"Ó","O",'\'','\''},
{"Ú","U",'\'','\''},
{"’","\'",'\0','\0'},
{apostrophe,"\'",'\0','\0'},
{quotel,"\'",'\0','\0'},
{quoter,"\'",'\0','\0'},
{ellips,"...",'\0','\0'}
};
/** Change and encode characters that may create troubles when output, such
check_position_marker_on ".
Exploits buffer.
*/
char *encodechar(char *input)
{
unsigned int i,j,k=0,t;
char c,v;
boolean byte3, found;
char notshown[4];
byte3=false;
for(i=0; (c=input[i])!='\0' && i<BUFFERSIZE-1;++i) {
if((c=='\"')&&(compress_messages==false)) {
buffer[k++]='\\';
} else if(c==-25) { // 0xE7
c=','; // Comma is translated in AWS files!
} else if(c=='^' && input[i+1]=='M') {
if(compress_messages==false) {
buffer[k++]='\\';
c='n';
} else {
c='\n';
}
++i;
} else if(c&'\x80' && convert_utf8==true) {
for(j=0; j<CONVSIZE;++j) {
found=true;
for(t=0;(v=conversion[j].orig[t])!='\0';++t) {
if(input[i+t]!=v) {
found=false;
break;
}
}
if(found==true){
if(convert_accents==true) {
for(t=0;(v=conversion[j].conv[t])!='\0';++t)
buffer[k++]=v;
if(conversion[j].accent!='\0') {
if(convert_accent_alt==true)
buffer[k++]=conversion[j].accent_alt;
else
buffer[k++]=conversion[j].accent;
}
} else {
for(t=0;(v=conversion[j].conv[t])!='\0';++t)
buffer[k++]=v;
}
i+=strlen(conversion[j].orig)-1;
goto cont;
}
}
if(j==CONVSIZE) {
if(byte3==false) {
notshown[0]=c;
notshown[1]=input[i+1];
notshown[2]='\0';
fprintf(stderr,
"WARNING: UTF-8 character %s (0x%X 0x%X)"
" has not been converted in \"%s\".\n",
notshown, (unsigned int) c, (unsigned int) input[i+1],
input);
} else {
notshown[0]='\x80';
notshown[1]=c;
notshown[2]=input[i+1];
notshown[3]='\0';
fprintf(stderr,
"WARNING: UTF-8 character %s (0x80 0x%X 0x%X)"
" has not been converted in \"%s\".\n",
notshown, (unsigned int) c, (unsigned int) input[i+1],
input);
}
}
}
buffer[k++]=c;
cont: t++;
}
buffer[k++]='\0';
return buffer;
}
/** Read the dictionary contained in the file. The number of words to be read
should have been already found.
@return a pointer to the allocated dictionary, or NULL if something bad
happened.
*/
word *read_dictionary(FILE *f, int size)
{
unsigned int cw;
fpos_t pos;
char *errorp="Could not allocate enough memory for the dictionary.\n";
if (size<=0)
return NULL;
word *dictionary = (word*)calloc(size, sizeof(word));
if (dictionary==NULL) {
printf("%s",errorp);
return NULL;
}
for(cw=0; cw<size;++cw) {
fscanf(f,"%80s", buffer);
dictionary[cw].w=malloc((strlen(buffer)+1)*sizeof(char));
if (dictionary[cw].w==NULL) {
free(dictionary);
printf("%s",errorp);
return NULL;
}
strcpy(dictionary[cw].w,buffer);
fscanf(f,"%d", &dictionary[cw].code);
fscanf(f,"%80s", buffer);
if(strcmp(buffer, "VERBO")==0) {
dictionary[cw].t=VERB;
} else if(strcmp(buffer, "AVVERBIO")==0) {
use_adverbs=true;
dictionary[cw].t=ADVERB;
if(dictionary[cw].code>255)
adverb_as_byte=false;
} else if(strcmp(buffer, "SEPARATORE")==0) {
dictionary[cw].t=SEPARATOR;
} else if(strcmp(buffer, "NOME")==0) {
dictionary[cw].t=NAME;
} else if(strcmp(buffer, "ATTORE")==0) {
dictionary[cw].t=ACTOR;
if(dictionary[cw].code>255)
actor_as_byte=false;
} else if(strcmp(buffer, "AGGETTIVO")==0) {
use_adjectives=true;
dictionary[cw].t=ADJECTIVE;
if(dictionary[cw].code>255)
adjective_as_byte=false;
} else {
fprintf(stderr,"Unknown word type: %s.\n",buffer);
free(dictionary);
return NULL;
}
}
return dictionary;
}
/** Get the dictionary size.
*/
unsigned int get_dict_size(FILE *f)
{
fpos_t pos;
unsigned int counter=0;
while(fscanf(f,"%80s",buffer)==1){
if(strcmp(buffer,"DIZIONARIO")==0) {
break;
}
}
fgetpos(f, &pos);
while(fscanf(f,"%80s",buffer)==1){
if(strcmp(buffer,"LOCAZIONI")==0) {
break;
}
++counter;
}
fsetpos(f, &pos);
return counter/3;
}
/** Load a line from a file, store it in the buffer and remove the newline.
*/
char *getlinep(FILE *f)
{
unsigned int sl;
if(fgets(buffer, BUFFERSIZE, f) == NULL) {
return NULL;
}
sl=strlen(buffer);
if(sl>=2 && buffer[sl - 2]=='\r') // This is needed for Windows
buffer[sl - 2] = '\0'; // style newline code (\r\n).
else if(sl>=1)
buffer[sl - 1] = '\0';
return buffer;
}
/** Get the number of the rooms in the game.
*/
int get_room_number(FILE *f)
{
fpos_t pos;
int counter=0;
while(getlinep(f)){
if(strcmp(buffer,"LOCAZIONI")==0) {
break;
}
}
fgetpos(f, &pos);
while (getlinep(f)) {
if(strcmp(buffer,"MESSAGGI")==0) {
break;
}
++counter;
}
fsetpos(f, &pos);
return counter/14;
}
/** Read all the "conditions" in the file.
*/
char **read_cond(FILE*f, int size)
{
int i;
char **cond;
char *errorp="Could not allocate enough memory for the conditions.\n";
if(size<=0)
return NULL;
if (verbose)
printf("\n");
cond = (char**)calloc(size, sizeof(char*));
if (cond==NULL) {
printf("%s",errorp);
return NULL;
}
for(i=0; i<size;++i) {
if(getlinep(f)==NULL) {
return NULL;
}
if (verbose)
printf("[%s]\n",buffer);
cond[i]=calloc(strlen(buffer)+1,sizeof(char));
if (cond[i]==NULL) {
printf("%s",errorp);
return NULL;
}
strcpy(cond[i],buffer);
}
return cond;
}
/** Read all the "local conditions" in the file.
*/
localc* read_local(FILE*f, int size)
{
unsigned int i,r;
localc* cond;
char *errorp="Could not allocate enough memory for the local conditions.\n";
if(size<=0)
return NULL;
cond = (localc*)calloc(size, sizeof(localc));
if (cond==NULL) {
printf("%s",errorp);
return NULL;
}
if(verbose)
printf("\n");
for(i=0; i<size;++i) {
getlinep(f);
if(sscanf(buffer,"%d",&r)!=1) {
printf("\nCould not read the room number! Read [%s]\n",buffer);
return NULL;
}
cond[i].room=r;
getlinep(f);
cond[i].condition=calloc(strlen(buffer)+1,sizeof(char));
if (cond[i].condition==NULL) {
printf("%s",errorp);
return NULL;
}
strcpy(cond[i].condition,buffer);
if(verbose)
printf("Local room %d [%s]\n", cond[i].room, cond[i].condition);
}
return cond;
}
/** Get the rooms in the game
*/
room* read_rooms(FILE *f, int size)
{
char *errorp="Could not allocate enough memory for the room description.\n";
int i,j;
room *world = (room*)calloc(size, sizeof(room));
if (world==NULL) {
printf("%s",errorp);
return NULL;
}
for(i=0; i<size;++i) {
getlinep(f); // Read line 1
sscanf(buffer, "%d",&(world[i].code));
getlinep(f); // Read line 2
world[i].long_d=calloc(strlen(buffer)+1,sizeof(char));
strcpy(world[i].long_d,buffer);
if(verbose)
printf("Room %d [%s]\n",world[i].code,world[i].long_d);
if(compress_descriptions==true)
analyze(encodechar(world[i].long_d));
getlinep(f); // Read line 3
world[i].s=calloc(strlen(buffer)+1,sizeof(char));
strcpy(world[i].s,buffer);
if(compress_descriptions==true)
analyze(encodechar(world[i].s));
getlinep(f); // Read line 4
world[i].short_d=calloc(strlen(buffer)+1,sizeof(char));
strcpy(world[i].short_d,buffer);
if(compress_descriptions==true)
analyze(encodechar(world[i].short_d));
for(j=0;j<10;++j) {
getlinep(f);
if(sscanf(buffer,"%d",&(world[i].directions[j]))!=1) {
printf("Error reading directions.\n");
printf("Object code %d, direction %d\n",world[i].code,j+1);
getlinep(f);
printf("line [%s]\n",buffer);
return NULL;
}
}
}
return world;
}
/** Get the rooms in the game
*/
object* read_objects(FILE *f, int size)
{
char *errorp="Could not allocate enough memory for the objects.\n";
unsigned int i,j;
if(size<=0)
return NULL;
object *obj = (object*)calloc(size, sizeof(object));
if (obj==NULL) {
printf("%s",errorp);
return NULL;
}
if(verbose)
printf("\n");
for(i=0; i<size;++i) {
getlinep(f);
sscanf(buffer, "%d",&(obj[i].code));
getlinep(f);
obj[i].s=calloc(strlen(buffer)+1,sizeof(char));
strcpy(obj[i].s,buffer);
getlinep(f);
obj[i].desc=calloc(strlen(buffer)+1,sizeof(char));
strcpy(obj[i].desc,buffer);
if(compress_descriptions==true)
analyze(encodechar(obj[i].desc));
getlinep(f);
sscanf(buffer, "%d",&(obj[i].weight));
getlinep(f);
sscanf(buffer, "%d",&(obj[i].size));
getlinep(f);
sscanf(buffer, "%d",&(obj[i].position));
getlinep(f);
if(strcmp(buffer,"FALSE")) {
// obj[i].attributes|=ISNOTMOVABLE;
} else {
obj[i].attributes|=ISNOTMOVABLE;
}
getlinep(f);
if(strcmp(buffer,"TRUE")) {
// obj[i].isnotwereable=false;
} else {
obj[i].attributes|=ISWEREABLE;
}
if(verbose)
printf("Object %d [%s]\n",obj[i].code,obj[i].desc);
}
return obj;
}
info *read_header(FILE *f)
{
char *errorp="Could not allocate enough memory for header info.\n";
info *in = (info*)malloc(sizeof(info));
getlinep(f);
if(strcmp(buffer,"AWS")!=0) {
printf("This is not an AWS file!\n");
return NULL;
}
getlinep(f);
getlinep(f);
in->version=calloc(strlen(buffer)+1,sizeof(char));
strcpy(in->version,buffer);
getlinep(f);
sscanf(buffer, "%d",&(in->textcolor));
getlinep(f);
sscanf(buffer, "%d",&(in->backcolor));
getlinep(f);
sscanf(buffer, "%d",&(in->textcolordark));
getlinep(f);
sscanf(buffer, "%d",&(in->backcolordark));
getlinep(f);
in->name=calloc(strlen(buffer)+1,sizeof(char));
strcpy(in->name,buffer);
getlinep(f);
in->author=calloc(strlen(buffer)+1,sizeof(char));
strcpy(in->author,buffer);
getlinep(f);
in->date=calloc(strlen(buffer)+1,sizeof(char));
strcpy(in->date,buffer);
getlinep(f);
in->description=calloc(strlen(buffer)+1,sizeof(char));
strcpy(in->description,buffer);
getlinep(f);
sscanf(buffer, "%d",&(in->code));
getlinep(f);
in->fontname=calloc(strlen(buffer)+1,sizeof(char));
strcpy(in->fontname,buffer);
getlinep(f);
sscanf(buffer, "%d",&(in->charsize));
getlinep(f);
sscanf(buffer, "%d",&(in->fontstyle));
getlinep(f);
sscanf(buffer, "%d",&(in->startroom));
getlinep(f);
if(strcmp(buffer,"FALSE")) {
in->graphical=false;
} else {
in->graphical=true;
}
getlinep(f);
sscanf(buffer, "%d",&(in->maxcarryingw));
getlinep(f);
sscanf(buffer, "%d",&(in->maxcarryings));
if(compress_messages==true) {
analyze(encodechar(in->name));
analyze(encodechar(in->author));
analyze(encodechar(in->date));
analyze(encodechar(in->description));
}
return in;
}
/** Get the messages in the game
*/
message* read_messages(FILE *f, int size)
{
char *errorp="Could not allocate enough memory for messages.\n";
unsigned int i,j;
message *msg = (message*)calloc(size, sizeof(message));
if (msg==NULL) {
printf("%s",errorp);
return NULL;
}
if(verbose)
printf("\n");
for(i=0; i<size;++i) {
getlinep(f);
sscanf(buffer, "%d",&(msg[i].code));
getlinep(f);
msg[i].txt=calloc(strlen(buffer)+1,sizeof(char));
strcpy(msg[i].txt,buffer);
if(compress_messages==true)
analyze(encodechar(msg[i].txt));
if(verbose)
printf("Message %d [%s]\n",msg[i].code,msg[i].txt);
}
if(compress_messages==true)
analyze(AREYOUSURE);
analyze(EXITRESTART);
return msg;
}
/** Get the number of the objects in the game.
*/
unsigned int get_objects_number(FILE *f)
{
fpos_t pos;
unsigned int counter=0;
unsigned int sl=0;
while(getlinep(f)){
if(strcmp(buffer,"OGGETTI")==0) {
break;
}
}
fgetpos(f, &pos);
while (getlinep(f)) {
if(strcmp(buffer,"FINEDATI")==0) {
break;
}
++counter;
}
fsetpos(f, &pos);
return counter/8;
}
/** Get the number of messages in the game.
*/
int get_messages_number(FILE *f)
{
fpos_t pos;
int counter=0;
while(getlinep(f)){
if(strcmp(buffer,"MESSAGGI")==0) {
break;
}
}
fgetpos(f, &pos);
while (getlinep(f)) {
if(strcmp(buffer,"OGGETTI")==0) {
break;
}
++counter;
}
fsetpos(f, &pos);
return counter/2;
}
/** Get the number of "high conditions" in the file.
*/
int get_hi_cond_size(FILE *f)
{
fpos_t pos;
int counter=0;
while (getlinep(f)) {
if(strcmp(buffer,"CONDIZIONIHI")==0) {
break;
}
}
fgetpos(f, &pos);
while (getlinep(f)) {
if(strcmp(buffer,"CONDIZIONILOW")==0) {
break;
}
++counter;
}
fsetpos(f, &pos);
return counter;
}
/** Get the number of "low conditions" in the file.
*/
int get_low_cond_size(FILE *f)
{
fpos_t pos;
int counter=0;
while(getlinep(f)){
if(strcmp(buffer,"CONDIZIONILOW")==0) {
break;
}
}
fgetpos(f, &pos);
while (getlinep(f)) {
if(strcmp(buffer,"CONDIZIONILOCALI")==0) {
break;
}
++counter;
}
fsetpos(f, &pos);
return counter;
}
/** Get the number of "local conditions" in the file.
*/
int get_local_cond_size(FILE *f)
{
fpos_t pos;
int counter=0;
while(getlinep(f)){
if(strcmp(buffer,"CONDIZIONILOCALI")==0) {
break;
}
}
fgetpos(f, &pos);
while (getlinep(f)) {
if(strcmp(buffer,"DIZIONARIO")==0) {
break;
}
++counter;
}
fsetpos(f, &pos);
return (counter)/2;
}
char token[BUFFERSIZE];
char next[BUFFERSIZE];
/** Get a new token (store it in the shared variable "token") and
return the new position in the line.
*/
unsigned int get_token(char *line, unsigned int pos)
{
char c;
unsigned int k=0;
while(line[pos]==' ')
++pos;
for(;(c=line[pos])!='\0' && c!=' ';++pos) {
if(c>='a' && c<='z')
c-=32; // Convert to uppercase
token[k++]=c;
}
token[k]='\0';
return pos+1;
}
/** Give a peek to the next token (store it in the shared variable "next").
*/
unsigned int peek_token(char *line, unsigned int pos)
{
char c;
unsigned int k=0;
while(line[pos]==' ')
++pos;
for(;(c=line[pos])!='\0' && c!=' ';++pos) {
if(c>='a' && c<='z')
c-=32; // Convert to uppercase
next[k++]=c;
}
next[k]='\0';
return pos+1;
}
void strcon(char *str1, const char* str2)
{
unsigned int i,j;
char c;
for(i=0;str1[i]!='\0'&&i<BUFFERSIZE;++i) ;
for(j=0;(c=str2[j])!='\0'&&i<BUFFERSIZE;++j)
str1[i++]=str2[j];
str1[i]='\0';
}
unsigned int process_functions(char *line, unsigned int scanpos)
{
unsigned int value;
scanpos=get_token(line, scanpos);
if(strcmp(token,"NO1")==0) {
strcon(function_res,"noun1");
} else if(strcmp(token,"CTR")==0) {
strcon(function_res,"counter[");
scanpos=process_functions(line,scanpos);
strcon(function_res,"]");
} else if(strcmp(token,"VBNO")==0) {
strcon(function_res,"verb");
} else if(strcmp(token,"NO2")==0) {
strcon(function_res,"noun2");
} else if(strcmp(token,"ADJENO")==0||strcmp(token,"ADVENO")==0) {
strcon(function_res,"adve");
} else if(strcmp(token,"ROOM")==0) {
strcon(function_res,"current_position");
} else if(strcmp(token,"OBJLOC")==0) {
strcon(function_res,"get_object_position(");
scanpos=process_functions(line,scanpos);
strcon(function_res,")");
} else if(strcmp(token,"WEIG")==0) {
strcon(function_res,"search_object_p(");
scanpos=process_functions(line,scanpos);
strcon(function_res,")->weight");
if(dont_care_size_weight) {
fprintf(stderr,
"WARNING: selected -w option and adventure exploits WEIGH!\n");
}
} else if(strcmp(token,"ACTORNO")==0) {
strcon(function_res,"actor");
} else {
if(sscanf(token, "%d",&value)==1) {
sprintf(token, "%d", value);
strcon(function_res,token);
} else {
printf("Function not recognized %s in line\n[%s]\n", token,line);
++no_of_errors;
}
}
return scanpos;
}
/* Decisions */
/** AT */
unsigned int decision_at(FILE *f, char *line, unsigned int scanpos)
{
boolean proc=false;
unsigned int sp;
int val1;
boolean polarity=true;
char *arg1=NULL,*arg2=NULL;
start_function();
scanpos=process_functions(line, scanpos);
arg1=(char *) calloc(strlen(function_res)+1,sizeof(char));
if(arg1==NULL) {
printf("Error allocating memory!\n");
exit(1);
}
strcpy(arg1,function_res);
sp=peek_token(line, scanpos);
if(shortcuts==true&&(strcmp(next,"AND")==0)) {
sp=peek_token(line, sp);
if(strcmp(next,"SET?")==0||strcmp(next,"RES?")==0) {
if(strcmp(next,"RES?")==0)
polarity=false;
start_function();
sp=process_functions(line, sp);
arg2=(char *) calloc(strlen(function_res)+1,sizeof(char));
if(arg2==NULL) {
printf("Error allocating memory!\n");
exit(1);
}
strcpy(arg2,function_res);
sp=peek_token(line, sp);
if(strcmp(next,"THEN")==0) {
sp=peek_token(line, sp);
if(strcmp(next,"MESS")==0) {
start_function();
sp=process_functions(line, sp);
peek_token(line, sp);
if(strcmp(next,"ENDIF")==0) {
scanpos=sp;
proc=true;
complete_shortcut=true;
need_amsm=true;
if(hardcoded_messages==false)
fprintf(f, "1) amsm(%s,%s,%d,%s);\n\n",
arg1,arg2,polarity,function_res);
else
fprintf(f, "1) amsm(%s,%s,%d,message%s);\n\n",
arg1,arg2,polarity,function_res);
}
}/* else {
if (polarity) {
need_as=true;
proc=true;
fprintf(f, "check_position_marker_on(%s,%s)", arg1,arg2);
} else {
need_ar=true;
proc=true;
fprintf(f, "check_position_marker_off(%s,%s)", arg1,arg2);
}
}*/
} /*else {
if (polarity) {
need_as=true;
proc=true;
fprintf(f, "check_position_marker_on(%s,%s)", arg1,arg2);
} else {
need_ar=true;
proc=true;
fprintf(f, "check_position_marker_off(%s,%s)", arg1,arg2);
}
}*/
}
}
if(proc==false) {
// This actually increases the size of the executable (C128)
/* if(sscanf(arg1, "%d",&val1)==1 && val1<256) {
fprintf(f, "cpi(%s)", arg1);
need_cpi=true;
} else {*/
fprintf(f, "current_position==%s", arg1);
//}
}
if(arg1!=NULL)
free(arg1);
if(arg2!=NULL)
free(arg2);
return scanpos;
}
/** NOTAT */
unsigned int decision_notat(FILE *f, char *line, unsigned int scanpos)
{
start_function();
scanpos=process_functions(line, scanpos);
fprintf(f, "current_position!=%s", function_res);
return scanpos;
}
/** SET? */
unsigned int decision_set(FILE *f, char *line, unsigned int scanpos)
{
start_function();
scanpos=process_functions(line, scanpos);
fprintf(f, "marker[%s]", function_res);
return scanpos;
}