/* ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com ** ** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ** ** Any non-GPL usage of this software or parts of this software is strictly ** forbidden. ** ** Commercial non-GPL licensing of this software is possible. ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. ** ** $Id$ **/ /* Binary search huffman table HCB_3 */ static hcb_bin_quad hcb3[] = { { /* 0 */ 0, { 1, 2, 0, 0 } }, { /* 1 */ 1, { 0, 0, 0, 0 } }, /* 0 */ { /* 2 */ 0, { 1, 2, 0, 0 } }, { /* 3 */ 0, { 2, 3, 0, 0 } }, { /* 4 */ 0, { 3, 4, 0, 0 } }, { /* 5 */ 0, { 4, 5, 0, 0 } }, { /* 6 */ 0, { 5, 6, 0, 0 } }, { /* 7 */ 0, { 6, 7, 0, 0 } }, { /* 8 */ 0, { 7, 8, 0, 0 } }, { /* 9 */ 1, { 1, 0, 0, 0 } }, /* 1000 */ { /* 10 */ 1, { 0, 0, 0, 1 } }, /* 1001 */ { /* 11 */ 1, { 0, 1, 0, 0 } }, /* 1010 */ { /* 12 */ 1, { 0, 0, 1, 0 } }, /* 1011 */ { /* 13 */ 0, { 4, 5, 0, 0 } }, { /* 14 */ 0, { 5, 6, 0, 0 } }, { /* 15 */ 0, { 6, 7, 0, 0 } }, { /* 16 */ 0, { 7, 8, 0, 0 } }, { /* 17 */ 1, { 1, 1, 0, 0 } }, { /* 18 */ 1, { 0, 0, 1, 1 } }, { /* 19 */ 0, { 6, 7, 0, 0 } }, { /* 20 */ 0, { 7, 8, 0, 0 } }, { /* 21 */ 0, { 8, 9, 0, 0 } }, { /* 22 */ 0, { 9, 10, 0, 0 } }, { /* 23 */ 0, { 10, 11, 0, 0 } }, { /* 24 */ 0, { 11, 12, 0, 0 } }, { /* 25 */ 1, { 0, 1, 1, 0 } }, /* 110100 */ { /* 26 */ 1, { 0, 1, 0, 1 } }, /* 110101 */ { /* 27 */ 1, { 1, 0, 1, 0 } }, /* 110110 */ { /* 28 */ 1, { 0, 1, 1, 1 } }, /* 110111 */ { /* 29 */ 1, { 1, 0, 0, 1 } }, /* 111000 */ { /* 30 */ 1, { 1, 1, 1, 0 } }, /* 111001 */ { /* 31 */ 0, { 6, 7, 0, 0 } }, { /* 32 */ 0, { 7, 8, 0, 0 } }, { /* 33 */ 0, { 8, 9, 0, 0 } }, { /* 34 */ 0, { 9, 10, 0, 0 } }, { /* 35 */ 0, { 10, 11, 0, 0 } }, { /* 36 */ 0, { 11, 12, 0, 0 } }, { /* 37 */ 1, { 1, 1, 1, 1 } }, /* 1110100 */ { /* 38 */ 1, { 1, 0, 1, 1 } }, /* 1110101 */ { /* 39 */ 1, { 1, 1, 0, 1 } }, /* 1110110 */ { /* 40 */ 0, { 9, 10, 0, 0 } }, { /* 41 */ 0, { 10, 11, 0, 0 } }, { /* 42 */ 0, { 11, 12, 0, 0 } }, { /* 43 */ 0, { 12, 13, 0, 0 } }, { /* 44 */ 0, { 13, 14, 0, 0 } }, { /* 45 */ 0, { 14, 15, 0, 0 } }, { /* 46 */ 0, { 15, 16, 0, 0 } }, { /* 47 */ 0, { 16, 17, 0, 0 } }, { /* 48 */ 0, { 17, 18, 0, 0 } }, { /* 49 */ 1, { 2, 0, 0, 0 } }, /* 11101110 */ { /* 50 */ 1, { 0, 0, 0, 2 } }, /* 11101111 */ { /* 51 */ 1, { 0, 0, 1, 2 } }, /* 11110000 */ { /* 52 */ 1, { 2, 1, 0, 0 } }, /* 11110001 */ { /* 53 */ 1, { 1, 2, 1, 0 } }, /* 11110010 */ { /* 54 */ 0, { 13, 14, 0, 0 } }, { /* 55 */ 0, { 14, 15, 0, 0 } }, { /* 56 */ 0, { 15, 16, 0, 0 } }, { /* 57 */ 0, { 16, 17, 0, 0 } }, { /* 58 */ 0, { 17, 18, 0, 0 } }, { /* 59 */ 0, { 18, 19, 0, 0 } }, { /* 60 */ 0, { 19, 20, 0, 0 } }, { /* 61 */ 0, { 20, 21, 0, 0 } }, { /* 62 */ 0, { 21, 22, 0, 0 } }, { /* 63 */ 0, { 22, 23, 0, 0 } }, { /* 64 */ 0, { 23, 24, 0, 0 } }, { /* 65 */ 0, { 24, 25, 0, 0 } }, { /* 66 */ 0, { 25, 26, 0, 0 } }, { /* 67 */ 1, { 0, 0, 2, 1 } }, { /* 68 */ 1, { 0, 1, 2, 1 } }, { /* 69 */ 1, { 1, 2, 0, 0 } }, { /* 70 */ 1, { 0, 1, 1, 2 } }, { /* 71 */ 1, { 2, 1, 1, 0 } }, { /* 72 */ 1, { 0, 0, 2, 0 } }, { /* 73 */ 1, { 0, 2, 1, 0 } }, { /* 74 */ 1, { 0, 1, 2, 0 } }, { /* 75 */ 1, { 0, 2, 0, 0 } }, { /* 76 */ 1, { 0, 1, 0, 2 } }, { /* 77 */ 1, { 2, 0, 1, 0 } }, { /* 78 */ 1, { 1, 2, 1, 1 } }, { /* 79 */ 1, { 0, 2, 1, 1 } }, { /* 80 */ 1, { 1, 1, 2, 0 } }, { /* 81 */ 1, { 1, 1, 2, 1 } }, { /* 82 */ 0, { 11, 12, 0, 0 } }, { /* 83 */ 0, { 12, 13, 0, 0 } }, { /* 84 */ 0, { 13, 14, 0, 0 } }, { /* 85 */ 0, { 14, 15, 0, 0 } }, { /* 86 */ 0, { 15, 16, 0, 0 } }, { /* 87 */ 0, { 16, 17, 0, 0 } }, { /* 88 */ 0, { 17, 18, 0, 0 } }, { /* 89 */ 0, { 18, 19, 0, 0 } }, { /* 90 */ 0, { 19, 20, 0, 0 } }, { /* 91 */ 0, { 20, 21, 0, 0 } }, { /* 92 */ 0, { 21, 22, 0, 0 } }, { /* 93 */ 1, { 1, 2, 0, 1 } }, /* 1111101010 */ { /* 94 */ 1, { 1, 0, 2, 0 } }, /* 1111101011 */ { /* 95 */ 1, { 1, 0, 2, 1 } }, /* 1111101100 */ { /* 96 */ 1, { 0, 2, 0, 1 } }, /* 1111101101 */ { /* 97 */ 1, { 2, 1, 1, 1 } }, /* 1111101110 */ { /* 98 */ 1, { 1, 1, 1, 2 } }, /* 1111101111 */ { /* 99 */ 1, { 2, 1, 0, 1 } }, /* 1111110000 */ { /* 00 */ 1, { 1, 0, 1, 2 } }, /* 1111110001 */ { /* 01 */ 1, { 0, 0, 2, 2 } }, /* 1111110010 */ { /* 02 */ 1, { 0, 1, 2, 2 } }, /* 1111110011 */ { /* 03 */ 1, { 2, 2, 1, 0 } }, /* 1111110100 */ { /* 04 */ 1, { 1, 2, 2, 0 } }, /* 1111110101 */ { /* 05 */ 1, { 1, 0, 0, 2 } }, /* 1111110110 */ { /* 06 */ 1, { 2, 0, 0, 1 } }, /* 1111110111 */ { /* 07 */ 1, { 0, 2, 2, 1 } }, /* 1111111000 */ { /* 08 */ 0, { 7, 8, 0, 0 } }, { /* 09 */ 0, { 8, 9, 0, 0 } }, { /* 10 */ 0, { 9, 10, 0, 0 } }, { /* 11 */ 0, { 10, 11, 0, 0 } }, { /* 12 */ 0, { 11, 12, 0, 0 } }, { /* 13 */ 0, { 12, 13, 0, 0 } }, { /* 14 */ 0, { 13, 14, 0, 0 } }, { /* 15 */ 1, { 2, 2, 0, 0 } }, /* 11111110010 */ { /* 16 */ 1, { 1, 2, 2, 1 } }, /* 11111110011 */ { /* 17 */ 1, { 1, 1, 0, 2 } }, /* 11111110100 */ { /* 18 */ 1, { 2, 0, 1, 1 } }, /* 11111110101 */ { /* 19 */ 1, { 1, 1, 2, 2 } }, /* 11111110110 */ { /* 20 */ 1, { 2, 2, 1, 1 } }, /* 11111110111 */ { /* 21 */ 1, { 0, 2, 2, 0 } }, /* 11111111000 */ { /* 22 */ 1, { 0, 2, 1, 2 } }, /* 11111111001 */ { /* 23 */ 0, { 6, 7, 0, 0 } }, { /* 24 */ 0, { 7, 8, 0, 0 } }, { /* 25 */ 0, { 8, 9, 0, 0 } }, { /* 26 */ 0, { 9, 10, 0, 0 } }, { /* 27 */ 0, { 10, 11, 0, 0 } }, { /* 28 */ 0, { 11, 12, 0, 0 } }, { /* 29 */ 1, { 1, 0, 2, 2 } }, /* 111111110100 */ { /* 30 */ 1, { 2, 2, 0, 1 } }, /* 111111110101 */ { /* 31 */ 1, { 2, 1, 2, 0 } }, /* 111111110110 */ { /* 32 */ 1, { 2, 2, 2, 0 } }, /* 111111110111 */ { /* 33 */ 1, { 0, 2, 2, 2 } }, /* 111111111000 */ { /* 34 */ 1, { 2, 2, 2, 1 } }, /* 111111111001 */ { /* 35 */ 1, { 2, 1, 2, 1 } }, /* 111111111010 */ { /* 36 */ 1, { 1, 2, 1, 2 } }, /* 111111111011 */ { /* 37 */ 1, { 1, 2, 2, 2 } }, /* 111111111100 */ { /* 38 */ 0, { 3, 4, 0, 0 } }, { /* 39 */ 0, { 4, 5, 0, 0 } }, { /* 40 */ 0, { 5, 6, 0, 0 } }, { /* 41 */ 1, { 0, 2, 0, 2 } }, /* 1111111111010 */ { /* 42 */ 1, { 2, 0, 2, 0 } }, /* 1111111111011 */ { /* 43 */ 1, { 1, 2, 0, 2 } }, /* 1111111111100 */ { /* 44 */ 0, { 3, 4, 0, 0 } }, { /* 45 */ 0, { 4, 5, 0, 0 } }, { /* 46 */ 0, { 5, 6, 0, 0 } }, { /* 47 */ 1, { 2, 0, 2, 1 } }, /* 11111111111010 */ { /* 48 */ 1, { 2, 1, 1, 2 } }, /* 11111111111011 */ { /* 49 */ 1, { 2, 1, 0, 2 } }, /* 11111111111100 */ { /* 50 */ 0, { 3, 4, 0, 0 } }, { /* 51 */ 0, { 4, 5, 0, 0 } }, { /* 52 */ 0, { 5, 6, 0, 0 } }, { /* 53 */ 1, { 2, 2, 2, 2 } }, /* 111111111111010 */ { /* 54 */ 1, { 2, 2, 1, 2 } }, /* 111111111111011 */ { /* 55 */ 1, { 2, 1, 2, 2 } }, /* 111111111111100 */ { /* 56 */ 1, { 2, 0, 1, 2 } }, /* 111111111111101 */ { /* 57 */ 1, { 2, 0, 0, 2 } }, /* 111111111111110 */ { /* 58 */ 0, { 1, 2, 0, 0 } }, { /* 59 */ 1, { 2, 2, 0, 2 } }, /* 1111111111111110 */ { /* 60 */ 1, { 2, 0, 2, 2 } } /* 1111111111111111 */ }; f='#n166'>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
/* Emacs style mode select   -*- C++ -*-
 *-----------------------------------------------------------------------------
 *
 *
 *  PrBoom a Doom port merged with LxDoom and LSDLDoom
 *  based on BOOM, a modified and improved DOOM engine
 *  Copyright (C) 1999 by
 *  id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
 *  Copyright (C) 1999-2000 by
 *  Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA
 *  02111-1307, USA.
 *
 * DESCRIPTION:
 *      Archiving: SaveGame I/O.
 *
 *-----------------------------------------------------------------------------*/

