forked from DavidKinder/Inform6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.c
2256 lines (1905 loc) · 85.9 KB
/
objects.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
/* ------------------------------------------------------------------------- */
/* "objects" : [1] the object-maker, which constructs objects and enters */
/* them into the tree, given a low-level specification; */
/* */
/* [2] the parser of Object/Nearby/Class directives, which */
/* checks syntax and translates such directives into */
/* specifications for the object-maker. */
/* */
/* Part of Inform 6.33 */
/* copyright (c) Graham Nelson 1993 - 2013 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
/* ------------------------------------------------------------------------- */
/* Objects. */
/* ------------------------------------------------------------------------- */
int no_objects; /* Number of objects made so far */
static int no_embedded_routines; /* Used for naming routines which
are given as property values: these
are called EmbeddedRoutine__1, ... */
static fpropt full_object; /* "fpropt" is a typedef for a struct
containing an array to hold the
attribute and property values of
a single object. We only keep one
of these, for the current object
being made, and compile it into
Z-machine tables when each object
definition is complete, since
sizeof(fpropt) is about 6200 bytes */
static fproptg full_object_g; /* Equivalent for Glulx. This object
is very small, since the large arrays
are allocated dynamically by the
Glulx compiler */
static char shortname_buffer[766]; /* Text buffer to hold the short name
(which is read in first, but
written almost last) */
static int parent_of_this_obj;
static char *classname_text, *objectname_text;
/* For printing names of embedded
routines only */
/* ------------------------------------------------------------------------- */
/* Classes. */
/* ------------------------------------------------------------------------- */
/* Arrays defined below: */
/* */
/* int32 class_begins_at[n] offset of properties block for */
/* nth class (always an offset */
/* inside the properties_table) */
/* int classes_to_inherit_from[] The list of classes to inherit */
/* from as taken from the current */
/* Nearby/Object/Class definition */
/* int class_object_numbers[n] The number of the prototype-object */
/* for the nth class */
/* ------------------------------------------------------------------------- */
int no_classes; /* Number of class defns made so far */
static int current_defn_is_class, /* TRUE if current Nearby/Object/Class
defn is in fact a Class definition */
no_classes_to_inherit_from; /* Number of classes in the list
of classes to inherit in the
current Nearby/Object/Class defn */
/* ------------------------------------------------------------------------- */
/* Making attributes and properties. */
/* ------------------------------------------------------------------------- */
int no_attributes, /* Number of attributes defined so far */
no_properties; /* Number of properties defined so far,
plus 1 (properties are numbered from
1 and Inform creates "name" and two
others itself, so the variable begins
the compilation pass set to 4) */
static void trace_s(char *name, int32 number, int f)
{ if (!printprops_switch) return;
printf("%s %02ld ",(f==0)?"Attr":"Prop",(long int) number);
if (f==0) printf(" ");
else printf("%s%s",(prop_is_long[number])?"L":" ",
(prop_is_additive[number])?"A":" ");
printf(" %s\n",name);
}
extern void make_attribute(void)
{ int i; char *name;
debug_location_beginning beginning_debug_location =
get_token_location_beginning();
if (!glulx_mode) {
if (no_attributes==((version_number==3)?32:48))
{ discard_token_location(beginning_debug_location);
if (version_number==3)
error("All 32 attributes already declared (compile as Advanced \
game to get an extra 16)");
else
error("All 48 attributes already declared");
panic_mode_error_recovery();
put_token_back();
return;
}
}
else {
if (no_attributes==NUM_ATTR_BYTES*8) {
discard_token_location(beginning_debug_location);
error_numbered(
"All attributes already declared -- increase NUM_ATTR_BYTES to use \
more than",
NUM_ATTR_BYTES*8);
panic_mode_error_recovery();
put_token_back();
return;
}
}
get_next_token();
i = token_value; name = token_text;
if ((token_type != SYMBOL_TT) || (!(sflags[i] & UNKNOWN_SFLAG)))
{ discard_token_location(beginning_debug_location);
ebf_error("new attribute name", token_text);
panic_mode_error_recovery();
put_token_back();
return;
}
directive_keywords.enabled = TRUE;
get_next_token();
directive_keywords.enabled = FALSE;
if ((token_type == DIR_KEYWORD_TT) && (token_value == ALIAS_DK))
{ get_next_token();
if (!((token_type == SYMBOL_TT)
&& (stypes[token_value] == ATTRIBUTE_T)))
{ discard_token_location(beginning_debug_location);
ebf_error("an existing attribute name after 'alias'",
token_text);
panic_mode_error_recovery();
put_token_back();
return;
}
assign_symbol(i, svals[token_value], ATTRIBUTE_T);
sflags[token_value] |= ALIASED_SFLAG;
sflags[i] |= ALIASED_SFLAG;
}
else
{ assign_symbol(i, no_attributes++, ATTRIBUTE_T);
put_token_back();
}
if (debugfile_switch)
{ debug_file_printf("<attribute>");
debug_file_printf("<identifier>%s</identifier>", name);
debug_file_printf("<value>%d</value>", svals[i]);
write_debug_locations(get_token_location_end(beginning_debug_location));
debug_file_printf("</attribute>");
}
trace_s(name, svals[i], 0);
return;
}
extern void make_property(void)
{ int32 default_value, i;
int additive_flag=FALSE; char *name;
assembly_operand AO;
debug_location_beginning beginning_debug_location =
get_token_location_beginning();
if (!glulx_mode) {
if (no_properties==((version_number==3)?32:64))
{ discard_token_location(beginning_debug_location);
if (version_number==3)
error("All 30 properties already declared (compile as \
Advanced game to get an extra 62)");
else
error("All 62 properties already declared");
panic_mode_error_recovery();
put_token_back();
return;
}
}
else {
/* INDIV_PROP_START could be a memory setting */
if (no_properties==INDIV_PROP_START) {
discard_token_location(beginning_debug_location);
error_numbered("All properties already declared -- max is",
INDIV_PROP_START);
panic_mode_error_recovery();
put_token_back();
return;
}
}
do
{ directive_keywords.enabled = TRUE;
get_next_token();
if ((token_type == DIR_KEYWORD_TT) && (token_value == LONG_DK))
obsolete_warning("all properties are now automatically 'long'");
else
if ((token_type == DIR_KEYWORD_TT) && (token_value == ADDITIVE_DK))
additive_flag = TRUE;
else break;
} while (TRUE);
put_token_back();
directive_keywords.enabled = FALSE;
get_next_token();
i = token_value; name = token_text;
if ((token_type != SYMBOL_TT) || (!(sflags[i] & UNKNOWN_SFLAG)))
{ discard_token_location(beginning_debug_location);
ebf_error("new property name", token_text);
panic_mode_error_recovery();
put_token_back();
return;
}
directive_keywords.enabled = TRUE;
get_next_token();
directive_keywords.enabled = FALSE;
if (strcmp(name+strlen(name)-3, "_to") == 0) sflags[i] |= STAR_SFLAG;
if ((token_type == DIR_KEYWORD_TT) && (token_value == ALIAS_DK))
{ discard_token_location(beginning_debug_location);
if (additive_flag)
{ error("'alias' incompatible with 'additive'");
panic_mode_error_recovery();
put_token_back();
return;
}
get_next_token();
if (!((token_type == SYMBOL_TT)
&& (stypes[token_value] == PROPERTY_T)))
{ ebf_error("an existing property name after 'alias'",
token_text);
panic_mode_error_recovery();
put_token_back();
return;
}
assign_symbol(i, svals[token_value], PROPERTY_T);
trace_s(name, svals[i], 1);
sflags[token_value] |= ALIASED_SFLAG;
sflags[i] |= ALIASED_SFLAG;
return;
}
default_value = 0;
put_token_back();
if (!((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)))
{ AO = parse_expression(CONSTANT_CONTEXT);
default_value = AO.value;
if (AO.marker != 0)
backpatch_zmachine(AO.marker, PROP_DEFAULTS_ZA,
(no_properties-1) * WORDSIZE);
}
prop_default_value[no_properties] = default_value;
prop_is_long[no_properties] = TRUE;
prop_is_additive[no_properties] = additive_flag;
assign_symbol(i, no_properties++, PROPERTY_T);
if (debugfile_switch)
{ debug_file_printf("<property>");
debug_file_printf("<identifier>%s</identifier>", name);
debug_file_printf("<value>%d</value>", svals[i]);
write_debug_locations
(get_token_location_end(beginning_debug_location));
debug_file_printf("</property>");
}
trace_s(name, svals[i], 1);
}
/* ------------------------------------------------------------------------- */
/* Properties. */
/* ------------------------------------------------------------------------- */
int32 *prop_default_value; /* Default values for properties */
int *prop_is_long, /* Property modifiers, TRUE or FALSE:
"long" means "never write a 1-byte
value to this property", and is an
obsolete feature: since Inform 5
all properties have been "long" */
*prop_is_additive; /* "additive" means that values
accumulate rather than erase each
other during class inheritance */
char *properties_table; /* Holds the table of property values
(holding one block for each object
and coming immediately after the
object tree in Z-memory) */
int properties_table_size; /* Number of bytes in this table */
/* ------------------------------------------------------------------------- */
/* Individual properties */
/* */
/* Each new i.p. name is given a unique number. These numbers start from */
/* 72, since 0 is reserved as a null, 1 to 63 refer to common properties */
/* and 64 to 71 are kept for methods of the metaclass Class (for example, */
/* 64 is "create"). */
/* */
/* An object provides individual properties by having property 3 set to a */
/* non-zero value, which must be a byte address of a table in the form: */
/* */
/* <record-1> ... <record-n> 00 00 */
/* */
/* where a <record> looks like */
/* */
/* <identifier> <size> <up to 255 bytes of data> */
/* or <identifier + 0x8000> */
/* ----- 2 bytes ---------- 1 byte <size> number of bytes */
/* */
/* The <identifier> part is the number allocated to the name of what is */
/* being provided. The top bit of this word is set to indicate that */
/* although the individual property is being provided, it is provided */
/* only privately (so that it is inaccessible except to the object's own */
/* embedded routines). */
/* */
/* In Glulx: i-props are numbered from INDIV_PROP_START+8 up. And all */
/* properties, common and individual, are stored in the same table. */
/* ------------------------------------------------------------------------- */
int no_individual_properties; /* Actually equal to the next
identifier number to be allocated,
so this is initially 72 even though
none have been made yet. */
static int individual_prop_table_size; /* Size of the table of individual
properties so far for current obj */
uchar *individuals_table; /* Table of records, each being the
i.p. table for an object */
int i_m; /* Write mark position in the above */
int individuals_length; /* Extent of individuals_table */
/* ------------------------------------------------------------------------- */
/* Arrays used by this file */
/* ------------------------------------------------------------------------- */
objecttz *objectsz; /* Z-code only */
objecttg *objectsg; /* Glulx only */
uchar *objectatts; /* Glulx only */
static int *classes_to_inherit_from;
int *class_object_numbers;
int32 *class_begins_at;
/* ------------------------------------------------------------------------- */
/* Tracing for compiler maintenance */
/* ------------------------------------------------------------------------- */
extern void list_object_tree(void)
{ int i;
printf("obj par nxt chl Object tree:\n");
for (i=0; i<no_objects; i++)
printf("%3d %3d %3d %3d\n",
i+1,objectsz[i].parent,objectsz[i].next, objectsz[i].child);
}
/* ------------------------------------------------------------------------- */
/* Object and class manufacture begins here. */
/* */
/* These definitions have headers (parsed far, far below) and a series */
/* of segments, introduced by keywords and optionally separated by commas. */
/* Each segment has its own parsing routine. Note that when errors are */
/* detected, parsing continues rather than being abandoned, which assists */
/* a little in "error recovery" (i.e. in stopping lots more errors being */
/* produced for essentially the same mistake). */
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
/* [1] The object-maker: builds an object from a specification, viz.: */
/* */
/* full_object, */
/* shortname_buffer, */
/* parent_of_this_obj, */
/* current_defn_is_class (flag) */
/* classes_to_inherit_from[], no_classes_to_inherit_from, */
/* individual_prop_table_size (to date ) */
/* */
/* For efficiency's sake, the individual properties table has already been */
/* created (as far as possible, i.e., all except for inherited individual */
/* properties); unless the flag is clear, in which case the actual */
/* definition did not specify any individual properties. */
/* ========================================================================= */
/* Property inheritance from classes. */
/* ------------------------------------------------------------------------- */
static void property_inheritance_z(void)
{
/* Apply the property inheritance rules to full_object, which should
initially be complete (i.e., this routine takes place after the whole
Nearby/Object/Class definition has been parsed through).
On exit, full_object contains the final state of the properties to
be written. */
int i, j, k, kmax, class, mark,
prop_number, prop_length, prop_in_current_defn;
uchar *class_prop_block;
ASSERT_ZCODE();
for (class=0; class<no_classes_to_inherit_from; class++)
{
j=0;
mark = class_begins_at[classes_to_inherit_from[class]-1];
class_prop_block = (uchar *) (properties_table + mark);
while (class_prop_block[j]!=0)
{ if (version_number == 3)
{ prop_number = class_prop_block[j]%32;
prop_length = 1 + class_prop_block[j++]/32;
}
else
{ prop_number = class_prop_block[j]%64;
prop_length = 1 + class_prop_block[j++]/64;
if (prop_length > 2)
prop_length = class_prop_block[j++]%64;
}
/* So we now have property number prop_number present in the
property block for the class being read: its bytes are
class_prop_block[j, ..., j + prop_length - 1]
Question now is: is there already a value given in the
current definition under this property name? */
prop_in_current_defn = FALSE;
kmax = full_object.l;
for (k=0; k<kmax; k++)
if (full_object.pp[k].num == prop_number)
{ prop_in_current_defn = TRUE;
/* (Note that the built-in "name" property is additive) */
if ((prop_number==1) || (prop_is_additive[prop_number]))
{
/* The additive case: we accumulate the class
property values onto the end of the full_object
property */
for (i=full_object.pp[k].l;
i<full_object.pp[k].l+prop_length/2; i++)
{ if (i >= 32)
{ error("An additive property has inherited \
so many values that the list has overflowed the maximum 32 entries");
break;
}
full_object.pp[k].ao[i].value = mark + j;
j += 2;
full_object.pp[k].ao[i].marker = INHERIT_MV;
full_object.pp[k].ao[i].type = LONG_CONSTANT_OT;
}
full_object.pp[k].l += prop_length/2;
}
else
/* The ordinary case: the full_object property
values simply overrides the class definition,
so we skip over the values in the class table */
j+=prop_length;
if (prop_number==3)
{ int y, z, class_block_offset;
uchar *p;
/* Property 3 holds the address of the table of
instance variables, so this is the case where
the object already has instance variables in its
own table but must inherit some more from the
class */
class_block_offset = class_prop_block[j-2]*256
+ class_prop_block[j-1];
p = individuals_table + class_block_offset;
z = class_block_offset;
while ((p[0]!=0)||(p[1]!=0))
{ int already_present = FALSE, l;
for (l = full_object.pp[k].ao[0].value; l < i_m;
l = l + 3 + individuals_table[l + 2])
if (individuals_table[l] == p[0]
&& individuals_table[l + 1] == p[1])
{ already_present = TRUE; break;
}
if (already_present == FALSE)
{ if (module_switch)
backpatch_zmachine(IDENT_MV,
INDIVIDUAL_PROP_ZA, i_m);
if (i_m+3+p[2] > MAX_INDIV_PROP_TABLE_SIZE)
memoryerror("MAX_INDIV_PROP_TABLE_SIZE",
MAX_INDIV_PROP_TABLE_SIZE);
individuals_table[i_m++] = p[0];
individuals_table[i_m++] = p[1];
individuals_table[i_m++] = p[2];
for (y=0;y < p[2]/2;y++)
{ individuals_table[i_m++] = (z+3+y*2)/256;
individuals_table[i_m++] = (z+3+y*2)%256;
backpatch_zmachine(INHERIT_INDIV_MV,
INDIVIDUAL_PROP_ZA, i_m-2);
}
}
z += p[2] + 3;
p += p[2] + 3;
}
individuals_length = i_m;
}
/* For efficiency we exit the loop now (this property
number has been dealt with) */
break;
}
if (!prop_in_current_defn)
{
/* The case where the class defined a property which wasn't
defined at all in full_object: we copy out the data into
a new property added to full_object */
k=full_object.l++;
full_object.pp[k].num = prop_number;
full_object.pp[k].l = prop_length/2;
for (i=0; i<prop_length/2; i++)
{ full_object.pp[k].ao[i].value = mark + j;
j+=2;
full_object.pp[k].ao[i].marker = INHERIT_MV;
full_object.pp[k].ao[i].type = LONG_CONSTANT_OT;
}
if (prop_number==3)
{ int y, z, class_block_offset;
uchar *p;
/* Property 3 holds the address of the table of
instance variables, so this is the case where
the object had no instance variables of its own
but must inherit some more from the class */
if (individual_prop_table_size++ == 0)
{ full_object.pp[k].num = 3;
full_object.pp[k].l = 1;
full_object.pp[k].ao[0].value
= individuals_length;
full_object.pp[k].ao[0].marker = INDIVPT_MV;
full_object.pp[k].ao[0].type = LONG_CONSTANT_OT;
i_m = individuals_length;
}
class_block_offset = class_prop_block[j-2]*256
+ class_prop_block[j-1];
p = individuals_table + class_block_offset;
z = class_block_offset;
while ((p[0]!=0)||(p[1]!=0))
{ if (module_switch)
backpatch_zmachine(IDENT_MV, INDIVIDUAL_PROP_ZA, i_m);
if (i_m+3+p[2] > MAX_INDIV_PROP_TABLE_SIZE)
memoryerror("MAX_INDIV_PROP_TABLE_SIZE",
MAX_INDIV_PROP_TABLE_SIZE);
individuals_table[i_m++] = p[0];
individuals_table[i_m++] = p[1];
individuals_table[i_m++] = p[2];
for (y=0;y < p[2]/2;y++)
{ individuals_table[i_m++] = (z+3+y*2)/256;
individuals_table[i_m++] = (z+3+y*2)%256;
backpatch_zmachine(INHERIT_INDIV_MV,
INDIVIDUAL_PROP_ZA, i_m-2);
}
z += p[2] + 3;
p += p[2] + 3;
}
individuals_length = i_m;
}
}
}
}
if (individual_prop_table_size > 0)
{
if (i_m+2 > MAX_INDIV_PROP_TABLE_SIZE)
memoryerror("MAX_INDIV_PROP_TABLE_SIZE",
MAX_INDIV_PROP_TABLE_SIZE);
individuals_table[i_m++] = 0;
individuals_table[i_m++] = 0;
individuals_length += 2;
}
}
static void property_inheritance_g(void)
{
/* Apply the property inheritance rules to full_object, which should
initially be complete (i.e., this routine takes place after the whole
Nearby/Object/Class definition has been parsed through).
On exit, full_object contains the final state of the properties to
be written. */
int i, j, k, class, num_props,
prop_number, prop_length, prop_flags, prop_in_current_defn;
int32 mark, prop_addr;
uchar *cpb, *pe;
ASSERT_GLULX();
for (class=0; class<no_classes_to_inherit_from; class++) {
mark = class_begins_at[classes_to_inherit_from[class]-1];
cpb = (uchar *) (properties_table + mark);
/* This now points to the compiled property-table for the class.
We'll have to go through and decompile it. (For our sins.) */
num_props = ReadInt32(cpb);
for (j=0; j<num_props; j++) {
pe = cpb + 4 + j*10;
prop_number = ReadInt16(pe);
pe += 2;
prop_length = ReadInt16(pe);
pe += 2;
prop_addr = ReadInt32(pe);
pe += 4;
prop_flags = ReadInt16(pe);
pe += 2;
/* So we now have property number prop_number present in the
property block for the class being read. Its bytes are
cpb[prop_addr ... prop_addr + prop_length - 1]
Question now is: is there already a value given in the
current definition under this property name? */
prop_in_current_defn = FALSE;
for (k=0; k<full_object_g.numprops; k++) {
if (full_object_g.props[k].num == prop_number) {
prop_in_current_defn = TRUE;
break;
}
}
if (prop_in_current_defn) {
if ((prop_number==1)
|| (prop_number < INDIV_PROP_START
&& prop_is_additive[prop_number])) {
/* The additive case: we accumulate the class
property values onto the end of the full_object
properties. Remember that k is still the index number
of the first prop-block matching our property number. */
int prevcont;
if (full_object_g.props[k].continuation == 0) {
full_object_g.props[k].continuation = 1;
prevcont = 1;
}
else {
prevcont = full_object_g.props[k].continuation;
for (k++; k<full_object_g.numprops; k++) {
if (full_object_g.props[k].num == prop_number) {
prevcont = full_object_g.props[k].continuation;
}
}
}
k = full_object_g.numprops++;
full_object_g.props[k].num = prop_number;
full_object_g.props[k].flags = 0;
full_object_g.props[k].datastart = full_object_g.propdatasize;
full_object_g.props[k].continuation = prevcont+1;
full_object_g.props[k].datalen = prop_length;
if (full_object_g.propdatasize + prop_length
> MAX_OBJ_PROP_TABLE_SIZE) {
memoryerror("MAX_OBJ_PROP_TABLE_SIZE",MAX_OBJ_PROP_TABLE_SIZE);
}
for (i=0; i<prop_length; i++) {
int ppos = full_object_g.propdatasize++;
full_object_g.propdata[ppos].value = prop_addr + 4*i;
full_object_g.propdata[ppos].marker = INHERIT_MV;
full_object_g.propdata[ppos].type = CONSTANT_OT;
}
}
else {
/* The ordinary case: the full_object_g property
values simply overrides the class definition,
so we skip over the values in the class table. */
}
}
else {
/* The case where the class defined a property which wasn't
defined at all in full_object_g: we copy out the data into
a new property added to full_object_g. */
k = full_object_g.numprops++;
full_object_g.props[k].num = prop_number;
full_object_g.props[k].flags = prop_flags;
full_object_g.props[k].datastart = full_object_g.propdatasize;
full_object_g.props[k].continuation = 0;
full_object_g.props[k].datalen = prop_length;
if (full_object_g.propdatasize + prop_length
> MAX_OBJ_PROP_TABLE_SIZE) {
memoryerror("MAX_OBJ_PROP_TABLE_SIZE",MAX_OBJ_PROP_TABLE_SIZE);
}
for (i=0; i<prop_length; i++) {
int ppos = full_object_g.propdatasize++;
full_object_g.propdata[ppos].value = prop_addr + 4*i;
full_object_g.propdata[ppos].marker = INHERIT_MV;
full_object_g.propdata[ppos].type = CONSTANT_OT;
}
}
if (full_object_g.numprops == MAX_OBJ_PROP_COUNT) {
memoryerror("MAX_OBJ_PROP_COUNT",MAX_OBJ_PROP_COUNT);
}
}
}
}
/* ------------------------------------------------------------------------- */
/* Construction of Z-machine-format property blocks. */
/* ------------------------------------------------------------------------- */
static int write_properties_between(uchar *p, int mark, int from, int to)
{ int j, k, prop_number, prop_length;
/* Note that p is properties_table. */
for (prop_number=to; prop_number>=from; prop_number--)
{ for (j=0; j<full_object.l; j++)
{ if ((full_object.pp[j].num == prop_number)
&& (full_object.pp[j].l != 100))
{ prop_length = 2*full_object.pp[j].l;
if (mark+2+prop_length >= MAX_PROP_TABLE_SIZE)
memoryerror("MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);
if (version_number == 3)
p[mark++] = prop_number + (prop_length - 1)*32;
else
{ switch(prop_length)
{ case 1:
p[mark++] = prop_number; break;
case 2:
p[mark++] = prop_number + 0x40; break;
default:
p[mark++] = prop_number + 0x80;
p[mark++] = prop_length + 0x80; break;
}
}
for (k=0; k<full_object.pp[j].l; k++)
{ if (full_object.pp[j].ao[k].marker != 0)
backpatch_zmachine(full_object.pp[j].ao[k].marker,
PROP_ZA, mark);
p[mark++] = full_object.pp[j].ao[k].value/256;
p[mark++] = full_object.pp[j].ao[k].value%256;
}
}
}
}
p[mark++]=0;
return(mark);
}
static int write_property_block_z(char *shortname)
{
/* Compile the (now complete) full_object properties into a
property-table block at "p" in Inform's memory.
"shortname" is the object's short name, if specified; otherwise
NULL.
Return the number of bytes written to the block. */
int32 mark = properties_table_size, i;
uchar *p = (uchar *) properties_table;
/* printf("Object at %04x\n", mark); */
if (shortname != NULL)
{ uchar *tmp;
if (mark+1+510 >= MAX_PROP_TABLE_SIZE)
memoryerror("MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);
tmp = translate_text(p+mark+1,p+mark+1+510,shortname);
if (!tmp) error ("Short name of object exceeded 765 Z-characters");
i = subtract_pointers(tmp,(p+mark+1));
p[mark] = i/2;
mark += i+1;
}
if (current_defn_is_class)
{ mark = write_properties_between(p,mark,3,3);
for (i=0;i<6;i++)
p[mark++] = full_object.atts[i];
class_begins_at[no_classes++] = mark;
}
mark = write_properties_between(p, mark, 1, (version_number==3)?31:63);
i = mark - properties_table_size;
properties_table_size = mark;
return(i);
}
static int gpropsort(void *ptr1, void *ptr2)
{
propg *prop1 = ptr1;
propg *prop2 = ptr2;
if (prop2->num == -1)
return -1;
if (prop1->num == -1)
return 1;
if (prop1->num < prop2->num)
return -1;
if (prop1->num > prop2->num)
return 1;
return (prop1->continuation - prop2->continuation);
}
static int32 write_property_block_g(void)
{
/* Compile the (now complete) full_object properties into a
property-table block at "p" in Inform's memory.
Return the number of bytes written to the block.
In Glulx, the shortname property isn't used here; it's already
been compiled into an ordinary string. */
int32 i;
int ix, jx, kx, totalprops;
int32 mark = properties_table_size;
int32 datamark;
uchar *p = (uchar *) properties_table;
if (current_defn_is_class) {
for (i=0;i<NUM_ATTR_BYTES;i++)
p[mark++] = full_object_g.atts[i];
class_begins_at[no_classes++] = mark;
}
qsort(full_object_g.props, full_object_g.numprops, sizeof(propg),
(int (*)(const void *, const void *))(&gpropsort));
full_object_g.finalpropaddr = mark;
totalprops = 0;
for (ix=0; ix<full_object_g.numprops; ix=jx) {
int propnum = full_object_g.props[ix].num;
if (propnum == -1)
break;
for (jx=ix;
jx<full_object_g.numprops && full_object_g.props[jx].num == propnum;
jx++);
totalprops++;
}
/* Write out the number of properties in this table. */
if (mark+4 >= MAX_PROP_TABLE_SIZE)
memoryerror("MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);
WriteInt32(p+mark, totalprops);
mark += 4;
datamark = mark + 10*totalprops;
for (ix=0; ix<full_object_g.numprops; ix=jx) {
int propnum = full_object_g.props[ix].num;
int flags = full_object_g.props[ix].flags;
int totallen = 0;
int32 datamarkstart = datamark;
if (propnum == -1)
break;
for (jx=ix;
jx<full_object_g.numprops && full_object_g.props[jx].num == propnum;
jx++) {
int32 datastart = full_object_g.props[jx].datastart;
if (datamark+4*full_object_g.props[jx].datalen >= MAX_PROP_TABLE_SIZE)
memoryerror("MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);
for (kx=0; kx<full_object_g.props[jx].datalen; kx++) {
int32 val = full_object_g.propdata[datastart+kx].value;
WriteInt32(p+datamark, val);
if (full_object_g.propdata[datastart+kx].marker != 0)
backpatch_zmachine(full_object_g.propdata[datastart+kx].marker,
PROP_ZA, datamark);
totallen++;
datamark += 4;
}
}
if (mark+10 >= MAX_PROP_TABLE_SIZE)
memoryerror("MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);
WriteInt16(p+mark, propnum);
mark += 2;
WriteInt16(p+mark, totallen);
mark += 2;
WriteInt32(p+mark, datamarkstart);
mark += 4;
WriteInt16(p+mark, flags);
mark += 2;
}
mark = datamark;
i = mark - properties_table_size;
properties_table_size = mark;
return i;
}
/* ------------------------------------------------------------------------- */
/* The final stage in Nearby/Object/Class definition processing. */
/* ------------------------------------------------------------------------- */
static void manufacture_object_z(void)
{ int i, j;
segment_markers.enabled = FALSE;
directives.enabled = TRUE;
property_inheritance_z();
objectsz[no_objects].parent = parent_of_this_obj;
objectsz[no_objects].next = 0;
objectsz[no_objects].child = 0;
if ((parent_of_this_obj > 0) && (parent_of_this_obj != 0x7fff))
{ i = objectsz[parent_of_this_obj-1].child;
if (i == 0)
objectsz[parent_of_this_obj-1].child = no_objects + 1;
else
{ while(objectsz[i-1].next != 0) i = objectsz[i-1].next;
objectsz[i-1].next = no_objects+1;
}
}
/* The properties table consists simply of a sequence of property
blocks, one for each object in order of definition, exactly as
it will appear in the final Z-machine. */
j = write_property_block_z(shortname_buffer);
objectsz[no_objects].propsize = j;
if (properties_table_size >= MAX_PROP_TABLE_SIZE)
memoryerror("MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);
if (current_defn_is_class)
for (i=0;i<6;i++) objectsz[no_objects].atts[i] = 0;
else
for (i=0;i<6;i++)
objectsz[no_objects].atts[i] = full_object.atts[i];
no_objects++;
}
static void manufacture_object_g(void)
{ int32 i, j;
segment_markers.enabled = FALSE;
directives.enabled = TRUE;
property_inheritance_g();
objectsg[no_objects].parent = parent_of_this_obj;
objectsg[no_objects].next = 0;
objectsg[no_objects].child = 0;
if ((parent_of_this_obj > 0) && (parent_of_this_obj != 0x7fffffff))
{ i = objectsg[parent_of_this_obj-1].child;
if (i == 0)
objectsg[parent_of_this_obj-1].child = no_objects + 1;
else
{ while(objectsg[i-1].next != 0) i = objectsg[i-1].next;
objectsg[i-1].next = no_objects+1;
}
}
objectsg[no_objects].shortname = compile_string(shortname_buffer,
FALSE, FALSE);
/* The properties table consists simply of a sequence of property
blocks, one for each object in order of definition, exactly as
it will appear in the final machine image. */
j = write_property_block_g();
objectsg[no_objects].propaddr = full_object_g.finalpropaddr;
objectsg[no_objects].propsize = j;
if (properties_table_size >= MAX_PROP_TABLE_SIZE)
memoryerror("MAX_PROP_TABLE_SIZE",MAX_PROP_TABLE_SIZE);
if (current_defn_is_class)
for (i=0;i<NUM_ATTR_BYTES;i++)
objectatts[no_objects*NUM_ATTR_BYTES+i] = 0;
else
for (i=0;i<NUM_ATTR_BYTES;i++)
objectatts[no_objects*NUM_ATTR_BYTES+i] = full_object_g.atts[i];
no_objects++;