Files
@ a22b6e149063
Branch filter:
Location: libtransport.git/backends/frotz/dfrotz/common/object.c
a22b6e149063
17.9 KiB
text/plain
Finished frotz backend :)
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 | /* object.c - Object manipulation opcodes
* Copyright (c) 1995-1997 Stefan Jokisch
*
* This file is part of Frotz.
*
* Frotz 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.
*
* Frotz is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "frotz.h"
#define MAX_OBJECT 2000
#define O1_PARENT 4
#define O1_SIBLING 5
#define O1_CHILD 6
#define O1_PROPERTY_OFFSET 7
#define O1_SIZE 9
#define O4_PARENT 6
#define O4_SIBLING 8
#define O4_CHILD 10
#define O4_PROPERTY_OFFSET 12
#define O4_SIZE 14
/*
* object_address
*
* Calculate the address of an object.
*
*/
static zword object_address (zword obj)
{
/* zchar obj_num[10]; */
/* Check object number */
if (obj > ((h_version <= V3) ? 255 : MAX_OBJECT)) {
print_string("@Attempt to address illegal object ");
print_num(obj);
print_string(". This is normally fatal.");
new_line();
runtime_error (ERR_ILL_OBJ);
}
/* Return object address */
if (h_version <= V3)
return h_objects + ((obj - 1) * O1_SIZE + 62);
else
return h_objects + ((obj - 1) * O4_SIZE + 126);
}/* object_address */
/*
* object_name
*
* Return the address of the given object's name.
*
*/
zword object_name (zword object)
{
zword obj_addr;
zword name_addr;
obj_addr = object_address (object);
/* The object name address is found at the start of the properties */
if (h_version <= V3)
obj_addr += O1_PROPERTY_OFFSET;
else
obj_addr += O4_PROPERTY_OFFSET;
LOW_WORD (obj_addr, name_addr)
return name_addr;
}/* object_name */
/*
* first_property
*
* Calculate the start address of the property list associated with
* an object.
*
*/
static zword first_property (zword obj)
{
zword prop_addr;
zbyte size;
/* Fetch address of object name */
prop_addr = object_name (obj);
/* Get length of object name */
LOW_BYTE (prop_addr, size)
/* Add name length to pointer */
return prop_addr + 1 + 2 * size;
}/* first_property */
/*
* next_property
*
* Calculate the address of the next property in a property list.
*
*/
static zword next_property (zword prop_addr)
{
zbyte value;
/* Load the current property id */
LOW_BYTE (prop_addr, value)
prop_addr++;
/* Calculate the length of this property */
if (h_version <= V3)
value >>= 5;
else if (!(value & 0x80))
value >>= 6;
else {
LOW_BYTE (prop_addr, value)
value &= 0x3f;
if (value == 0) value = 64; /* demanded by Spec 1.0 */
}
/* Add property length to current property pointer */
return prop_addr + value + 1;
}/* next_property */
/*
* unlink_object
*
* Unlink an object from its parent and siblings.
*
*/
static void unlink_object (zword object)
{
zword obj_addr;
zword parent_addr;
zword sibling_addr;
if (object == 0) {
runtime_error (ERR_REMOVE_OBJECT_0);
return;
}
obj_addr = object_address (object);
if (h_version <= V3) {
zbyte parent;
zbyte younger_sibling;
zbyte older_sibling;
zbyte zero = 0;
/* Get parent of object, and return if no parent */
obj_addr += O1_PARENT;
LOW_BYTE (obj_addr, parent)
if (!parent)
return;
/* Get (older) sibling of object and set both parent and sibling
pointers to 0 */
SET_BYTE (obj_addr, zero)
obj_addr += O1_SIBLING - O1_PARENT;
LOW_BYTE (obj_addr, older_sibling)
SET_BYTE (obj_addr, zero)
/* Get first child of parent (the youngest sibling of the object) */
parent_addr = object_address (parent) + O1_CHILD;
LOW_BYTE (parent_addr, younger_sibling)
/* Remove object from the list of siblings */
if (younger_sibling == object)
SET_BYTE (parent_addr, older_sibling)
else {
do {
sibling_addr = object_address (younger_sibling) + O1_SIBLING;
LOW_BYTE (sibling_addr, younger_sibling)
} while (younger_sibling != object);
SET_BYTE (sibling_addr, older_sibling)
}
} else {
zword parent;
zword younger_sibling;
zword older_sibling;
zword zero = 0;
/* Get parent of object, and return if no parent */
obj_addr += O4_PARENT;
LOW_WORD (obj_addr, parent)
if (!parent)
return;
/* Get (older) sibling of object and set both parent and sibling
pointers to 0 */
SET_WORD (obj_addr, zero)
obj_addr += O4_SIBLING - O4_PARENT;
LOW_WORD (obj_addr, older_sibling)
SET_WORD (obj_addr, zero)
/* Get first child of parent (the youngest sibling of the object) */
parent_addr = object_address (parent) + O4_CHILD;
LOW_WORD (parent_addr, younger_sibling)
/* Remove object from the list of siblings */
if (younger_sibling == object)
SET_WORD (parent_addr, older_sibling)
else {
do {
sibling_addr = object_address (younger_sibling) + O4_SIBLING;
LOW_WORD (sibling_addr, younger_sibling)
} while (younger_sibling != object);
SET_WORD (sibling_addr, older_sibling)
}
}
}/* unlink_object */
/*
* z_clear_attr, clear an object attribute.
*
* zargs[0] = object
* zargs[1] = number of attribute to be cleared
*
*/
void z_clear_attr (void)
{
zword obj_addr;
zbyte value;
if (story_id == SHERLOCK)
if (zargs[1] == 48)
return;
if (zargs[1] > ((h_version <= V3) ? 31 : 47))
runtime_error (ERR_ILL_ATTR);
/* If we are monitoring attribute assignment display a short note */
if (f_setup.attribute_assignment) {
stream_mssg_on ();
print_string ("@clear_attr ");
print_object (zargs[0]);
print_string (" ");
print_num (zargs[1]);
stream_mssg_off ();
}
if (zargs[0] == 0) {
runtime_error (ERR_CLEAR_ATTR_0);
return;
}
/* Get attribute address */
obj_addr = object_address (zargs[0]) + zargs[1] / 8;
/* Clear attribute bit */
LOW_BYTE (obj_addr, value)
value &= ~(0x80 >> (zargs[1] & 7));
SET_BYTE (obj_addr, value)
}/* z_clear_attr */
/*
* z_jin, branch if the first object is inside the second.
*
* zargs[0] = first object
* zargs[1] = second object
*
*/
void z_jin (void)
{
zword obj_addr;
/* If we are monitoring object locating display a short note */
if (f_setup.object_locating) {
stream_mssg_on ();
print_string ("@jin ");
print_object (zargs[0]);
print_string (" ");
print_object (zargs[1]);
stream_mssg_off ();
}
if (zargs[0] == 0) {
runtime_error (ERR_JIN_0);
branch (0 == zargs[1]);
return;
}
obj_addr = object_address (zargs[0]);
if (h_version <= V3) {
zbyte parent;
/* Get parent id from object */
obj_addr += O1_PARENT;
LOW_BYTE (obj_addr, parent)
/* Branch if the parent is obj2 */
branch (parent == zargs[1]);
} else {
zword parent;
/* Get parent id from object */
obj_addr += O4_PARENT;
LOW_WORD (obj_addr, parent)
/* Branch if the parent is obj2 */
branch (parent == zargs[1]);
}
}/* z_jin */
/*
* z_get_child, store the child of an object.
*
* zargs[0] = object
*
*/
void z_get_child (void)
{
zword obj_addr;
/* If we are monitoring object locating display a short note */
if (f_setup.object_locating) {
stream_mssg_on ();
print_string ("@get_child ");
print_object (zargs[0]);
stream_mssg_off ();
}
if (zargs[0] == 0) {
runtime_error (ERR_GET_CHILD_0);
store (0);
branch (FALSE);
return;
}
obj_addr = object_address (zargs[0]);
if (h_version <= V3) {
zbyte child;
/* Get child id from object */
obj_addr += O1_CHILD;
LOW_BYTE (obj_addr, child)
/* Store child id and branch */
store (child);
branch (child);
} else {
zword child;
/* Get child id from object */
obj_addr += O4_CHILD;
LOW_WORD (obj_addr, child)
/* Store child id and branch */
store (child);
branch (child);
}
}/* z_get_child */
/*
* z_get_next_prop, store the number of the first or next property.
*
* zargs[0] = object
* zargs[1] = address of current property (0 gets the first property)
*
*/
void z_get_next_prop (void)
{
zword prop_addr;
zbyte value;
zbyte mask;
if (zargs[0] == 0) {
runtime_error (ERR_GET_NEXT_PROP_0);
store (0);
return;
}
/* Property id is in bottom five (six) bits */
mask = (h_version <= V3) ? 0x1f : 0x3f;
/* Load address of first property */
prop_addr = first_property (zargs[0]);
if (zargs[1] != 0) {
/* Scan down the property list */
do {
LOW_BYTE (prop_addr, value)
prop_addr = next_property (prop_addr);
} while ((value & mask) > zargs[1]);
/* Exit if the property does not exist */
if ((value & mask) != zargs[1])
runtime_error (ERR_NO_PROP);
}
/* Return the property id */
LOW_BYTE (prop_addr, value)
store ((zword) (value & mask));
}/* z_get_next_prop */
/*
* z_get_parent, store the parent of an object.
*
* zargs[0] = object
*
*/
void z_get_parent (void)
{
zword obj_addr;
/* If we are monitoring object locating display a short note */
if (f_setup.object_locating) {
stream_mssg_on ();
print_string ("@get_parent ");
print_object (zargs[0]);
stream_mssg_off ();
}
if (zargs[0] == 0) {
runtime_error (ERR_GET_PARENT_0);
store (0);
return;
}
obj_addr = object_address (zargs[0]);
if (h_version <= V3) {
zbyte parent;
/* Get parent id from object */
obj_addr += O1_PARENT;
LOW_BYTE (obj_addr, parent)
/* Store parent */
store (parent);
} else {
zword parent;
/* Get parent id from object */
obj_addr += O4_PARENT;
LOW_WORD (obj_addr, parent)
/* Store parent */
store (parent);
}
}/* z_get_parent */
/*
* z_get_prop, store the value of an object property.
*
* zargs[0] = object
* zargs[1] = number of property to be examined
*
*/
void z_get_prop (void)
{
zword prop_addr;
zword wprop_val;
zbyte bprop_val;
zbyte value;
zbyte mask;
if (zargs[0] == 0) {
runtime_error (ERR_GET_PROP_0);
store (0);
return;
}
/* Property id is in bottom five (six) bits */
mask = (h_version <= V3) ? 0x1f : 0x3f;
/* Load address of first property */
prop_addr = first_property (zargs[0]);
/* Scan down the property list */
for (;;) {
LOW_BYTE (prop_addr, value)
if ((value & mask) <= zargs[1])
break;
prop_addr = next_property (prop_addr);
}
if ((value & mask) == zargs[1]) { /* property found */
/* Load property (byte or word sized) */
prop_addr++;
if ((h_version <= V3 && !(value & 0xe0)) || (h_version >= V4 && !(value & 0xc0))) {
LOW_BYTE (prop_addr, bprop_val)
wprop_val = bprop_val;
} else LOW_WORD (prop_addr, wprop_val)
} else { /* property not found */
/* Load default value */
prop_addr = h_objects + 2 * (zargs[1] - 1);
LOW_WORD (prop_addr, wprop_val)
}
/* Store the property value */
store (wprop_val);
}/* z_get_prop */
/*
* z_get_prop_addr, store the address of an object property.
*
* zargs[0] = object
* zargs[1] = number of property to be examined
*
*/
void z_get_prop_addr (void)
{
zword prop_addr;
zbyte value;
zbyte mask;
if (zargs[0] == 0) {
runtime_error (ERR_GET_PROP_ADDR_0);
store (0);
return;
}
if (story_id == BEYOND_ZORK)
if (zargs[0] > MAX_OBJECT)
{ store (0); return; }
/* Property id is in bottom five (six) bits */
mask = (h_version <= V3) ? 0x1f : 0x3f;
/* Load address of first property */
prop_addr = first_property (zargs[0]);
/* Scan down the property list */
for (;;) {
LOW_BYTE (prop_addr, value)
if ((value & mask) <= zargs[1])
break;
prop_addr = next_property (prop_addr);
}
/* Calculate the property address or return zero */
if ((value & mask) == zargs[1]) {
if (h_version >= V4 && (value & 0x80))
prop_addr++;
store ((zword) (prop_addr + 1));
} else store (0);
}/* z_get_prop_addr */
/*
* z_get_prop_len, store the length of an object property.
*
* zargs[0] = address of property to be examined
*
*/
void z_get_prop_len (void)
{
zword addr;
zbyte value;
/* Back up the property pointer to the property id */
addr = zargs[0] - 1;
LOW_BYTE (addr, value)
/* Calculate length of property */
if (h_version <= V3)
value = (value >> 5) + 1;
else if (!(value & 0x80))
value = (value >> 6) + 1;
else {
value &= 0x3f;
if (value == 0) value = 64; /* demanded by Spec 1.0 */
}
/* Store length of property */
store (value);
}/* z_get_prop_len */
/*
* z_get_sibling, store the sibling of an object.
*
* zargs[0] = object
*
*/
void z_get_sibling (void)
{
zword obj_addr;
if (zargs[0] == 0) {
runtime_error (ERR_GET_SIBLING_0);
store (0);
branch (FALSE);
return;
}
obj_addr = object_address (zargs[0]);
if (h_version <= V3) {
zbyte sibling;
/* Get sibling id from object */
obj_addr += O1_SIBLING;
LOW_BYTE (obj_addr, sibling)
/* Store sibling and branch */
store (sibling);
branch (sibling);
} else {
zword sibling;
/* Get sibling id from object */
obj_addr += O4_SIBLING;
LOW_WORD (obj_addr, sibling)
/* Store sibling and branch */
store (sibling);
branch (sibling);
}
}/* z_get_sibling */
/*
* z_insert_obj, make an object the first child of another object.
*
* zargs[0] = object to be moved
* zargs[1] = destination object
*
*/
void z_insert_obj (void)
{
zword obj1 = zargs[0];
zword obj2 = zargs[1];
zword obj1_addr;
zword obj2_addr;
/* If we are monitoring object movements display a short note */
if (f_setup.object_movement) {
stream_mssg_on ();
print_string ("@move_obj ");
print_object (obj1);
print_string (" ");
print_object (obj2);
stream_mssg_off ();
}
if (obj1 == 0) {
runtime_error (ERR_MOVE_OBJECT_0);
return;
}
if (obj2 == 0) {
runtime_error (ERR_MOVE_OBJECT_TO_0);
return;
}
/* Get addresses of both objects */
obj1_addr = object_address (obj1);
obj2_addr = object_address (obj2);
/* Remove object 1 from current parent */
unlink_object (obj1);
/* Make object 1 first child of object 2 */
if (h_version <= V3) {
zbyte child;
obj1_addr += O1_PARENT;
SET_BYTE (obj1_addr, obj2)
obj2_addr += O1_CHILD;
LOW_BYTE (obj2_addr, child)
SET_BYTE (obj2_addr, obj1)
obj1_addr += O1_SIBLING - O1_PARENT;
SET_BYTE (obj1_addr, child)
} else {
zword child;
obj1_addr += O4_PARENT;
SET_WORD (obj1_addr, obj2)
obj2_addr += O4_CHILD;
LOW_WORD (obj2_addr, child)
SET_WORD (obj2_addr, obj1)
obj1_addr += O4_SIBLING - O4_PARENT;
SET_WORD (obj1_addr, child)
}
}/* z_insert_obj */
/*
* z_put_prop, set the value of an object property.
*
* zargs[0] = object
* zargs[1] = number of property to set
* zargs[2] = value to set property to
*
*/
void z_put_prop (void)
{
zword prop_addr;
zword value;
zbyte mask;
if (zargs[0] == 0) {
runtime_error (ERR_PUT_PROP_0);
return;
}
/* Property id is in bottom five or six bits */
mask = (h_version <= V3) ? 0x1f : 0x3f;
/* Load address of first property */
prop_addr = first_property (zargs[0]);
/* Scan down the property list */
for (;;) {
LOW_BYTE (prop_addr, value)
if ((value & mask) <= zargs[1])
break;
prop_addr = next_property (prop_addr);
}
/* Exit if the property does not exist */
if ((value & mask) != zargs[1])
runtime_error (ERR_NO_PROP);
/* Store the new property value (byte or word sized) */
prop_addr++;
if ((h_version <= V3 && !(value & 0xe0)) || (h_version >= V4 && !(value & 0xc0))) {
zbyte v = zargs[2];
SET_BYTE (prop_addr, v)
} else {
zword v = zargs[2];
SET_WORD (prop_addr, v)
}
}/* z_put_prop */
/*
* z_remove_obj, unlink an object from its parent and siblings.
*
* zargs[0] = object
*
*/
void z_remove_obj (void)
{
/* If we are monitoring object movements display a short note */
if (f_setup.object_movement) {
stream_mssg_on ();
print_string ("@remove_obj ");
print_object (zargs[0]);
stream_mssg_off ();
}
/* Call unlink_object to do the job */
unlink_object (zargs[0]);
}/* z_remove_obj */
/*
* z_set_attr, set an object attribute.
*
* zargs[0] = object
* zargs[1] = number of attribute to set
*
*/
void z_set_attr (void)
{
zword obj_addr;
zbyte value;
if (story_id == SHERLOCK)
if (zargs[1] == 48)
return;
if (zargs[1] > ((h_version <= V3) ? 31 : 47))
runtime_error (ERR_ILL_ATTR);
/* If we are monitoring attribute assignment display a short note */
if (f_setup.attribute_assignment) {
stream_mssg_on ();
print_string ("@set_attr ");
print_object (zargs[0]);
print_string (" ");
print_num (zargs[1]);
stream_mssg_off ();
}
if (zargs[0] == 0) {
runtime_error (ERR_SET_ATTR_0);
return;
}
/* Get attribute address */
obj_addr = object_address (zargs[0]) + zargs[1] / 8;
/* Load attribute byte */
LOW_BYTE (obj_addr, value)
/* Set attribute bit */
value |= 0x80 >> (zargs[1] & 7);
/* Store attribute byte */
SET_BYTE (obj_addr, value)
}/* z_set_attr */
/*
* z_test_attr, branch if an object attribute is set.
*
* zargs[0] = object
* zargs[1] = number of attribute to test
*
*/
void z_test_attr (void)
{
zword obj_addr;
zbyte value;
if (zargs[1] > ((h_version <= V3) ? 31 : 47))
runtime_error (ERR_ILL_ATTR);
/* If we are monitoring attribute testing display a short note */
if (f_setup.attribute_testing) {
stream_mssg_on ();
print_string ("@test_attr ");
print_object (zargs[0]);
print_string (" ");
print_num (zargs[1]);
stream_mssg_off ();
}
if (zargs[0] == 0) {
runtime_error (ERR_TEST_ATTR_0);
branch (FALSE);
return;
}
/* Get attribute address */
obj_addr = object_address (zargs[0]) + zargs[1] / 8;
/* Load attribute byte */
LOW_BYTE (obj_addr, value)
/* Test attribute */
branch (value & (0x80 >> (zargs[1] & 7)));
}/* z_test_attr */
|