#include "doomstat.h"
#include "r_main.h"
#include "p_maputl.h"
#include "p_spec.h"
#include "p_tick.h"
#include "p_saveg.h"
#include "m_random.h"
#include "am_map.h"
#include "p_enemy.h"
#include "i_system.h"
#include "rockmacros.h"
byte *save_p;

// Pads save_p to a 4-byte boundary
//  so that the load/save works on SGI&Gecko.
#define PADSAVEP()    do { save_p += (4 - ((unsigned long) save_p & 3)) & 3; } while (0)
//
// P_ArchivePlayers
//
void P_ArchivePlayers (void)
{
   int i;

   CheckSaveGame(sizeof(player_t) * MAXPLAYERS); // killough
   for (i=0 ; i<MAXPLAYERS ; i++)
      if (playeringame[i])
      {
         int      j;
         player_t *dest;

         PADSAVEP();
         dest = (player_t *) save_p;
         memcpy(dest, &players[i], sizeof(player_t));
         save_p += sizeof(player_t);
         for (j=0; j<NUMPSPRITES; j++)
            if (dest->psprites[j].state)
               dest->psprites[j].state =
                  (state_t *)(dest->psprites[j].state-states);
      }
}

//
// P_UnArchivePlayers
//
void P_UnArchivePlayers (void)
{
   int i;

   for (i=0 ; i<MAXPLAYERS ; i++)
      if (playeringame[i])
      {
         int j;

         PADSAVEP();

         memcpy(&players[i], save_p, sizeof(player_t));
         save_p += sizeof(player_t);

         // will be set when unarc thinker
         players[i].mo = NULL;
         players[i].message = NULL;
         players[i].attacker = NULL;

         for (j=0 ; j<NUMPSPRITES ; j++)
            if (players[i]. psprites[j].state)
               players[i]. psprites[j].state =
                  &states[ (unsigned long)players[i].psprites[j].state ];
      }
}


