Debian Codegraph @

prboom2/src/hexen/sv_save.c

raw
1 //
2 // Copyright(C) 1993-1996 Id Software, Inc.
3 // Copyright(C) 1993-2008 Raven Software
4 // Copyright(C) 2005-2014 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16
17 #include <stdint.h>
18
19 #include "doomstat.h"
20 #include "p_tick.h"
21 #include "p_setup.h"
22 #include "p_spec.h"
23 #include "d_player.h"
24 #include "p_mobj.h"
25 #include "p_map.h"
26 #include "g_game.h"
27 #include "r_state.h"
28 #include "r_main.h"
29 #include "p_maputl.h"
30 #include "p_enemy.h"
31 #include "p_saveg.h"
32 #include "hu_stuff.h"
33 #include "lprintf.h"
34
35 #include "dsda/map_format.h"
36 #include "dsda/mapinfo.h"
37
38 #include "hexen/a_action.h"
39 #include "hexen/p_acs.h"
40 #include "hexen/po_man.h"
41 #include "hexen/sn_sonix.h"
42
43 #include "sv_save.h"
44
45 #define MAX_TARGET_PLAYERS 512
46 #define MOBJ_NULL -1
47 #define MOBJ_XX_PLAYER -2
48 #define MAX_MAPS 99
49
50 typedef enum
51 {
52 ASEG_GAME_HEADER = 101,
53 ASEG_MAP_HEADER,
54 ASEG_WORLD,
55 ASEG_POLYOBJS,
56 ASEG_MOBJS,
57 ASEG_THINKERS,
58 ASEG_SCRIPTS,
59 ASEG_PLAYERS,
60 ASEG_SOUNDS,
61 ASEG_MISC,
62 ASEG_END
63 } gameArchiveSegment_t;
64
65 typedef enum
66 {
67 TC_NULL,
68 TC_MOVE_CEILING,
69 TC_VERTICAL_DOOR,
70 TC_MOVE_FLOOR,
71 TC_PLAT_RAISE,
72 TC_INTERPRET_ACS,
73 TC_FLOOR_WAGGLE,
74 TC_LIGHT,
75 TC_PHASE,
76 TC_BUILD_PILLAR,
77 TC_ROTATE_POLY,
78 TC_MOVE_POLY,
79 TC_POLY_DOOR
80 } thinkClass_t;
81
82 typedef struct
83 {
84 thinkClass_t tClass;
85 think_t thinkerFunc;
86 void (*writeFunc)();
87 void (*readFunc)();
88 void (*restoreFunc) ();
89 size_t size;
90 } thinkInfo_t;
91
92 static int MobjCount;
93 static mobj_t **MobjList;
94 static mobj_t ***TargetPlayerAddrs;
95 static int TargetPlayerCount;
96
97 extern int inv_ptr;
98 extern int curpos;
99
100 typedef struct
101 {
102 size_t size;
103 byte *buffer;
104 } map_archive_t;
105
106 static map_archive_t map_archive[MAX_MAPS];
107 static map_archive_t *ma_p;
108 static byte *buffer_p;
109
110 static dboolean MapArchiveExists(int map)
111 {
112 return (map_archive[map].buffer != NULL);
113 }
114
115 static void FreeMapArchive(void)
116 {
117 int map;
118
119 for (map = 0; map < MAX_MAPS; ++map)
120 if (map_archive[map].buffer)
121 {
122 Z_Free(map_archive[map].buffer);
123 map_archive[map].buffer = NULL;
124 map_archive[map].size = 0;
125 }
126 }
127
128 void SV_StoreMapArchive(void)
129 {
130 int i;
131
132 for (i = 0; i < MAX_MAPS; ++i)
133 {
134 P_SAVE_X(map_archive[i].size);
135
136 if (map_archive[i].size)
137 {
138 P_SAVE_SIZE(map_archive[i].buffer, map_archive[i].size);
139 }
140 }
141 }
142
143 void SV_RestoreMapArchive(void)
144 {
145 int i;
146
147 FreeMapArchive();
148
149 for (i = 0; i < MAX_MAPS; ++i)
150 {
151 P_LOAD_X(map_archive[i].size);
152
153 if (map_archive[i].size)
154 {
155 map_archive[i].buffer = Z_Malloc(map_archive[i].size);
156 P_LOAD_SIZE(map_archive[i].buffer, map_archive[i].size);
157 }
158 }
159 }
160
161 static dboolean SV_IsMobjThinker(thinker_t *th)
162 {
163 return th->function == P_MobjThinker ||
164 (th->function == P_RemoveThinkerDelayed && th->references);
165 }
166
167 static void CheckBuffer(size_t size)
168 {
169 size_t delta = buffer_p - ma_p->buffer;
170
171 while (delta + size > ma_p->size)
172 {
173 ma_p->size += 1024;
174 ma_p->buffer = Z_Realloc(ma_p->buffer, ma_p->size);
175 buffer_p = ma_p->buffer + delta;
176 }
177 }
178
179 static void SV_Read(void *buffer, size_t size)
180 {
181 if (buffer_p - ma_p->buffer + size > ma_p->size)
182 {
183 I_Error("Invalid map archive in SV_Read");
184 }
185
186 memcpy(buffer, buffer_p, size);
187 buffer_p += size;
188 }
189
190 static byte SV_ReadByte(void)
191 {
192 byte result;
193 SV_Read(&result, sizeof(byte));
194 return result;
195 }
196
197 static unsigned short SV_ReadWord(void)
198 {
199 unsigned short result;
200 SV_Read(&result, sizeof(unsigned short));
201 return result;
202 }
203
204 static int SV_ReadLong(void)
205 {
206 int result;
207 SV_Read(&result, sizeof(int));
208 return result;
209 }
210
211 static uint64_t SV_ReadFlags(void)
212 {
213 uint64_t result;
214 SV_Read(&result, sizeof(uint64_t));
215 return result;
216 }
217
218 static void SV_Write(const void *buffer, size_t size)
219 {
220 CheckBuffer(size);
221 memcpy(buffer_p, buffer, size);
222 buffer_p += size;
223 }
224
225 static void SV_WriteByte(byte val)
226 {
227 SV_Write(&val, sizeof(byte));
228 }
229
230 static void SV_WriteWord(unsigned short val)
231 {
232 SV_Write(&val, sizeof(unsigned short));
233 }
234
235 static void SV_WriteLong(unsigned int val)
236 {
237 SV_Write(&val, sizeof(unsigned int));
238 }
239
240 static void SV_WriteFlags(uint64_t val)
241 {
242 SV_Write(&val, sizeof(uint64_t));
243 }
244
245 static void SV_OpenRead(int map)
246 {
247 ma_p = &map_archive[map];
248 buffer_p = ma_p->buffer;
249 }
250
251 static void SV_OpenWrite(int map)
252 {
253 ma_p = &map_archive[map];
254 if (ma_p->buffer)
255 {
256 Z_Free(ma_p->buffer);
257 }
258 ma_p->size = 1024;
259 ma_p->buffer = Z_Malloc(ma_p->size);
260 buffer_p = ma_p->buffer;
261 }
262
263 static int GetMobjNum(mobj_t * mobj)
264 {
265 if (mobj == NULL)
266 {
267 return MOBJ_NULL;
268 }
269 if (mobj->player)
270 {
271 return MOBJ_XX_PLAYER;
272 }
273 return mobj->archiveNum;
274 }
275
276 static void SetMobjPtr(mobj_t **ptr, unsigned int archiveNum)
277 {
278 if (archiveNum == MOBJ_NULL)
279 {
280 *ptr = NULL;
281 }
282 else if (archiveNum == MOBJ_XX_PLAYER)
283 {
284 if (TargetPlayerCount == MAX_TARGET_PLAYERS)
285 {
286 I_Error("RestoreMobj: exceeded MAX_TARGET_PLAYERS");
287 }
288 TargetPlayerAddrs[TargetPlayerCount++] = ptr;
289 *ptr = NULL;
290 }
291 else
292 {
293 P_SetTarget(ptr, MobjList[archiveNum]);
294 }
295 }
296
297 static void StreamInMobjSpecials(mobj_t *mobj)
298 {
299 mobj->special1.i = SV_ReadLong();
300 SetMobjPtr(&mobj->special1.m, SV_ReadLong());
301 mobj->special2.i = SV_ReadLong();
302 SetMobjPtr(&mobj->special2.m, SV_ReadLong());
303 }
304
305 static void StreamIn_mobj_t(mobj_t *str)
306 {
307 unsigned int i;
308
309 // "is the mobj marked for deletion?"
310 str->index = SV_ReadByte();
311
312 // fixed_t x, y, z;
313 str->x = SV_ReadLong();
314 str->y = SV_ReadLong();
315 str->z = SV_ReadLong();
316
317 // struct mobj_s *snext, *sprev;
318 // Pointer values are discarded:
319 str->snext = NULL;
320 str->sprev = NULL;
321
322 // angle_t angle;
323 str->angle = SV_ReadLong();
324
325 // spritenum_t sprite;
326 str->sprite = SV_ReadLong();
327
328 // int frame;
329 str->frame = SV_ReadLong();
330
331 // struct mobj_s *bnext, *bprev;
332 // Values are read but discarded; this will be restored when the thing's
333 // position is set.
334 str->bnext = NULL;
335 str->bprev = NULL;
336
337 // struct subsector_s *subsector;
338 // Read but discard: pointer will be restored when thing position is set.
339 str->subsector = NULL;
340
341 // fixed_t floorz, ceilingz;
342 str->floorz = SV_ReadLong();
343 str->ceilingz = SV_ReadLong();
344
345 // fixed_t floorpic;
346 str->floorpic = SV_ReadLong();
347
348 // fixed_t radius, height;
349 str->radius = SV_ReadLong();
350 str->height = SV_ReadLong();
351
352 // fixed_t momx, momy, momz;
353 str->momx = SV_ReadLong();
354 str->momy = SV_ReadLong();
355 str->momz = SV_ReadLong();
356
357 // int validcount;
358 str->validcount = SV_ReadLong();
359
360 // mobjtype_t type;
361 str->type = SV_ReadLong();
362
363 // mobjinfo_t *info;
364 // Pointer value is read but discarded.
365 str->info = NULL;
366
367 // int tics;
368 str->tics = SV_ReadLong();
369
370 // state_t *state;
371 // Restore as index into states table.
372 i = SV_ReadLong();
373 str->state = &states[i];
374
375 // int damage;
376 str->damage = SV_ReadLong();
377
378 // int flags;
379 str->flags = SV_ReadFlags();
380
381 // int flags2;
382 str->flags2 = SV_ReadFlags();
383
384 // specialval_t special1;
385 // specialval_t special2;
386 // Read in special values: there are special cases to deal with with
387 // mobj pointers.
388 StreamInMobjSpecials(str);
389
390 // int health;
391 str->health = SV_ReadLong();
392
393 // short movedir;
394 str->movedir = SV_ReadWord();
395
396 // short movecount;
397 str->movecount = SV_ReadWord();
398
399 // struct mobj_s *target;
400 i = SV_ReadLong();
401 SetMobjPtr(&str->target, i);
402
403 // int reactiontime;
404 str->reactiontime = SV_ReadLong();
405
406 // int threshold;
407 str->threshold = SV_ReadLong();
408
409 // struct player_s *player;
410 // Saved as player number.
411 i = SV_ReadLong();
412 if (i == 0)
413 {
414 str->player = NULL;
415 }
416 else
417 {
418 str->player = &players[i - 1];
419 str->player->mo = str;
420 }
421
422 // int lastlook;
423 str->lastlook = SV_ReadLong();
424
425 // fixed_t floorclip;
426 str->floorclip = SV_ReadLong();
427
428 // int archiveNum;
429 str->archiveNum = SV_ReadLong();
430
431 // short tid;
432 str->tid = SV_ReadWord();
433
434 // int special;
435 str->special = SV_ReadLong();
436
437 // int args[5];
438 for (i=0; i<5; ++i)
439 {
440 str->special_args[i] = SV_ReadLong();
441 }
442
443 str->friction = ORIG_FRICTION;
444 str->gravity = FRACUNIT;
445 str->alpha = 1.f;
446 }
447
448 static void StreamOutMobjSpecials(mobj_t *mobj)
449 {
450 dboolean corpse;
451
452 corpse = (mobj->flags & MF_CORPSE) != 0;
453
454 SV_WriteLong(mobj->type == HEXEN_MT_KORAX ? 0 : mobj->special1.i);
455 SV_WriteLong(corpse ? MOBJ_NULL : GetMobjNum(mobj->special1.m));
456 SV_WriteLong(mobj->special2.i);
457 SV_WriteLong(corpse ? MOBJ_NULL : GetMobjNum(mobj->special2.m));
458 }
459
460 static void StreamOut_mobj_t(mobj_t *str)
461 {
462 int i;
463
464 // store "marked for deletion" flag
465 SV_WriteByte(str->thinker.function == P_RemoveThinkerDelayed);
466
467 // fixed_t x, y, z;
468 SV_WriteLong(str->x);
469 SV_WriteLong(str->y);
470 SV_WriteLong(str->z);
471
472 // angle_t angle;
473 SV_WriteLong(str->angle);
474
475 // spritenum_t sprite;
476 SV_WriteLong(str->sprite);
477
478 // int frame;
479 SV_WriteLong(str->frame);
480
481 // fixed_t floorz, ceilingz;
482 SV_WriteLong(str->floorz);
483 SV_WriteLong(str->ceilingz);
484
485 // fixed_t floorpic;
486 SV_WriteLong(str->floorpic);
487
488 // fixed_t radius, height;
489 SV_WriteLong(str->radius);
490 SV_WriteLong(str->height);
491
492 // fixed_t momx, momy, momz;
493 SV_WriteLong(str->momx);
494 SV_WriteLong(str->momy);
495 SV_WriteLong(str->momz);
496
497 // int validcount;
498 SV_WriteLong(str->validcount);
499
500 // mobjtype_t type;
501 SV_WriteLong(str->type);
502
503 // int tics;
504 SV_WriteLong(str->tics);
505
506 // state_t *state;
507 // Save as index into the states table.
508 SV_WriteLong(str->state - states);
509
510 // int damage;
511 SV_WriteLong(str->damage);
512
513 // int flags;
514 SV_WriteFlags(str->flags);
515
516 // int flags2;
517 SV_WriteFlags(str->flags2);
518
519 // specialval_t special1;
520 // specialval_t special2;
521 // There are lots of special cases for the special values:
522 StreamOutMobjSpecials(str);
523
524 // int health;
525 SV_WriteLong(str->health);
526
527 // int movedir;
528 SV_WriteWord(str->movedir);
529
530 // int movecount;
531 SV_WriteWord(str->movecount);
532
533 // struct mobj_s *target;
534 if ((str->flags & MF_CORPSE) != 0)
535 {
536 SV_WriteLong(MOBJ_NULL);
537 }
538 else
539 {
540 SV_WriteLong(GetMobjNum(str->target));
541 }
542
543 // int reactiontime;
544 SV_WriteLong(str->reactiontime);
545
546 // int threshold;
547 SV_WriteLong(str->threshold);
548
549 // struct player_s *player;
550 // Stored as index into players[] array, if there is a player pointer.
551 if (str->player != NULL)
552 {
553 SV_WriteLong(str->player - players + 1);
554 }
555 else
556 {
557 SV_WriteLong(0);
558 }
559
560 // int lastlook;
561 SV_WriteLong(str->lastlook);
562
563 // fixed_t floorclip;
564 SV_WriteLong(str->floorclip);
565
566 // int archiveNum;
567 SV_WriteLong(str->archiveNum);
568
569 // short tid;
570 SV_WriteWord(str->tid);
571
572 // int special;
573 SV_WriteLong(str->special);
574
575 // int args[5];
576 for (i=0; i<5; ++i)
577 {
578 SV_WriteLong(str->special_args[i]);
579 }
580 }
581
582 static void StreamIn_floormove_t(floormove_t *str)
583 {
584 int i;
585
586 // sector_t *sector;
587 i = SV_ReadLong();
588 str->sector = sectors + i;
589
590 // floor_e type;
591 str->type = SV_ReadLong();
592
593 // int crush;
594 str->crush = SV_ReadLong();
595
596 // int direction;
597 str->direction = SV_ReadLong();
598
599 // newspecial_t newspecial;
600 str->newspecial.special = SV_ReadWord();
601 str->newspecial.flags = SV_ReadLong();
602 str->newspecial.damage.amount = SV_ReadWord();
603 str->newspecial.damage.leakrate = SV_ReadByte();
604 str->newspecial.damage.interval = SV_ReadByte();
605
606 // short texture;
607 str->texture = SV_ReadWord();
608
609 // fixed_t floordestheight;
610 str->floordestheight = SV_ReadLong();
611
612 // fixed_t speed;
613 str->speed = SV_ReadLong();
614
615 // int delayCount;
616 str->delayCount = SV_ReadLong();
617
618 // int delayTotal;
619 str->delayTotal = SV_ReadLong();
620
621 // fixed_t stairsDelayHeight;
622 str->stairsDelayHeight = SV_ReadLong();
623
624 // fixed_t stairsDelayHeightDelta;
625 str->stairsDelayHeightDelta = SV_ReadLong();
626
627 // fixed_t resetHeight;
628 str->resetHeight = SV_ReadLong();
629
630 // short resetDelay;
631 str->resetDelay = SV_ReadWord();
632
633 // short resetDelayCount;
634 str->resetDelayCount = SV_ReadWord();
635
636 // byte textureChange;
637 str->textureChange = SV_ReadByte();
638 }
639
640 static void StreamOut_floormove_t(floormove_t *str)
641 {
642 // sector_t *sector;
643 SV_WriteLong(str->sector - sectors);
644
645 // floor_e type;
646 SV_WriteLong(str->type);
647
648 // int crush;
649 SV_WriteLong(str->crush);
650
651 // int direction;
652 SV_WriteLong(str->direction);
653
654 // newspecial_t newspecial;
655 SV_WriteWord(str->newspecial.special);
656 SV_WriteLong(str->newspecial.flags);
657 SV_WriteWord(str->newspecial.damage.amount);
658 SV_WriteByte(str->newspecial.damage.leakrate);
659 SV_WriteByte(str->newspecial.damage.interval);
660
661 // short texture;
662 SV_WriteWord(str->texture);
663
664 // fixed_t floordestheight;
665 SV_WriteLong(str->floordestheight);
666
667 // fixed_t speed;
668 SV_WriteLong(str->speed);
669
670 // int delayCount;
671 SV_WriteLong(str->delayCount);
672
673 // int delayTotal;
674 SV_WriteLong(str->delayTotal);
675
676 // fixed_t stairsDelayHeight;
677 SV_WriteLong(str->stairsDelayHeight);
678
679 // fixed_t stairsDelayHeightDelta;
680 SV_WriteLong(str->stairsDelayHeightDelta);
681
682 // fixed_t resetHeight;
683 SV_WriteLong(str->resetHeight);
684
685 // short resetDelay;
686 SV_WriteWord(str->resetDelay);
687
688 // short resetDelayCount;
689 SV_WriteWord(str->resetDelayCount);
690
691 // byte textureChange;
692 SV_WriteByte(str->textureChange);
693 }
694
695 static void StreamIn_plat_t(plat_t *str)
696 {
697 int i;
698
699 // sector_t *sector;
700 i = SV_ReadLong();
701 str->sector = sectors + i;
702
703 // fixed_t speed;
704 str->speed = SV_ReadLong();
705
706 // fixed_t low;
707 str->low = SV_ReadLong();
708
709 // fixed_t high;
710 str->high = SV_ReadLong();
711
712 // int wait;
713 str->wait = SV_ReadLong();
714
715 // int count;
716 str->count = SV_ReadLong();
717
718 // plat_e status;
719 str->status = SV_ReadLong();
720
721 // plat_e oldstatus;
722 str->oldstatus = SV_ReadLong();
723
724 // int crush;
725 str->crush = SV_ReadLong();
726
727 // int tag;
728 str->tag = SV_ReadLong();
729
730 // plattype_e type;
731 str->type = SV_ReadLong();
732 }
733
734 static void StreamOut_plat_t(plat_t *str)
735 {
736 // sector_t *sector;
737 SV_WriteLong(str->sector - sectors);
738
739 // fixed_t speed;
740 SV_WriteLong(str->speed);
741
742 // fixed_t low;
743 SV_WriteLong(str->low);
744
745 // fixed_t high;
746 SV_WriteLong(str->high);
747
748 // int wait;
749 SV_WriteLong(str->wait);
750
751 // int count;
752 SV_WriteLong(str->count);
753
754 // plat_e status;
755 SV_WriteLong(str->status);
756
757 // plat_e oldstatus;
758 SV_WriteLong(str->oldstatus);
759
760 // int crush;
761 SV_WriteLong(str->crush);
762
763 // int tag;
764 SV_WriteLong(str->tag);
765
766 // plattype_e type;
767 SV_WriteLong(str->type);
768 }
769
770 static void StreamIn_ceiling_t(ceiling_t *str)
771 {
772 int i;
773
774 // sector_t *sector;
775 i = SV_ReadLong();
776 str->sector = sectors + i;
777
778 // ceiling_e type;
779 str->type = SV_ReadLong();
780
781 // fixed_t bottomheight, topheight;
782 str->bottomheight = SV_ReadLong();
783 str->topheight = SV_ReadLong();
784
785 // fixed_t speed;
786 str->speed = SV_ReadLong();
787
788 // int crush;
789 str->crush = SV_ReadLong();
790
791 // int direction;
792 str->direction = SV_ReadLong();
793
794 // int tag;
795 str->tag = SV_ReadLong();
796
797 // int olddirection;
798 str->olddirection = SV_ReadLong();
799 }
800
801 static void StreamOut_ceiling_t(ceiling_t *str)
802 {
803 // sector_t *sector;
804 SV_WriteLong(str->sector - sectors);
805
806 // ceiling_e type;
807 SV_WriteLong(str->type);
808
809 // fixed_t bottomheight, topheight;
810 SV_WriteLong(str->bottomheight);
811 SV_WriteLong(str->topheight);
812
813 // fixed_t speed;
814 SV_WriteLong(str->speed);
815
816 // int crush;
817 SV_WriteLong(str->crush);
818
819 // int direction;
820 SV_WriteLong(str->direction);
821
822 // int tag;
823 SV_WriteLong(str->tag);
824
825 // int olddirection;
826 SV_WriteLong(str->olddirection);
827 }
828
829 static void StreamIn_light_t(light_t *str)
830 {
831 int i;
832
833 // sector_t *sector;
834 i = SV_ReadLong();
835 str->sector = sectors + i;
836
837 // lighttype_t type;
838 str->type = SV_ReadLong();
839
840 // int value1;
841 str->value1 = SV_ReadLong();
842
843 // int value2;
844 str->value2 = SV_ReadLong();
845
846 // int tics1;
847 str->tics1 = SV_ReadLong();
848
849 // int tics2;
850 str->tics2 = SV_ReadLong();
851
852 // int count;
853 str->count = SV_ReadLong();
854 }
855
856 static void StreamOut_light_t(light_t *str)
857 {
858 // sector_t *sector;
859 SV_WriteLong(str->sector - sectors);
860
861 // lighttype_t type;
862 SV_WriteLong(str->type);
863
864 // int value1;
865 SV_WriteLong(str->value1);
866
867 // int value2;
868 SV_WriteLong(str->value2);
869
870 // int tics1;
871 SV_WriteLong(str->tics1);
872
873 // int tics2;
874 SV_WriteLong(str->tics2);
875
876 // int count;
877 SV_WriteLong(str->count);
878 }
879
880 static void StreamIn_vldoor_t(vldoor_t *str)
881 {
882 int i;
883
884 // sector_t *sector;
885 i = SV_ReadLong();
886 str->sector = &sectors[i];
887
888 // vldoor_e type;
889 str->type = SV_ReadLong();
890
891 // fixed_t topheight;
892 str->topheight = SV_ReadLong();
893
894 // fixed_t speed;
895 str->speed = SV_ReadLong();
896
897 // int direction;
898 str->direction = SV_ReadLong();
899
900 // int topwait;
901 str->topwait = SV_ReadLong();
902
903 // int topcountdown;
904 str->topcountdown = SV_ReadLong();
905 }
906
907 static void StreamOut_vldoor_t(vldoor_t *str)
908 {
909 // sector_t *sector;
910 SV_WriteLong(str->sector - sectors);
911
912 // vldoor_e type;
913 SV_WriteLong(str->type);
914
915 // fixed_t topheight;
916 SV_WriteLong(str->topheight);
917
918 // fixed_t speed;
919 SV_WriteLong(str->speed);
920
921 // int direction;
922 SV_WriteLong(str->direction);
923
924 // int topwait;
925 SV_WriteLong(str->topwait);
926
927 // int topcountdown;
928 SV_WriteLong(str->topcountdown);
929 }
930
931 static void StreamIn_phase_t(phase_t *str)
932 {
933 int i;
934
935 // sector_t *sector;
936 i = SV_ReadLong();
937 str->sector = &sectors[i];
938
939 // int index;
940 str->index = SV_ReadLong();
941
942 // int base;
943 str->base = SV_ReadLong();
944 }
945
946 static void StreamOut_phase_t(phase_t *str)
947 {
948 // sector_t *sector;
949 SV_WriteLong(str->sector - sectors);
950
951 // int index;
952 SV_WriteLong(str->index);
953
954 // int base;
955 SV_WriteLong(str->base);
956 }
957
958 static void StreamIn_acs_t(acs_t *str)
959 {
960 int i;
961
962 // mobj_t *activator;
963 i = SV_ReadLong();
964 SetMobjPtr(&str->activator, i);
965
966 // line_t *line;
967 i = SV_ReadLong();
968 if (i != -1)
969 {
970 str->line = &lines[i];
971 }
972 else
973 {
974 str->line = NULL;
975 }
976
977 // int side;
978 str->side = SV_ReadLong();
979
980 // int number;
981 str->number = SV_ReadLong();
982
983 // int infoIndex;
984 str->infoIndex = SV_ReadLong();
985
986 // int delayCount;
987 str->delayCount = SV_ReadLong();
988
989 // int stack[ACS_STACK_DEPTH];
990 for (i=0; i<ACS_STACK_DEPTH; ++i)
991 {
992 str->stack[i] = SV_ReadLong();
993 }
994
995 // int stackPtr;
996 str->stackPtr = SV_ReadLong();
997
998 // int vars[MAX_ACS_SCRIPT_VARS];
999 for (i=0; i<MAX_ACS_SCRIPT_VARS; ++i)
1000 {
1001 str->vars[i] = SV_ReadLong();
1002 }
1003
1004 // int *ip;
1005 str->ip = SV_ReadLong();
1006 }
1007
1008 static void StreamOut_acs_t(acs_t *str)
1009 {
1010 int i;
1011
1012 // mobj_t *activator;
1013 SV_WriteLong(GetMobjNum(str->activator));
1014
1015 // line_t *line;
1016 if (str->line != NULL)
1017 {
1018 SV_WriteLong(str->line - lines);
1019 }
1020 else
1021 {
1022 SV_WriteLong(-1);
1023 }
1024
1025 // int side;
1026 SV_WriteLong(str->side);
1027
1028 // int number;
1029 SV_WriteLong(str->number);
1030
1031 // int infoIndex;
1032 SV_WriteLong(str->infoIndex);
1033
1034 // int delayCount;
1035 SV_WriteLong(str->delayCount);
1036
1037 // int stack[ACS_STACK_DEPTH];
1038 for (i=0; i<ACS_STACK_DEPTH; ++i)
1039 {
1040 SV_WriteLong(str->stack[i]);
1041 }
1042
1043 // int stackPtr;
1044 SV_WriteLong(str->stackPtr);
1045
1046 // int vars[MAX_ACS_SCRIPT_VARS];
1047 for (i=0; i<MAX_ACS_SCRIPT_VARS; ++i)
1048 {
1049 SV_WriteLong(str->vars[i]);
1050 }
1051
1052 // int *ip;
1053 SV_WriteLong(str->ip);
1054 }
1055
1056 static void StreamIn_polyevent_t(polyevent_t *str)
1057 {
1058 // int polyobj;
1059 str->polyobj = SV_ReadLong();
1060
1061 // int speed;
1062 str->speed = SV_ReadLong();
1063
1064 // unsigned int dist;
1065 str->dist = SV_ReadLong();
1066
1067 // int angle;
1068 str->angle = SV_ReadLong();
1069
1070 // fixed_t xSpeed;
1071 str->xSpeed = SV_ReadLong();
1072
1073 // fixed_t ySpeed;
1074 str->ySpeed = SV_ReadLong();
1075 }
1076
1077 static void StreamOut_polyevent_t(polyevent_t *str)
1078 {
1079 // int polyobj;
1080 SV_WriteLong(str->polyobj);
1081
1082 // int speed;
1083 SV_WriteLong(str->speed);
1084
1085 // unsigned int dist;
1086 SV_WriteLong(str->dist);
1087
1088 // int angle;
1089 SV_WriteLong(str->angle);
1090
1091 // fixed_t xSpeed;
1092 SV_WriteLong(str->xSpeed);
1093
1094 // fixed_t ySpeed;
1095 SV_WriteLong(str->ySpeed);
1096 }
1097
1098 static void StreamIn_pillar_t(pillar_t *str)
1099 {
1100 int i;
1101
1102 // sector_t *sector;
1103 i = SV_ReadLong();
1104 str->sector = &sectors[i];
1105
1106 // int ceilingSpeed;
1107 str->ceilingSpeed = SV_ReadLong();
1108
1109 // int floorSpeed;
1110 str->floorSpeed = SV_ReadLong();
1111
1112 // int floordest;
1113 str->floordest = SV_ReadLong();
1114
1115 // int ceilingdest;
1116 str->ceilingdest = SV_ReadLong();
1117
1118 // int direction;
1119 str->direction = SV_ReadLong();
1120
1121 // int crush;
1122 str->crush = SV_ReadLong();
1123 }
1124
1125 static void StreamOut_pillar_t(pillar_t *str)
1126 {
1127 // sector_t *sector;
1128 SV_WriteLong(str->sector - sectors);
1129
1130 // int ceilingSpeed;
1131 SV_WriteLong(str->ceilingSpeed);
1132
1133 // int floorSpeed;
1134 SV_WriteLong(str->floorSpeed);
1135
1136 // int floordest;
1137 SV_WriteLong(str->floordest);
1138
1139 // int ceilingdest;
1140 SV_WriteLong(str->ceilingdest);
1141
1142 // int direction;
1143 SV_WriteLong(str->direction);
1144
1145 // int crush;
1146 SV_WriteLong(str->crush);
1147 }
1148
1149 static void StreamIn_polydoor_t(polydoor_t *str)
1150 {
1151 // int polyobj;
1152 str->polyobj = SV_ReadLong();
1153
1154 // int speed;
1155 str->speed = SV_ReadLong();
1156
1157 // int dist;
1158 str->dist = SV_ReadLong();
1159
1160 // int totalDist;
1161 str->totalDist = SV_ReadLong();
1162
1163 // int direction;
1164 str->direction = SV_ReadLong();
1165
1166 // fixed_t xSpeed, ySpeed;
1167 str->xSpeed = SV_ReadLong();
1168 str->ySpeed = SV_ReadLong();
1169
1170 // int tics;
1171 str->tics = SV_ReadLong();
1172
1173 // int waitTics;
1174 str->waitTics = SV_ReadLong();
1175
1176 // podoortype_t type;
1177 str->type = SV_ReadLong();
1178
1179 // dboolean close;
1180 str->close = SV_ReadLong();
1181 }
1182
1183 static void StreamOut_polydoor_t(polydoor_t *str)
1184 {
1185 // int polyobj;
1186 SV_WriteLong(str->polyobj);
1187
1188 // int speed;
1189 SV_WriteLong(str->speed);
1190
1191 // int dist;
1192 SV_WriteLong(str->dist);
1193
1194 // int totalDist;
1195 SV_WriteLong(str->totalDist);
1196
1197 // int direction;
1198 SV_WriteLong(str->direction);
1199
1200 // fixed_t xSpeed, ySpeed;
1201 SV_WriteLong(str->xSpeed);
1202 SV_WriteLong(str->ySpeed);
1203
1204 // int tics;
1205 SV_WriteLong(str->tics);
1206
1207 // int waitTics;
1208 SV_WriteLong(str->waitTics);
1209
1210 // podoortype_t type;
1211 SV_WriteLong(str->type);
1212
1213 // dboolean close;
1214 SV_WriteLong(str->close);
1215 }
1216
1217 static void StreamIn_planeWaggle_t(planeWaggle_t *str)
1218 {
1219 int i;
1220
1221 // sector_t *sector;
1222 i = SV_ReadLong();
1223 str->sector = &sectors[i];
1224
1225 // fixed_t originalHeight;
1226 str->originalHeight = SV_ReadLong();
1227
1228 // fixed_t accumulator;
1229 str->accumulator = SV_ReadLong();
1230
1231 // fixed_t accDelta;
1232 str->accDelta = SV_ReadLong();
1233
1234 // fixed_t targetScale;
1235 str->targetScale = SV_ReadLong();
1236
1237 // fixed_t scale;
1238 str->scale = SV_ReadLong();
1239
1240 // fixed_t scaleDelta;
1241 str->scaleDelta = SV_ReadLong();
1242
1243 // int ticker;
1244 str->ticker = SV_ReadLong();
1245
1246 // int state;
1247 str->state = SV_ReadLong();
1248 }
1249
1250 static void StreamOut_planeWaggle_t(planeWaggle_t *str)
1251 {
1252 // sector_t *sector;
1253 SV_WriteLong(str->sector - sectors);
1254
1255 // fixed_t originalHeight;
1256 SV_WriteLong(str->originalHeight);
1257
1258 // fixed_t accumulator;
1259 SV_WriteLong(str->accumulator);
1260
1261 // fixed_t accDelta;
1262 SV_WriteLong(str->accDelta);
1263
1264 // fixed_t targetScale;
1265 SV_WriteLong(str->targetScale);
1266
1267 // fixed_t scale;
1268 SV_WriteLong(str->scale);
1269
1270 // fixed_t scaleDelta;
1271 SV_WriteLong(str->scaleDelta);
1272
1273 // int ticker;
1274 SV_WriteLong(str->ticker);
1275
1276 // int state;
1277 SV_WriteLong(str->state);
1278 }
1279
1280 void SV_Init(void)
1281 {
1282 FreeMapArchive();
1283 }
1284
1285 static void AssertSegment(gameArchiveSegment_t segType)
1286 {
1287 if (SV_ReadLong() != segType)
1288 {
1289 I_Error("Corrupt save game: Segment [%d] failed alignment check",
1290 segType);
1291 }
1292 }
1293
1294 static void ArchiveWorld(void)
1295 {
1296 int i;
1297 int j;
1298 sector_t *sec;
1299 line_t *li;
1300 side_t *si;
1301
1302 SV_WriteLong(ASEG_WORLD);
1303 for (i = 0, sec = sectors; i < numsectors; i++, sec++)
1304 {
1305 SV_WriteWord(sec->floorheight >> FRACBITS);
1306 SV_WriteWord(sec->ceilingheight >> FRACBITS);
1307 SV_WriteWord(sec->floorpic);
1308 SV_WriteWord(sec->ceilingpic);
1309 SV_WriteWord(sec->lightlevel);
1310 SV_WriteWord(sec->special);
1311 SV_WriteWord(sec->tag);
1312 SV_WriteWord(sec->seqType);
1313 }
1314 for (i = 0, li = lines; i < numlines; i++, li++)
1315 {
1316 SV_WriteLong(li->flags);
1317 // TODO: how does this work? it's a short
1318 SV_WriteByte(li->special);
1319 SV_WriteLong(li->special_args[0]);
1320 SV_WriteLong(li->special_args[1]);
1321 SV_WriteLong(li->special_args[2]);
1322 SV_WriteLong(li->special_args[3]);
1323 SV_WriteLong(li->special_args[4]);
1324 for (j = 0; j < 2; j++)
1325 {
1326 if (li->sidenum[j] == NO_INDEX)
1327 {
1328 continue;
1329 }
1330 si = &sides[li->sidenum[j]];
1331 SV_WriteWord(si->textureoffset >> FRACBITS);
1332 SV_WriteWord(si->rowoffset >> FRACBITS);
1333 SV_WriteWord(si->toptexture);
1334 SV_WriteWord(si->bottomtexture);
1335 SV_WriteWord(si->midtexture);
1336 }
1337 }
1338 }
1339
1340 static void UnarchiveWorld(void)
1341 {
1342 int i;
1343 int j;
1344 sector_t *sec;
1345 line_t *li;
1346 side_t *si;
1347
1348 AssertSegment(ASEG_WORLD);
1349 for (i = 0, sec = sectors; i < numsectors; i++, sec++)
1350 {
1351 sec->floorheight = SV_ReadWord() << FRACBITS;
1352 sec->ceilingheight = SV_ReadWord() << FRACBITS;
1353 sec->floorpic = SV_ReadWord();
1354 sec->ceilingpic = SV_ReadWord();
1355 sec->lightlevel = SV_ReadWord();
1356 sec->special = SV_ReadWord();
1357 sec->tag = SV_ReadWord();
1358 sec->seqType = SV_ReadWord();
1359 sec->ceilingdata = 0;
1360 sec->floordata = 0;
1361 sec->lightingdata = 0;
1362 sec->soundtarget = 0;
1363 }
1364 for (i = 0, li = lines; i < numlines; i++, li++)
1365 {
1366 li->flags = SV_ReadLong();
1367 li->special = SV_ReadByte();
1368 li->special_args[0] = SV_ReadLong();
1369 li->special_args[1] = SV_ReadLong();
1370 li->special_args[2] = SV_ReadLong();
1371 li->special_args[3] = SV_ReadLong();
1372 li->special_args[4] = SV_ReadLong();
1373 for (j = 0; j < 2; j++)
1374 {
1375 if (li->sidenum[j] == NO_INDEX)
1376 {
1377 continue;
1378 }
1379 si = &sides[li->sidenum[j]];
1380 si->textureoffset = SV_ReadWord() << FRACBITS;
1381 si->rowoffset = SV_ReadWord() << FRACBITS;
1382 si->toptexture = SV_ReadWord();
1383 si->bottomtexture = SV_ReadWord();
1384 si->midtexture = SV_ReadWord();
1385 }
1386 }
1387 }
1388
1389 static void SetMobjArchiveNums(void)
1390 {
1391 mobj_t *mobj;
1392 thinker_t *thinker;
1393
1394 MobjCount = 0;
1395 for (thinker = thinkercap.next; thinker != &thinkercap;
1396 thinker = thinker->next)
1397 {
1398 if (SV_IsMobjThinker(thinker))
1399 {
1400 mobj = (mobj_t *) thinker;
1401 if (mobj->player)
1402 { // Skipping player mobjs
1403 continue;
1404 }
1405 mobj->archiveNum = MobjCount++;
1406 }
1407 }
1408 }
1409
1410 static void ArchiveMobjs(void)
1411 {
1412 int count;
1413 thinker_t *thinker;
1414
1415 SV_WriteLong(ASEG_MOBJS);
1416 SV_WriteLong(MobjCount);
1417 count = 0;
1418 for (thinker = thinkercap.next; thinker != &thinkercap;
1419 thinker = thinker->next)
1420 {
1421 if (!SV_IsMobjThinker(thinker))
1422 { // Not a mobj thinker
1423 continue;
1424 }
1425 if (((mobj_t *) thinker)->player)
1426 { // Skipping player mobjs
1427 continue;
1428 }
1429 count++;
1430 StreamOut_mobj_t((mobj_t *) thinker);
1431 }
1432 if (count != MobjCount)
1433 {
1434 I_Error("ArchiveMobjs: bad mobj count");
1435 }
1436 }
1437
1438 static void UnarchiveMobjs(void)
1439 {
1440 int i;
1441 mobj_t *mobj;
1442
1443 AssertSegment(ASEG_MOBJS);
1444 TargetPlayerAddrs = Z_Malloc(MAX_TARGET_PLAYERS * sizeof(mobj_t **));
1445 TargetPlayerCount = 0;
1446 MobjCount = SV_ReadLong();
1447 MobjList = Z_Malloc(MobjCount * sizeof(mobj_t *));
1448 for (i = 0; i < MobjCount; i++)
1449 {
1450 MobjList[i] = Z_MallocLevel(sizeof(mobj_t));
1451 memset(MobjList[i], 0, sizeof(mobj_t));
1452 }
1453 for (i = 0; i < MobjCount; i++)
1454 {
1455 int references;
1456
1457 mobj = MobjList[i];
1458 StreamIn_mobj_t(mobj);
1459
1460 // Restore broken pointers.
1461 mobj->info = &mobjinfo[mobj->type];
1462
1463 // "marked for deletion"
1464 if (mobj->index)
1465 {
1466 mobj->index = -1;
1467 mobj->thinker.function = P_RemoveThinkerDelayed;
1468
1469 references = mobj->thinker.references;
1470 P_AddThinker(&mobj->thinker);
1471 mobj->thinker.references = references;
1472
1473 continue;
1474 }
1475
1476 P_SetThingPosition(mobj);
1477 mobj->floorz = mobj->subsector->sector->floorheight;
1478 mobj->ceilingz = mobj->subsector->sector->ceilingheight;
1479
1480 mobj->thinker.function = P_MobjThinker;
1481
1482 references = mobj->thinker.references;
1483 P_AddThinker(&mobj->thinker);
1484 mobj->thinker.references = references;
1485 }
1486 map_format.build_mobj_thing_id_list();
1487 P_InitCreatureCorpseQueue(true); // true = scan for corpses
1488 }
1489
1490 static void RestoreFloorWaggle(planeWaggle_t *th)
1491 {
1492 th->sector->floordata = th->thinker.function;
1493 }
1494
1495 static void RestoreBuildPillar(pillar_t *th)
1496 {
1497 th->sector->floordata = th->thinker.function;
1498 }
1499
1500 static void RestoreVerticalDoor(vldoor_t *th)
1501 {
1502 th->sector->ceilingdata = th->thinker.function;
1503 }
1504
1505 static void RestoreMoveFloor(floormove_t *th)
1506 {
1507 th->sector->floordata = th->thinker.function;
1508 }
1509
1510 static void RestorePlatRaise(plat_t *plat)
1511 {
1512 plat->sector->floordata = T_PlatRaise;
1513 P_AddActivePlat(plat);
1514 }
1515
1516 static void RestoreMoveCeiling(ceiling_t *ceiling)
1517 {
1518 ceiling->sector->ceilingdata = T_MoveCeiling;
1519 P_AddActiveCeiling(ceiling);
1520 }
1521
1522
1523 static thinkInfo_t ThinkerInfo[] = {
1524 {
1525 TC_MOVE_FLOOR,
1526 T_MoveFloor,
1527 StreamOut_floormove_t,
1528 StreamIn_floormove_t,
1529 RestoreMoveFloor,
1530 sizeof(floormove_t)
1531 },
1532 {
1533 TC_PLAT_RAISE,
1534 T_PlatRaise,
1535 StreamOut_plat_t,
1536 StreamIn_plat_t,
1537 RestorePlatRaise,
1538 sizeof(plat_t)
1539 },
1540 {
1541 TC_MOVE_CEILING,
1542 T_MoveCeiling,
1543 StreamOut_ceiling_t,
1544 StreamIn_ceiling_t,
1545 RestoreMoveCeiling,
1546 sizeof(ceiling_t)
1547 },
1548 {
1549 TC_LIGHT,
1550 T_Light,
1551 StreamOut_light_t,
1552 StreamIn_light_t,
1553 NULL,
1554 sizeof(light_t)
1555 },
1556 {
1557 TC_VERTICAL_DOOR,
1558 T_VerticalDoor,
1559 StreamOut_vldoor_t,
1560 StreamIn_vldoor_t,
1561 RestoreVerticalDoor,
1562 sizeof(vldoor_t)
1563 },
1564 {
1565 TC_PHASE,
1566 T_Phase,
1567 StreamOut_phase_t,
1568 StreamIn_phase_t,
1569 NULL,
1570 sizeof(phase_t)
1571 },
1572 {
1573 TC_INTERPRET_ACS,
1574 T_InterpretACS,
1575 StreamOut_acs_t,
1576 StreamIn_acs_t,
1577 NULL,
1578 sizeof(acs_t)
1579 },
1580 {
1581 TC_ROTATE_POLY,
1582 T_RotatePoly,
1583 StreamOut_polyevent_t,
1584 StreamIn_polyevent_t,
1585 NULL,
1586 sizeof(polyevent_t)
1587 },
1588 {
1589 TC_BUILD_PILLAR,
1590 T_BuildPillar,
1591 StreamOut_pillar_t,
1592 StreamIn_pillar_t,
1593 RestoreBuildPillar,
1594 sizeof(pillar_t)
1595 },
1596 {
1597 TC_MOVE_POLY,
1598 T_MovePoly,
1599 StreamOut_polyevent_t,
1600 StreamIn_polyevent_t,
1601 NULL,
1602 sizeof(polyevent_t)
1603 },
1604 {
1605 TC_POLY_DOOR,
1606 T_PolyDoor,
1607 StreamOut_polydoor_t,
1608 StreamIn_polydoor_t,
1609 NULL,
1610 sizeof(polydoor_t)
1611 },
1612 {
1613 TC_FLOOR_WAGGLE,
1614 T_FloorWaggle,
1615 StreamOut_planeWaggle_t,
1616 StreamIn_planeWaggle_t,
1617 RestoreFloorWaggle,
1618 sizeof(planeWaggle_t)
1619 },
1620 { TC_NULL, NULL, NULL, NULL, NULL, 0 },
1621 };
1622
1623 static void ArchiveThinkers(void)
1624 {
1625 thinker_t *thinker;
1626 thinkInfo_t *info;
1627
1628 SV_WriteLong(ASEG_THINKERS);
1629 for (thinker = thinkercap.next; thinker != &thinkercap;
1630 thinker = thinker->next)
1631 {
1632 for (info = ThinkerInfo; info->tClass != TC_NULL; info++)
1633 {
1634 if (thinker->function == info->thinkerFunc)
1635 {
1636 SV_WriteByte(info->tClass);
1637 info->writeFunc(thinker);
1638 break;
1639 }
1640 }
1641 }
1642 // Add a termination marker
1643 SV_WriteByte(TC_NULL);
1644 }
1645
1646 static void UnarchiveThinkers(void)
1647 {
1648 int tClass;
1649 thinker_t *thinker;
1650 thinkInfo_t *info;
1651
1652 AssertSegment(ASEG_THINKERS);
1653 while ((tClass = SV_ReadByte()) != TC_NULL)
1654 {
1655 for (info = ThinkerInfo; info->tClass != TC_NULL; info++)
1656 {
1657 if (tClass == info->tClass)
1658 {
1659 thinker = Z_MallocLevel(info->size);
1660 memset(thinker, 0, info->size);
1661 info->readFunc(thinker);
1662 thinker->function = info->thinkerFunc;
1663 if (info->restoreFunc)
1664 {
1665 info->restoreFunc(thinker);
1666 }
1667 P_AddThinker(thinker);
1668 break;
1669 }
1670 }
1671 if (info->tClass == TC_NULL)
1672 {
1673 I_Error("UnarchiveThinkers: Unknown tClass %d in "
1674 "savegame", tClass);
1675 }
1676 }
1677 }
1678
1679 static void ArchiveScripts(void)
1680 {
1681 int i;
1682
1683 SV_WriteLong(ASEG_SCRIPTS);
1684 for (i = 0; i < ACScriptCount; i++)
1685 {
1686 SV_WriteWord(ACSInfo[i].state);
1687 SV_WriteWord(ACSInfo[i].waitValue);
1688 }
1689
1690 for (i = 0; i< MAX_ACS_MAP_VARS; ++i)
1691 {
1692 SV_WriteLong(MapVars[i]);
1693 }
1694 }
1695
1696 static void UnarchiveScripts(void)
1697 {
1698 int i;
1699
1700 AssertSegment(ASEG_SCRIPTS);
1701 for (i = 0; i < ACScriptCount; i++)
1702 {
1703 ACSInfo[i].state = SV_ReadWord();
1704 ACSInfo[i].waitValue = SV_ReadWord();
1705 }
1706
1707 for (i = 0; i < MAX_ACS_MAP_VARS; ++i)
1708 {
1709 MapVars[i] = SV_ReadLong();
1710 }
1711 }
1712
1713 static void ArchiveMisc(void)
1714 {
1715 int ix;
1716
1717 SV_WriteLong(ASEG_MISC);
1718 for (ix = 0; ix < g_maxplayers; ix++)
1719 {
1720 SV_WriteLong(localQuakeHappening[ix]);
1721 }
1722 }
1723
1724 static void UnarchiveMisc(void)
1725 {
1726 int ix;
1727
1728 AssertSegment(ASEG_MISC);
1729 for (ix = 0; ix < g_maxplayers; ix++)
1730 {
1731 localQuakeHappening[ix] = SV_ReadLong();
1732 }
1733 }
1734
1735 static void RemoveAllThinkers(void)
1736 {
1737 thinker_t *thinker;
1738 thinker_t *nextThinker;
1739
1740 thinker = thinkercap.next;
1741 while (thinker != &thinkercap)
1742 {
1743 nextThinker = thinker->next;
1744 if (SV_IsMobjThinker(thinker))
1745 {
1746 P_RemoveMobj((mobj_t *) thinker);
1747 P_RemoveThinkerDelayed(thinker); // fix mobj leak
1748 }
1749 else
1750 {
1751 Z_Free(thinker);
1752 }
1753 thinker = nextThinker;
1754 }
1755 P_InitThinkers();
1756 }
1757
1758 static void ArchiveSounds(void)
1759 {
1760 seqnode_t *node;
1761 sector_t *sec;
1762 int difference;
1763 int i;
1764
1765 SV_WriteLong(ASEG_SOUNDS);
1766
1767 // Save the sound sequences
1768 SV_WriteLong(ActiveSequences);
1769 for (node = SequenceListHead; node; node = node->next)
1770 {
1771 SV_WriteLong(node->sequence);
1772 SV_WriteLong(node->delayTics);
1773 SV_WriteLong(node->volume);
1774 SV_WriteLong(SN_GetSequenceOffset(node->sequence,
1775 node->sequencePtr));
1776 SV_WriteLong(node->currentSoundID);
1777 for (i = 0; i < po_NumPolyobjs; i++)
1778 {
1779 if (node->mobj == (mobj_t *) & polyobjs[i].startSpot)
1780 {
1781 break;
1782 }
1783 }
1784 if (i == po_NumPolyobjs)
1785 { // Sound is attached to a sector, not a polyobj
1786 sec = R_PointInSector(node->mobj->x, node->mobj->y);
1787 difference = (int) ((byte *) sec
1788 - (byte *) & sectors[0]) / sizeof(sector_t);
1789 SV_WriteLong(0); // 0 -- sector sound origin
1790 }
1791 else
1792 {
1793 SV_WriteLong(1); // 1 -- polyobj sound origin
1794 difference = i;
1795 }
1796 SV_WriteLong(difference);
1797 }
1798 }
1799
1800 static void UnarchiveSounds(void)
1801 {
1802 int i;
1803 int numSequences;
1804 int sequence;
1805 int delayTics;
1806 int volume;
1807 int seqOffset;
1808 int soundID;
1809 int polySnd;
1810 int secNum;
1811 mobj_t *sndMobj;
1812
1813 AssertSegment(ASEG_SOUNDS);
1814
1815 // Reload and restart all sound sequences
1816 numSequences = SV_ReadLong();
1817 i = 0;
1818 while (i < numSequences)
1819 {
1820 sequence = SV_ReadLong();
1821 delayTics = SV_ReadLong();
1822 volume = SV_ReadLong();
1823 seqOffset = SV_ReadLong();
1824
1825 soundID = SV_ReadLong();
1826 polySnd = SV_ReadLong();
1827 secNum = SV_ReadLong();
1828 if (!polySnd)
1829 {
1830 sndMobj = (mobj_t *) & sectors[secNum].soundorg;
1831 }
1832 else
1833 {
1834 sndMobj = (mobj_t *) & polyobjs[secNum].startSpot;
1835 }
1836 SN_StartSequence(sndMobj, sequence);
1837 SN_ChangeNodeData(i, seqOffset, delayTics, volume, soundID);
1838 i++;
1839 }
1840 }
1841
1842 static void ArchivePolyobjs(void)
1843 {
1844 int i;
1845
1846 SV_WriteLong(ASEG_POLYOBJS);
1847 SV_WriteLong(po_NumPolyobjs);
1848 for (i = 0; i < po_NumPolyobjs; i++)
1849 {
1850 SV_WriteLong(polyobjs[i].tag);
1851 SV_WriteLong(polyobjs[i].angle);
1852 SV_WriteLong(polyobjs[i].startSpot.x);
1853 SV_WriteLong(polyobjs[i].startSpot.y);
1854 }
1855 }
1856
1857 static void UnarchivePolyobjs(void)
1858 {
1859 int i;
1860 fixed_t deltaX;
1861 fixed_t deltaY;
1862
1863 AssertSegment(ASEG_POLYOBJS);
1864 if (SV_ReadLong() != po_NumPolyobjs)
1865 {
1866 I_Error("UnarchivePolyobjs: Bad polyobj count");
1867 }
1868 for (i = 0; i < po_NumPolyobjs; i++)
1869 {
1870 if (SV_ReadLong() != polyobjs[i].tag)
1871 {
1872 I_Error("UnarchivePolyobjs: Invalid polyobj tag");
1873 }
1874 PO_RotatePolyobj(polyobjs[i].tag, (angle_t) SV_ReadLong());
1875 deltaX = SV_ReadLong() - polyobjs[i].startSpot.x;
1876 deltaY = SV_ReadLong() - polyobjs[i].startSpot.y;
1877 PO_MovePolyobj(polyobjs[i].tag, deltaX, deltaY);
1878 }
1879 }
1880
1881 void SV_SaveMap(void)
1882 {
1883 // Initialize the output buffer
1884 SV_OpenWrite(gamemap);
1885
1886 // Place a header marker
1887 SV_WriteLong(ASEG_MAP_HEADER);
1888
1889 // Write the level timer
1890 SV_WriteLong(leveltime);
1891
1892 // Set the mobj archive numbers
1893 SetMobjArchiveNums();
1894
1895 ArchiveWorld();
1896 ArchivePolyobjs();
1897 ArchiveMobjs();
1898 ArchiveThinkers();
1899 ArchiveScripts();
1900 ArchiveSounds();
1901 ArchiveMisc();
1902
1903 // Place a termination marker
1904 SV_WriteLong(ASEG_END);
1905 }
1906
1907 void SV_LoadMap(void)
1908 {
1909 // Load a base level
1910 G_InitNew(gameskill, gameepisode, gamemap, false);
1911
1912 // Remove all thinkers
1913 RemoveAllThinkers();
1914
1915 // Initialize the input buffer
1916 SV_OpenRead(gamemap);
1917
1918 AssertSegment(ASEG_MAP_HEADER);
1919
1920 // Read the level timer
1921 leveltime = SV_ReadLong();
1922
1923 UnarchiveWorld();
1924 UnarchivePolyobjs();
1925 UnarchiveMobjs();
1926 UnarchiveThinkers();
1927 UnarchiveScripts();
1928 UnarchiveSounds();
1929 UnarchiveMisc();
1930
1931 AssertSegment(ASEG_END);
1932
1933 // Free mobj list and save buffer
1934 Z_Free(MobjList);
1935 }
1936
1937 void SV_MapTeleport(int map, int position)
1938 {
1939 int i;
1940 int j;
1941 int key_i;
1942 player_t playerBackup[MAX_MAXPLAYERS];
1943 mobj_t *targetPlayerMobj;
1944 mobj_t *mobj;
1945 int inventoryPtr;
1946 int currentInvPos;
1947 dboolean rClass;
1948 dboolean playerWasReborn;
1949 dboolean oldWeaponowned[HEXEN_NUMWEAPONS];
1950 int oldKeys[NUMCARDS];
1951 int oldPieces = 0;
1952 int bestWeapon;
1953
1954 memset(oldKeys, 0, sizeof(oldKeys));
1955 memset(oldWeaponowned, 0, sizeof(oldWeaponowned));
1956
1957 if (!deathmatch)
1958 {
1959 if (dsda_MapCluster(gamemap) == dsda_MapCluster(map))
1960 { // Same cluster - save map without saving player mobjs
1961 SV_SaveMap();
1962 }
1963 else
1964 { // Entering new cluster - clear map archive
1965 SV_Init();
1966 }
1967 }
1968
1969 // Store player structs for later
1970 rClass = randomclass;
1971 randomclass = false;
1972 for (i = 0; i < g_maxplayers; i++)
1973 {
1974 playerBackup[i] = players[i];
1975 }
1976
1977 // Save some globals that get trashed during the load
1978 inventoryPtr = inv_ptr;
1979 currentInvPos = curpos;
1980
1981 // Only SV_LoadMap() uses TargetPlayerAddrs, so it's NULLed here
1982 // for the following check (player mobj redirection)
1983 TargetPlayerAddrs = NULL;
1984
1985 dsda_UpdateGameMap(1, map);
1986
1987 if (!deathmatch && MapArchiveExists(gamemap))
1988 { // Unarchive map
1989 SV_LoadMap();
1990 P_MapStart();
1991 }
1992 else
1993 { // New map
1994 G_InitNew(gameskill, gameepisode, gamemap, false);
1995
1996 P_MapStart();
1997
1998 // Destroy all freshly spawned players
1999 for (i = 0; i < g_maxplayers; i++)
2000 {
2001 if (playeringame[i])
2002 {
2003 P_RemoveMobj(players[i].mo);
2004 }
2005 }
2006 }
2007
2008 // Restore player structs
2009 targetPlayerMobj = NULL;
2010 for (i = 0; i < g_maxplayers; i++)
2011 {
2012 if (!playeringame[i])
2013 {
2014 continue;
2015 }
2016 players[i] = playerBackup[i];
2017 players[i].attacker = NULL;
2018 players[i].poisoner = NULL;
2019
2020 if (netgame)
2021 {
2022 if (players[i].playerstate == PST_DEAD)
2023 { // In a network game, force all players to be alive
2024 players[i].playerstate = PST_REBORN;
2025 }
2026 if (!deathmatch)
2027 { // Cooperative net-play, retain keys and weapons
2028 for (key_i = 0; key_i < NUMCARDS; ++key_i)
2029 oldKeys[key_i] = players[i].cards[key_i];
2030 oldPieces = players[i].pieces;
2031 for (j = 0; j < HEXEN_NUMWEAPONS; j++)
2032 {
2033 oldWeaponowned[j] = players[i].weaponowned[j];
2034 }
2035 }
2036 }
2037 playerWasReborn = (players[i].playerstate == PST_REBORN);
2038 if (deathmatch)
2039 {
2040 memset(players[i].frags, 0, sizeof(players[i].frags));
2041 mobj = P_SpawnMobj(playerstarts[0][i].x,
2042 playerstarts[0][i].y, 0,
2043 HEXEN_MT_PLAYER_FIGHTER);
2044 players[i].mo = mobj;
2045 G_DeathMatchSpawnPlayer(i);
2046 P_RemoveMobj(mobj);
2047 }
2048 else
2049 {
2050 P_SpawnPlayer(i, &playerstarts[position][i]);
2051 }
2052
2053 if (playerWasReborn && netgame && !deathmatch)
2054 { // Restore keys and weapons when reborn in co-op
2055 for (key_i = 0; key_i < NUMCARDS; ++key_i)
2056 players[i].cards[key_i] = oldKeys[key_i];
2057 players[i].pieces = oldPieces;
2058 for (bestWeapon = 0, j = 0; j < HEXEN_NUMWEAPONS; j++)
2059 {
2060 if (oldWeaponowned[j])
2061 {
2062 bestWeapon = j;
2063 players[i].weaponowned[j] = true;
2064 }
2065 }
2066 players[i].ammo[MANA_1] = 25;
2067 players[i].ammo[MANA_2] = 25;
2068 if (bestWeapon)
2069 { // Bring up the best weapon
2070 players[i].pendingweapon = bestWeapon;
2071 }
2072 }
2073
2074 if (targetPlayerMobj == NULL)
2075 { // The poor sap
2076 targetPlayerMobj = players[i].mo;
2077 }
2078 }
2079 randomclass = rClass;
2080
2081 // Redirect anything targeting a player mobj
2082 if (TargetPlayerAddrs)
2083 {
2084 for (i = 0; i < TargetPlayerCount; i++)
2085 {
2086 *TargetPlayerAddrs[i] = targetPlayerMobj;
2087 }
2088 Z_Free(TargetPlayerAddrs);
2089 }
2090
2091 // Destroy all things touching players
2092 for (i = 0; i < g_maxplayers; i++)
2093 {
2094 if (playeringame[i])
2095 {
2096 P_TeleportMove(players[i].mo, players[i].mo->x, players[i].mo->y, false);
2097 }
2098 }
2099
2100 // Restore trashed globals
2101 inv_ptr = inventoryPtr;
2102 curpos = currentInvPos;
2103
2104 // Launch waiting scripts
2105 if (!deathmatch)
2106 {
2107 P_CheckACSStore();
2108 }
2109
2110 P_MapEnd();
2111 }