//
// P_ArchiveWorld
//
void P_ArchiveWorld (void)
{
   int            i;
   const sector_t *sec;
   const line_t   *li;
   const side_t   *si;
   short          *put;

   // killough 3/22/98: fix bug caused by hoisting save_p too early
   // killough 10/98: adjust size for changes below
   size_t size =
      (sizeof(short)*5 + sizeof sec->floorheight + sizeof sec->ceilingheight)
      * numsectors + sizeof(short)*3*numlines + 4;

   for (i=0; i<numlines; i++)
   {
      if (lines[i].sidenum[0] != -1)
         size +=
            sizeof(short)*3 + sizeof si->textureoffset + sizeof si->rowoffset;
      if (lines[i].sidenum[1] != -1)
         size +=
            sizeof(short)*3 + sizeof si->textureoffset + sizeof si->rowoffset;
   }

   CheckSaveGame(size); // killough

   PADSAVEP();                // killough 3/22/98

   put = (short *)save_p;

   // do sectors
   for (i=0, sec = sectors ; i<numsectors ; i++,sec++)
   {
      // killough 10/98: save full floor & ceiling heights, including fraction
      memcpy(put, &sec->floorheight, sizeof sec->floorheight);
      put = (void *)((char *) put + sizeof sec->floorheight);
      memcpy(put, &sec->ceilingheight, sizeof sec->ceilingheight);
      put = (void *)((char *) put + sizeof sec->ceilingheight);

      *put++ = sec->floorpic;
      *put++ = sec->ceilingpic;
      *put++ = sec->lightlevel;
      *put++ = sec->special;            // needed?   yes -- transfer types
      *put++ = sec->tag;                // needed?   need them -- killough
   }

   // do lines
   for (i=0, li = lines ; i<numlines ; i++,li++)
   {
      int j;

      *put++ = li->flags;
      *put++ = li->special;
      *put++ = li->tag;

      for (j=0; j<2; j++)
         if (li->sidenum[j] != -1)
         {
            si = &sides[li->sidenum[j]];

            // killough 10/98: save full sidedef offsets,
            // preserving fractional scroll offsets

            memcpy(put, &si->textureoffset, sizeof si->textureoffset);
            put = (void *)((char *) put + sizeof si->textureoffset);
            memcpy(put, &si->rowoffset, sizeof si->rowoffset);
            put = (void *)((char *) put + sizeof si->rowoffset);

            *put++ = si->toptexture;
            *put++ = si->bottomtexture;
            *put++ = si->midtexture;
         }
   }
   save_p = (byte *) put;
}



//
// P_UnArchiveWorld
//
void P_UnArchiveWorld (void)
{
   int          i;
   sector_t     *sec;
   line_t       *li;
   const short  *get;

   PADSAVEP();                // killough 3/22/98

   get = (short *) save_p;

   // do sectors
   for (i=0, sec = sectors ; i<numsectors ; i++,sec++)
   {
      // killough 10/98: load full floor & ceiling heights, including fractions

      memcpy(&sec->floorheight, get, sizeof sec->floorheight);
      get = (void *)((char *) get + sizeof sec->floorheight);
      memcpy(&sec->ceilingheight, get, sizeof sec->ceilingheight);
      get = (void *)((char *) get + sizeof sec->ceilingheight);

      sec->floorpic = *get++;
      sec->ceilingpic = *get++;
      sec->lightlevel = *get++;
      sec->special = *get++;
      sec->tag = *get++;
      sec->ceilingdata = 0; //jff 2/22/98 now three thinker fields, not two
      sec->floordata = 0;
      sec->lightingdata = 0;
      sec->soundtarget = 0;
   }

   // do lines
   for (i=0, li = lines ; i<numlines ; i++,li++)
   {
      int j;

      li->flags = *get++;
      li->special = *get++;
      li->tag = *get++;
      for (j=0 ; j<2 ; j++)
         if (li->sidenum[j] != -1)
         {
            side_t *si = &sides[li->sidenum[j]];

            // killough 10/98: load full sidedef offsets, including fractions

            memcpy(&si->textureoffset, get, sizeof si->textureoffset);
            get = (void *)((char *) get + sizeof si->textureoffset);
            memcpy(&si->rowoffset, get, sizeof si->rowoffset);
            get = (void *)((char *) get + sizeof si->rowoffset);

            si->toptexture = *get++;
            si->bottomtexture = *get++;
            si->midtexture = *get++;
         }
   }
   save_p = (byte *) get;
}

//
// Thinkers
//

enum {
   tc_end,
   tc_mobj
};
typedef unsigned thinkerclass_t;

// phares 9/13/98: Moved this code outside of P_ArchiveThinkers so the
// thinker indices could be used by the code that saves sector info.

static int number_of_thinkers;

void P_ThinkerToIndex(void)
{
   thinker_t *th;

   // killough 2/14/98:
   // count the number of thinkers, and mark each one with its index, using
   // the prev field as a placeholder, since it can be restored later.

   number_of_thinkers = 0;
   for (th = thinkercap.next ; th != &thinkercap ; th=th->next)
      if (th->function == P_MobjThinker)
         th->prev = (thinker_t *)(long)(++number_of_thinkers);
}

// phares 9/13/98: Moved this code outside of P_ArchiveThinkers so the
// thinker indices could be used by the code that saves sector info.

void P_IndexToThinker(void)
{
   // killough 2/14/98: restore prev pointers
   thinker_t *th;
   thinker_t *prev = &thinkercap;

   for (th = thinkercap.next ; th != &thinkercap ; prev=th, th=th->next)
      th->prev = prev;
}

//
// P_ArchiveThinkers
//
// 2/14/98 killough: substantially modified to fix savegame bugs

void P_ArchiveThinkers (void)
{
   thinker_t *th;

   CheckSaveGame(sizeof brain);      // killough 3/26/98: Save boss brain state
   memcpy(save_p, &brain, sizeof brain);
   save_p += sizeof brain;

   /* check that enough room is available in savegame buffer
    * - killough 2/14/98
    * cph - use number_of_thinkers saved by P_ThinkerToIndex above
    */
   CheckSaveGame(number_of_thinkers*(sizeof(mobj_t)+4));

   // save off the current thinkers
   for (th = thinkercap.next ; th != &thinkercap ; th=th->next)
      if (th->function == P_MobjThinker)
      {
         mobj_t *mobj;

         *save_p++ = tc_mobj;
         PADSAVEP();
         mobj = (mobj_t *)save_p;
         memcpy (mobj, th, sizeof(*mobj));
         save_p += sizeof(*mobj);
         mobj->state = (state_t *)(mobj->state - states);

         // killough 2/14/98: convert pointers into indices.
         // Fixes many savegame problems, by properly saving
         // target and tracer fields. Note: we store NULL if
         // the thinker pointed to by these fields is not a
         // mobj thinker.

         if (mobj->target)
            mobj->target = mobj->target->thinker.function ==
                           P_MobjThinker ?
                           (mobj_t *) mobj->target->thinker.prev : NULL;

         if (mobj->tracer)
            mobj->tracer = mobj->tracer->thinker.function ==
                           P_MobjThinker ?
                           (mobj_t *) mobj->tracer->thinker.prev : NULL;

         // killough 2/14/98: new field: save last known enemy. Prevents
         // monsters from going to sleep after killing monsters and not
         // seeing player anymore.

         if (mobj->lastenemy)
            mobj->lastenemy = mobj->lastenemy->thinker.function ==
                              P_MobjThinker ?
                              (mobj_t *) mobj->lastenemy->thinker.prev : NULL;

         // killough 2/14/98: end changes

         if (mobj->above_thing)                                      // phares
            mobj->above_thing = mobj->above_thing->thinker.function ==
                                P_MobjThinker ?
                                (mobj_t *) mobj->above_thing->thinker.prev : NULL;

         if (mobj->below_thing)
            mobj->below_thing = mobj->below_thing->thinker.function ==
                                P_MobjThinker ?
                                (mobj_t *) mobj->below_thing->thinker.prev : NULL;      // phares

         if (mobj->player)
            mobj->player = (player_t *)((mobj->player-players) + 1);
      }

   // add a terminating marker
   *save_p++ = tc_end;

   // killough 9/14/98: save soundtargets
   {
      int i;
      CheckSaveGame(numsectors * sizeof(mobj_t *));       // killough 9/14/98
      for (i = 0; i < numsectors; i++)
      {
         mobj_t *target = sectors[i].soundtarget;
         if (target)
            target = (mobj_t *) target->thinker.prev;
         memcpy(save_p, &target, sizeof target);
         save_p += sizeof target;
      }
   }
}

/*
 * killough 11/98
 *
 * Same as P_SetTarget() in p_tick.c, except that the target is nullified
 * first, so that no old target's reference count is decreased (when loading
 * savegames, old targets are indices, not really pointers to targets).
 */

static void P_SetNewTarget(mobj_t **mop, mobj_t *targ)
{
   *mop = NULL;
   P_SetTarget(mop, targ);
}

//
// P_UnArchiveThinkers
//
// 2/14/98 killough: substantially modified to fix savegame bugs
//

void P_UnArchiveThinkers (void)
{
   thinker_t *th;
   mobj_t    **mobj_p;    // killough 2/14/98: Translation table
   size_t    size;        // killough 2/14/98: size of or index into table

   totallive = 0;
   // killough 3/26/98: Load boss brain state
   memcpy(&brain, save_p, sizeof brain);
   save_p += sizeof brain;

   // remove all the current thinkers
   for (th = thinkercap.next; th != &thinkercap; )
   {
      thinker_t *next = th->next;
      if (th->function == P_MobjThinker)
         P_RemoveMobj ((mobj_t *) th);
      else
         Z_Free (th);
      th = next;
   }
   P_InitThinkers ();

   // killough 2/14/98: count number of thinkers by skipping through them
   {
      byte *sp = save_p;     // save pointer and skip header
      for (size = 1; *save_p++ == tc_mobj; size++)  // killough 2/14/98
      {                     // skip all entries, adding up count
         PADSAVEP();
         save_p += sizeof(mobj_t);
      }

      if (*--save_p != tc_end)
         I_Error ("P_UnArchiveThinkers: Unknown tclass %i in savegame", *save_p);

      // first table entry special: 0 maps to NULL
      *(mobj_p = malloc(size * sizeof *mobj_p)) = 0;   // table of pointers
      save_p = sp;           // restore save pointer
   }

   // read in saved thinkers
   for (size = 1; *save_p++ == tc_mobj; size++)    // killough 2/14/98
   {
      mobj_t *mobj = Z_Malloc(sizeof(mobj_t), PU_LEVEL, NULL);

      // killough 2/14/98 -- insert pointers to thinkers into table, in order:
      mobj_p[size] = mobj;

      PADSAVEP();
      memcpy (mobj, save_p, sizeof(mobj_t));
      save_p += sizeof(mobj_t);
      mobj->state = states + (unsigned long) mobj->state;

      if (mobj->player)
         (mobj->player = &players[(unsigned long) mobj->player - 1]) -> mo = mobj;

      P_SetThingPosition (mobj);
      mobj->info = &mobjinfo[mobj->type];

      // killough 2/28/98:
      // Fix for falling down into a wall after savegame loaded:
      //      mobj->floorz = mobj->subsector->sector->floorheight;
      //      mobj->ceilingz = mobj->subsector->sector->ceilingheight;

      mobj->thinker.function = P_MobjThinker;
      P_AddThinker (&mobj->thinker);

      if (!((mobj->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL | MF_CORPSE)))
         totallive++;
   }

   // killough 2/14/98: adjust target and tracer fields, plus
   // lastenemy field, to correctly point to mobj thinkers.
   // NULL entries automatically handled by first table entry.
   //
   // killough 11/98: use P_SetNewTarget() to set fields

   for (th = thinkercap.next ; th != &thinkercap ; th=th->next)
   {
      P_SetNewTarget(&((mobj_t *) th)->target,
                     mobj_p[(size_t)((mobj_t *)th)->target]);

      P_SetNewTarget(&((mobj_t *) th)->tracer,
                     mobj_p[(size_t)((mobj_t *)th)->tracer]);

      P_SetNewTarget(&((mobj_t *) th)->lastenemy,
                     mobj_p[(size_t)((mobj_t *)th)->lastenemy]);

      // phares: added two new fields for Sprite Height problem

      P_SetNewTarget(&((mobj_t *) th)->above_thing,
                     mobj_p[(size_t)((mobj_t *)th)->above_thing]);

      P_SetNewTarget(&((mobj_t *) th)->below_thing,
                     mobj_p[(size_t)((mobj_t *)th)->below_thing]);
   }

   {  // killough 9/14/98: restore soundtargets
      int i;
      for (i = 0; i < numsectors; i++)
      {
         mobj_t *target;
         memcpy(&target, save_p, sizeof target);
         save_p += sizeof target;
         P_SetNewTarget(&sectors[i].soundtarget, mobj_p[(size_t) target]);
      }
   }

   free(mobj_p);    // free translation table

   // killough 3/26/98: Spawn icon landings:
   if (gamemode == commercial)
      P_SpawnBrainTargets();
}

//
// P_ArchiveSpecials
//
enum {
   tc_ceiling,
   tc_door,
   tc_floor,
   tc_plat,
   tc_flash,
   tc_strobe,
   tc_glow,
   tc_elevator,    //jff 2/22/98 new elevator type thinker
   tc_scroll,      // killough 3/7/98: new scroll effect thinker
   tc_pusher,      // phares 3/22/98:  new push/pull effect thinker
   tc_flicker,     // killough 10/4/98
   tc_endspecials
};
unsigned specials_e;

//
// Things to handle:
//
// T_MoveCeiling, (ceiling_t: sector_t * swizzle), - active list
// T_VerticalDoor, (vldoor_t: sector_t * swizzle),
// T_MoveFloor, (floormove_t: sector_t * swizzle),
// T_LightFlash, (lightflash_t: sector_t * swizzle),
// T_StrobeFlash, (strobe_t: sector_t *),
// T_Glow, (glow_t: sector_t *),
// T_PlatRaise, (plat_t: sector_t *), - active list
// T_MoveElevator, (plat_t: sector_t *), - active list      // jff 2/22/98
// T_Scroll                                                 // killough 3/7/98
// T_Pusher                                                 // phares 3/22/98
// T_FireFlicker                                            // killough 10/4/98
//

void P_ArchiveSpecials (void)
{
   thinker_t *th;
   size_t    size = 0;          // killough

   // save off the current thinkers (memory size calculation -- killough)

   for (th = thinkercap.next ; th != &thinkercap ; th=th->next)
      if (!th->function)
      {
         platlist_t *pl;
         ceilinglist_t *cl;     //jff 2/22/98 need this for ceilings too now
         for (pl=activeplats; pl; pl=pl->next)
            if (pl->plat == (plat_t *) th)   // killough 2/14/98
            {
               size += 4+sizeof(plat_t);
               goto end;
            }
         for (cl=activeceilings; cl; cl=cl->next) // search for activeceiling
            if (cl->ceiling == (ceiling_t *) th)   //jff 2/22/98
            {
               size += 4+sizeof(ceiling_t);
               goto end;
            }
end:;
      }
      else
         size +=
            th->function==T_MoveCeiling  ? 4+sizeof(ceiling_t) :
            th->function==T_VerticalDoor ? 4+sizeof(vldoor_t)  :
            th->function==T_MoveFloor    ? 4+sizeof(floormove_t):
            th->function==T_PlatRaise    ? 4+sizeof(plat_t)    :
            th->function==T_LightFlash   ? 4+sizeof(lightflash_t):
            th->function==T_StrobeFlash  ? 4+sizeof(strobe_t)  :
            th->function==T_Glow         ? 4+sizeof(glow_t)    :
            th->function==T_MoveElevator ? 4+sizeof(elevator_t):
            th->function==T_Scroll       ? 4+sizeof(scroll_t)  :
            th->function==T_Pusher       ? 4+sizeof(pusher_t)  :
            th->function==T_FireFlicker? 4+sizeof(fireflicker_t) :
            0;

   CheckSaveGame(size);          // killough

   // save off the current thinkers
   for (th=thinkercap.next; th!=&thinkercap; th=th->next)
   {
      if (!th->function)
      {
         platlist_t *pl;
         ceilinglist_t *cl;    //jff 2/22/98 add iter variable for ceilings

         // killough 2/8/98: fix plat original height bug.
         // Since acv==NULL, this could be a plat in stasis.
         // so check the active plats list, and save this
         // plat (jff: or ceiling) even if it is in stasis.

         for (pl=activeplats; pl; pl=pl->next)
            if (pl->plat == (plat_t *) th)      // killough 2/14/98
               goto plat;

         for (cl=activeceilings; cl; cl=cl->next)
            if (cl->ceiling == (ceiling_t *) th)      //jff 2/22/98
               goto ceiling;

         continue;
      }

      if (th->function == T_MoveCeiling)
      {
         ceiling_t *ceiling;
ceiling:                               // killough 2/14/98
         *save_p++ = tc_ceiling;
         PADSAVEP();
         ceiling = (ceiling_t *)save_p;
         memcpy (ceiling, th, sizeof(*ceiling));
         save_p += sizeof(*ceiling);
         ceiling->sector = (sector_t *)(ceiling->sector - sectors);
         continue;
      }

      if (th->function == T_VerticalDoor)
      {
         vldoor_t *door;
         *save_p++ = tc_door;
         PADSAVEP();
         door = (vldoor_t *) save_p;
         memcpy (door, th, sizeof *door);
         save_p += sizeof(*door);
         door->sector = (sector_t *)(door->sector - sectors);
         //jff 1/31/98 archive line remembered by door as well
         door->line = (line_t *) (door->line ? door->line-lines : -1);
         continue;
      }

      if (th->function == T_MoveFloor)
      {
         floormove_t *floor;
         *save_p++ = tc_floor;
         PADSAVEP();
         floor = (floormove_t *)save_p;
         memcpy (floor, th, sizeof(*floor));
         save_p += sizeof(*floor);
         floor->sector = (sector_t *)(floor->sector - sectors);
         continue;
      }

      if (th->function == T_PlatRaise)
      {
         plat_t *plat;
plat:   // killough 2/14/98: added fix for original plat height above
         *save_p++ = tc_plat;
         PADSAVEP();
         plat = (plat_t *)save_p;
         memcpy (plat, th, sizeof(*plat));
         save_p += sizeof(*plat);
         plat->sector = (sector_t *)(plat->sector - sectors);
         continue;
      }

      if (th->function == T_LightFlash)
      {
         lightflash_t *flash;
         *save_p++ = tc_flash;
         PADSAVEP();
         flash = (lightflash_t *)save_p;
         memcpy (flash, th, sizeof(*flash));
         save_p += sizeof(*flash);
         flash->sector = (sector_t *)(flash->sector - sectors);
         continue;
      }

      if (th->function == T_StrobeFlash)
      {
         strobe_t *strobe;
         *save_p++ = tc_strobe;
         PADSAVEP();
         strobe = (strobe_t *)save_p;
         memcpy (strobe, th, sizeof(*strobe));
         save_p += sizeof(*strobe);
         strobe->sector = (sector_t *)(strobe->sector - sectors);
         continue;
      }

      if (th->function == T_Glow)
      {
         glow_t *glow;
         *save_p++ = tc_glow;
         PADSAVEP();
         glow = (glow_t *)save_p;
         memcpy (glow, th, sizeof(*glow));
         save_p += sizeof(*glow);
         glow->sector = (sector_t *)(glow->sector - sectors);
         continue;
      }

      // killough 10/4/98: save flickers
      if (th->function == T_FireFlicker)
      {
         fireflicker_t *flicker;
         *save_p++ = tc_flicker;
         PADSAVEP();
         flicker = (fireflicker_t *)save_p;
         memcpy (flicker, th, sizeof(*flicker));
         save_p += sizeof(*flicker);
         flicker->sector = (sector_t *)(flicker->sector - sectors);
         continue;
      }

      //jff 2/22/98 new case for elevators
      if (th->function == T_MoveElevator)
      {
         elevator_t *elevator;         //jff 2/22/98
         *save_p++ = tc_elevator;
         PADSAVEP();
         elevator = (elevator_t *)save_p;
         memcpy (elevator, th, sizeof(*elevator));
         save_p += sizeof(*elevator);
         elevator->sector = (sector_t *)(elevator->sector - sectors);
         continue;
      }

      // killough 3/7/98: Scroll effect thinkers
      if (th->function == T_Scroll)
      {
         *save_p++ = tc_scroll;
         memcpy (save_p, th, sizeof(scroll_t));
         save_p += sizeof(scroll_t);
         continue;
      }

      // phares 3/22/98: Push/Pull effect thinkers

      if (th->function == T_Pusher)
      {
         *save_p++ = tc_pusher;
         memcpy (save_p, th, sizeof(pusher_t));
         save_p += sizeof(pusher_t);
         continue;
      }
   }

   // add a terminating marker
   *save_p++ = tc_endspecials;
}


//
// P_UnArchiveSpecials
//
void P_UnArchiveSpecials (void)
{
   byte tclass;

   // read in saved thinkers
   while ((tclass = *save_p++) != tc_endspecials)  // killough 2/14/98
      switch (tclass)
      {
      case tc_ceiling: