Subversion Repositories eduke32

Rev

Rev 872 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 Plagman 1
//-------------------------------------------------------------------------
2
/*
484 terminx 3
Copyright (C) 1996, 2003 - 3D Realms Entertainment
4
Copyright (C) 2000, 2003 - Matt Saettler (EDuke Enhancements)
5
Copyright (C) 2004, 2007 - EDuke32 developers
5 Plagman 6
 
7
This file is part of EDuke32
8
 
9
EDuke32 is free software; you can redistribute it and/or
10
modify it under the terms of the GNU General Public License version 2
11
as published by the Free Software Foundation.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 
17
See the GNU General Public License for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
*/
23
//-------------------------------------------------------------------------
24
 
25
#include "duke3d.h"
26
#include "osd.h"
27
 
514 terminx 28
#ifdef RENDERTYPEWIN
29
#define WIN32_LEAN_AND_MEAN
30
#include <windows.h>
31
#endif
32
 
5 Plagman 33
extern char pow2char[];
34
 
528 terminx 35
extern int everyothertime;
36
static int which_palookup = 9;
564 terminx 37
int g_NumPalettes;
5 Plagman 38
short spritecache[MAXTILES][3];
39
 
40
static char precachehightile[2][MAXTILES>>3];
41
static int  precachecount;
42
 
886 terminx 43
extern char *duke3dgrpstring;
44
 
528 terminx 45
static void tloadtile(int tilenume, int type)
5 Plagman 46
{
423 terminx 47
    if ((picanm[tilenume]&63) < 1)
335 terminx 48
    {
423 terminx 49
        if (!(gotpic[tilenume>>3] & pow2char[tilenume&7])) precachecount++;
50
        gotpic[tilenume>>3] |= pow2char[tilenume&7];
51
        precachehightile[(unsigned char)type][tilenume>>3] |= pow2char[tilenume&7];
52
        return;
53
    }
54
 
55
    {
29 terminx 56
        int i,j;
5 Plagman 57
 
335 terminx 58
        if ((picanm[tilenume]&192)==192)
59
        {
29 terminx 60
            i = tilenume - (picanm[tilenume]&63);
61
            j = tilenume;
335 terminx 62
        }
63
        else
64
        {
29 terminx 65
            i = tilenume;
66
            j = tilenume + (picanm[tilenume]&63);
67
        }
335 terminx 68
        for (;i<=j;i++)
69
        {
29 terminx 70
            if (!(gotpic[i>>3] & pow2char[i&7])) precachecount++;
71
            gotpic[i>>3] |= pow2char[i&7];
352 terminx 72
            precachehightile[(unsigned char)type][i>>3] |= pow2char[i&7];
29 terminx 73
        }
335 terminx 74
    }
5 Plagman 75
}
76
 
528 terminx 77
static void cachespritenum(int i)
5 Plagman 78
{
79
    char maxc;
528 terminx 80
    int j;
5 Plagman 81
 
331 terminx 82
    if (ud.monsters_off && badguy(&sprite[i])) return;
5 Plagman 83
 
84
    maxc = 1;
85
 
331 terminx 86
    if (spritecache[PN][0] == PN)
87
        for (j = PN; j <= spritecache[PN][1]; j++)
5 Plagman 88
            tloadtile(j,1);
89
 
331 terminx 90
    switch (dynamictostatic[PN])
5 Plagman 91
    {
337 terminx 92
    case HYDRENT__STATIC:
93
        tloadtile(BROKEFIREHYDRENT,1);
94
        for (j = TOILETWATER; j < (TOILETWATER+4); j++) tloadtile(j,1);
95
        break;
96
    case TOILET__STATIC:
97
        tloadtile(TOILETBROKE,1);
98
        for (j = TOILETWATER; j < (TOILETWATER+4); j++) tloadtile(j,1);
99
        break;
100
    case STALL__STATIC:
101
        tloadtile(STALLBROKE,1);
102
        for (j = TOILETWATER; j < (TOILETWATER+4); j++) tloadtile(j,1);
103
        break;
104
    case RUBBERCAN__STATIC:
105
        maxc = 2;
106
        break;
107
    case TOILETWATER__STATIC:
108
        maxc = 4;
109
        break;
110
    case FEMPIC1__STATIC:
111
        maxc = 44;
112
        break;
113
    case LIZTROOP__STATIC:
114
    case LIZTROOPRUNNING__STATIC:
115
    case LIZTROOPSHOOT__STATIC:
116
    case LIZTROOPJETPACK__STATIC:
117
    case LIZTROOPONTOILET__STATIC:
118
    case LIZTROOPDUCKING__STATIC:
119
        for (j = LIZTROOP; j < (LIZTROOP+72); j++) tloadtile(j,1);
120
        for (j=HEADJIB1;j<LEGJIB1+3;j++) tloadtile(j,1);
121
        maxc = 0;
122
        break;
123
    case WOODENHORSE__STATIC:
124
        maxc = 5;
125
        for (j = HORSEONSIDE; j < (HORSEONSIDE+4); j++) tloadtile(j,1);
126
        break;
127
    case NEWBEAST__STATIC:
128
    case NEWBEASTSTAYPUT__STATIC:
129
        maxc = 90;
130
        break;
131
    case BOSS1__STATIC:
132
    case BOSS2__STATIC:
133
    case BOSS3__STATIC:
134
        maxc = 30;
135
        break;
136
    case OCTABRAIN__STATIC:
137
    case OCTABRAINSTAYPUT__STATIC:
138
    case COMMANDER__STATIC:
139
    case COMMANDERSTAYPUT__STATIC:
140
        maxc = 38;
141
        break;
142
    case RECON__STATIC:
143
        maxc = 13;
144
        break;
145
    case PIGCOP__STATIC:
146
    case PIGCOPDIVE__STATIC:
147
        maxc = 61;
148
        break;
149
    case SHARK__STATIC:
150
        maxc = 30;
151
        break;
152
    case LIZMAN__STATIC:
153
    case LIZMANSPITTING__STATIC:
154
    case LIZMANFEEDING__STATIC:
155
    case LIZMANJUMP__STATIC:
156
        for (j=LIZMANHEAD1;j<LIZMANLEG1+3;j++) tloadtile(j,1);
157
        maxc = 80;
158
        break;
159
    case APLAYER__STATIC:
160
        maxc = 0;
161
        if (ud.multimode > 1)
162
        {
5 Plagman 163
            maxc = 5;
337 terminx 164
            for (j = 1420;j < 1420+106; j++) tloadtile(j,1);
165
        }
166
        break;
167
    case ATOMICHEALTH__STATIC:
168
        maxc = 14;
169
        break;
170
    case DRONE__STATIC:
171
        maxc = 10;
172
        break;
173
    case EXPLODINGBARREL__STATIC:
174
    case SEENINE__STATIC:
175
    case OOZFILTER__STATIC:
176
        maxc = 3;
177
        break;
178
    case NUKEBARREL__STATIC:
179
    case CAMERA1__STATIC:
180
        maxc = 5;
181
        break;
182
        // caching of HUD sprites for weapons that may be in the level
183
    case CHAINGUNSPRITE__STATIC:
184
        for (j=CHAINGUN; j<=CHAINGUN+7; j++) tloadtile(j,1);
185
        break;
186
    case RPGSPRITE__STATIC:
187
        for (j=RPGGUN; j<=RPGGUN+2; j++) tloadtile(j,1);
188
        break;
189
    case FREEZESPRITE__STATIC:
190
        for (j=FREEZE; j<=FREEZE+5; j++) tloadtile(j,1);
191
        break;
192
    case GROWSPRITEICON__STATIC:
193
    case SHRINKERSPRITE__STATIC:
194
        for (j=SHRINKER-2; j<=SHRINKER+5; j++) tloadtile(j,1);
195
        break;
196
    case HBOMBAMMO__STATIC:
197
    case HEAVYHBOMB__STATIC:
198
        for (j=HANDREMOTE; j<=HANDREMOTE+5; j++) tloadtile(j,1);
199
        break;
200
    case TRIPBOMBSPRITE__STATIC:
201
        for (j=HANDHOLDINGLASER; j<=HANDHOLDINGLASER+4; j++) tloadtile(j,1);
202
        break;
203
    case SHOTGUNSPRITE__STATIC:
204
        tloadtile(SHOTGUNSHELL,1);
205
        for (j=SHOTGUN; j<=SHOTGUN+6; j++) tloadtile(j,1);
206
        break;
207
    case DEVISTATORSPRITE__STATIC:
208
        for (j=DEVISTATOR; j<=DEVISTATOR+1; j++) tloadtile(j,1);
209
        break;
5 Plagman 210
 
211
    }
212
 
331 terminx 213
    for (j = PN; j < (PN+maxc); j++) tloadtile(j,1);
5 Plagman 214
}
215
 
395 terminx 216
static void cachegoodsprites(void)
5 Plagman 217
{
528 terminx 218
    int i,j;
5 Plagman 219
 
331 terminx 220
    for (i=0;i<MAXTILES;i++)
5 Plagman 221
    {
331 terminx 222
        if (spriteflags[i] & SPRITE_FLAG_PROJECTILE)
5 Plagman 223
            tloadtile(i,1);
331 terminx 224
        if (spritecache[i][0] == i && spritecache[i][2])
225
            for (j = i; j <= spritecache[i][1]; j++)
5 Plagman 226
                tloadtile(j,1);
227
    }
228
    tloadtile(BOTTOMSTATUSBAR,1);
333 terminx 229
    if (ud.multimode > 1)
5 Plagman 230
        tloadtile(FRAGBAR,1);
231
 
232
    tloadtile(VIEWSCREEN,1);
233
 
331 terminx 234
    for (i=STARTALPHANUM;i<ENDALPHANUM+1;i++) tloadtile(i,1);
235
    for (i=BIGALPHANUM; i<BIGALPHANUM+82; i++) tloadtile(i,1);
236
    for (i=MINIFONT;i<MINIFONT+63;i++) tloadtile(i,1);
5 Plagman 237
 
331 terminx 238
    for (i=FOOTPRINTS;i<FOOTPRINTS+3;i++) tloadtile(i,1);
5 Plagman 239
 
333 terminx 240
    for (i = BURNING; i < BURNING+14; i++) tloadtile(i,1);
241
    for (i = BURNING2; i < BURNING2+14; i++) tloadtile(i,1);
5 Plagman 242
 
333 terminx 243
    for (i = CRACKKNUCKLES; i < CRACKKNUCKLES+4; i++) tloadtile(i,1);
5 Plagman 244
 
333 terminx 245
    for (i = FIRSTGUN; i < FIRSTGUN+3 ; i++) tloadtile(i,1);
246
    for (i = FIRSTGUNRELOAD; i < FIRSTGUNRELOAD+8 ; i++) tloadtile(i,1);
5 Plagman 247
 
333 terminx 248
    for (i = EXPLOSION2; i < EXPLOSION2+21 ; i++) tloadtile(i,1);
5 Plagman 249
 
511 terminx 250
    for (i = COOLEXPLOSION1; i < COOLEXPLOSION1+21 ; i++) tloadtile(i,1);
251
 
5 Plagman 252
    tloadtile(BULLETHOLE,1);
511 terminx 253
    tloadtile(BLOODPOOL,1);
254
    for (i = TRANSPORTERBEAM; i < (TRANSPORTERBEAM+6); i++) tloadtile(i,1);
522 terminx 255
 
333 terminx 256
    for (i = SMALLSMOKE; i < (SMALLSMOKE+4); i++) tloadtile(i,1);
710 terminx 257
    for (i = SHOTSPARK1; i < (SHOTSPARK1+4); i++) tloadtile(i,1);
5 Plagman 258
 
511 terminx 259
    for (i = BLOOD; i < (BLOOD+4); i++) tloadtile(i,1);
333 terminx 260
    for (i = JIBS1; i < (JIBS5+5); i++) tloadtile(i,1);
710 terminx 261
    for (i = JIBS6; i < (JIBS6+8); i++) tloadtile(i,1);
262
 
511 terminx 263
    for (i = SCRAP1; i < (SCRAP1+29); i++) tloadtile(i,1);
5 Plagman 264
 
511 terminx 265
    tloadtile(FIRELASER,1);
266
    for (i=FORCERIPPLE; i<(FORCERIPPLE+9); i++) tloadtile(i,1);
267
 
268
    for (i=MENUSCREEN; i<DUKECAR; i++) tloadtile(i,1);
522 terminx 269
 
5 Plagman 270
    for (i=RPG; i<RPG+7; i++) tloadtile(i,1);
271
    for (i=FREEZEBLAST; i<FREEZEBLAST+3; i++) tloadtile(i,1);
272
    for (i=SHRINKSPARK; i<SHRINKSPARK+4; i++) tloadtile(i,1);
273
    for (i=GROWSPARK; i<GROWSPARK+4; i++) tloadtile(i,1);
274
    for (i=SHRINKEREXPLOSION; i<SHRINKEREXPLOSION+4; i++) tloadtile(i,1);
275
    for (i=MORTER; i<MORTER+4; i++) tloadtile(i,4);
276
}
277
 
528 terminx 278
static int getsound(unsigned int num)
5 Plagman 279
{
634 terminx 280
    short fp = -1;
584 terminx 281
    int   l;
5 Plagman 282
 
581 terminx 283
    if (num >= MAXSOUNDS || ud.config.SoundToggle == 0) return 0;
563 terminx 284
    if (ud.config.FXDevice < 0) return 0;
5 Plagman 285
 
580 terminx 286
    if (!g_sounds[num].filename) return 0;
640 terminx 287
    if (g_sounds[num].filename1)fp = kopen4load(g_sounds[num].filename1,loadfromgrouponly);
634 terminx 288
    if (fp == -1)fp = kopen4load(g_sounds[num].filename,loadfromgrouponly);
331 terminx 289
    if (fp == -1) return 0;
5 Plagman 290
 
333 terminx 291
    l = kfilelength(fp);
580 terminx 292
    g_sounds[num].soundsiz = l;
5 Plagman 293
 
333 terminx 294
    if ((ud.level_number == 0 && ud.volume_number == 0 && (num == 189 || num == 232 || num == 99 || num == 233 || num == 17)) ||
295
            (l < 12288))
5 Plagman 296
    {
580 terminx 297
        g_sounds[num].lock = 199;
793 qbix79 298
        allocache((intptr_t *)&g_sounds[num].ptr,l,(char *)&g_sounds[num].lock);
580 terminx 299
        if (g_sounds[num].ptr != NULL)
300
            kread(fp, g_sounds[num].ptr , l);
5 Plagman 301
    }
333 terminx 302
    kclose(fp);
5 Plagman 303
    return 1;
304
}
305
 
395 terminx 306
static void precachenecessarysounds(void)
5 Plagman 307
{
528 terminx 308
    int i, j;
5 Plagman 309
 
563 terminx 310
    if (ud.config.FXDevice < 0) return;
5 Plagman 311
    j = 0;
312
 
581 terminx 313
    for (i=0;i<MAXSOUNDS;i++)
580 terminx 314
        if (g_sounds[i].ptr == 0)
5 Plagman 315
        {
316
            j++;
333 terminx 317
            if ((j&7) == 0)
335 terminx 318
            {
319
                handleevents();
320
                getpackets();
321
            }
5 Plagman 322
            getsound(i);
323
        }
324
}
325
 
398 terminx 326
static void dofrontscreens(char *statustext)
327
{
584 terminx 328
    int i=0,j;
398 terminx 329
 
330
    if (ud.recstat != 2)
331
    {
332
        if (!statustext)
333
        {
564 terminx 334
            //g_player[myconnectindex].ps->palette = palette;
335
            setgamepalette(g_player[myconnectindex].ps, palette, 1);    // JBF 20040308
398 terminx 336
            fadepal(0,0,0, 0,64,7);
337
            i = ud.screen_size;
338
            ud.screen_size = 0;
339
            vscrn();
340
            clearview(0L);
341
        }
342
 
343
        SetGameVarID(g_iReturnVarID,LOADSCREEN, -1, -1);
344
        OnEvent(EVENT_GETLOADTILE, -1, myconnectindex, -1);
345
        j = GetGameVarID(g_iReturnVarID, -1, -1);
346
        rotatesprite(320<<15,200<<15,65536L,0,j > MAXTILES-1?j-MAXTILES:j,0,0,2+8+64,0,0,xdim-1,ydim-1);
347
        if (j > MAXTILES-1)
348
        {
349
            nextpage();
350
            return;
351
        }
352
        if (boardfilename[0] != 0 && ud.level_number == 7 && ud.volume_number == 0)
353
        {
354
            menutext(160,90,0,0,"ENTERING USER MAP");
355
            gametextpal(160,90+10,boardfilename,14,2);
356
        }
357
        else
358
        {
359
            menutext(160,90,0,0,"ENTERING");
562 terminx 360
            if (map[(ud.volume_number*MAXLEVELS) + ud.level_number].name != NULL)
361
                menutext(160,90+16+8,0,0,map[(ud.volume_number*MAXLEVELS) + ud.level_number].name);
398 terminx 362
        }
363
 
364
        if (statustext) gametext(160,180,statustext,0,2+8+16);
365
 
763 terminx 366
        OnEvent(EVENT_DISPLAYLOADINGSCREEN, g_player[screenpeek].ps->i, screenpeek, -1);
398 terminx 367
        nextpage();
368
 
369
        if (!statustext)
370
        {
371
            fadepal(0,0,0, 63,0,-7);
372
 
373
            KB_FlushKeyboardQueue();
374
            ud.screen_size = i;
375
        }
376
    }
377
    else
378
    {
379
        if (!statustext)
380
        {
381
            clearview(0L);
564 terminx 382
            //g_player[myconnectindex].ps->palette = palette;
398 terminx 383
            //palto(0,0,0,0);
564 terminx 384
            setgamepalette(g_player[myconnectindex].ps, palette, 0);    // JBF 20040308
398 terminx 385
        }
386
        SetGameVarID(g_iReturnVarID,LOADSCREEN, -1, -1);
387
        OnEvent(EVENT_GETLOADTILE, -1, myconnectindex, -1);
388
        j = GetGameVarID(g_iReturnVarID, -1, -1);
389
        rotatesprite(320<<15,200<<15,65536L,0,j > MAXTILES-1?j-MAXTILES:j,0,0,2+8+64,0,0,xdim-1,ydim-1);
390
        if (j > MAXTILES-1)
391
        {
392
            nextpage();
393
            return;
394
        }
395
        menutext(160,105,0,0,"LOADING...");
396
        if (statustext) gametext(160,180,statustext,0,2+8+16);
763 terminx 397
        OnEvent(EVENT_DISPLAYLOADINGSCREEN, g_player[screenpeek].ps->i, screenpeek, -1);
398 terminx 398
        nextpage();
399
    }
400
}
401
 
5 Plagman 402
void cacheit(void)
403
{
855 terminx 404
    int i,j,pc=0;
584 terminx 405
    int tc;
406
    unsigned int starttime, endtime;
5 Plagman 407
 
331 terminx 408
    if (ud.recstat == 2)
29 terminx 409
        return;
410
 
693 terminx 411
    MUSIC_Pause();
412
    if (map[MAXVOLUMES*MAXLEVELS+2].musicfn1)
413
    {
414
        MUSIC_StopSong();
415
        playmusic(&env_music_fn[2][0],MAXVOLUMES*MAXLEVELS+2); // loadmus
416
    }
645 terminx 417
 
29 terminx 418
    starttime = getticks();
419
 
5 Plagman 420
    precachenecessarysounds();
421
 
422
    cachegoodsprites();
423
 
331 terminx 424
    for (i=0;i<numwalls;i++)
5 Plagman 425
    {
426
        tloadtile(wall[i].picnum, 0);
427
 
335 terminx 428
        if (wall[i].overpicnum >= 0)
429
        {
5 Plagman 430
            tloadtile(wall[i].overpicnum, 0);
431
        }
432
    }
433
 
331 terminx 434
    for (i=0;i<numsectors;i++)
5 Plagman 435
    {
333 terminx 436
        tloadtile(sector[i].floorpicnum, 0);
437
        tloadtile(sector[i].ceilingpicnum, 0);
438
        if (sector[i].ceilingpicnum == LA)  // JBF 20040509: if( waloff[sector[i].ceilingpicnum] == LA) WTF??!??!?!?
5 Plagman 439
        {
440
            tloadtile(LA+1, 0);
441
            tloadtile(LA+2, 0);
442
        }
443
 
444
        j = headspritesect[i];
331 terminx 445
        while (j >= 0)
5 Plagman 446
        {
331 terminx 447
            if (sprite[j].xrepeat != 0 && sprite[j].yrepeat != 0 && (sprite[j].cstat&32768) == 0)
5 Plagman 448
                cachespritenum(j);
449
            j = nextspritesect[j];
450
        }
451
    }
452
 
29 terminx 453
    tc = totalclock;
5 Plagman 454
    j = 0;
455
 
335 terminx 456
    for (i=0;i<MAXTILES;i++)
457
    {
458
        if (!(i&7) && !gotpic[i>>3])
459
        {
29 terminx 460
            i+=7;
461
            continue;
462
        }
335 terminx 463
        if (gotpic[i>>3] & pow2char[i&7])
464
        {
29 terminx 465
            if (waloff[i] == 0)
466
                loadtile((short)i);
5 Plagman 467
 
263 terminx 468
#if defined(POLYMOST) && defined(USE_OPENGL)
563 terminx 469
            if (ud.config.useprecache)
335 terminx 470
            {
855 terminx 471
                int k;
472
 
29 terminx 473
                if (precachehightile[0][i>>3] & pow2char[i&7])
521 terminx 474
                    for (k=0; k<MAXPALOOKUPS && !KB_KeyPressed(sc_Space); k++)
29 terminx 475
                        polymost_precache(i,k,0);
5 Plagman 476
 
29 terminx 477
                if (precachehightile[1][i>>3] & pow2char[i&7])
521 terminx 478
                    for (k=0; k<MAXPALOOKUPS && !KB_KeyPressed(sc_Space); k++)
29 terminx 479
                        polymost_precache(i,k,1);
480
            }
259 terminx 481
#endif
29 terminx 482
            j++;
483
            pc++;
335 terminx 484
        }
485
        else continue;
5 Plagman 486
 
335 terminx 487
        if ((j&7) == 0)
488
        {
489
            handleevents();
490
            getpackets();
491
        }
492
        if (totalclock - tc > TICRATE/4)
493
        {
584 terminx 494
            sprintf(tempbuf,"Loading resources... %d%%\n",min(100,100*pc/precachecount));
29 terminx 495
            dofrontscreens(tempbuf);
496
            tc = totalclock;
497
        }
5 Plagman 498
    }
499
 
500
    clearbufbyte(gotpic,sizeof(gotpic),0L);
501
 
29 terminx 502
    endtime = getticks();
503
    OSD_Printf("Cache time: %dms\n", endtime-starttime);
5 Plagman 504
}
505
 
509 terminx 506
void xyzmirror(int i,int wn)
5 Plagman 507
{
508
    //if (waloff[wn] == 0) loadtile(wn);
509
    setviewtotile(wn,tilesizy[wn],tilesizx[wn]);
510
 
511
    drawrooms(SX,SY,SZ,SA,100+sprite[i].shade,SECT);
335 terminx 512
    display_mirror = 1;
513
    animatesprites(SX,SY,SA,65536L);
514
    display_mirror = 0;
5 Plagman 515
    drawmasks();
516
 
517
    setviewback();
518
    squarerotatetile(wn);
519
    invalidatetile(wn,-1,255);
520
}
521
 
522
void vscrn(void)
523
{
584 terminx 524
    int i, j, ss, x1, x2, y1, y2;
5 Plagman 525
 
331 terminx 526
    if (ud.screen_size < 0) ud.screen_size = 0;
423 terminx 527
    if (ud.screen_size > 64) ud.screen_size = 64;
331 terminx 528
    if (ud.screen_size == 0) flushperms();
5 Plagman 529
 
530
    ss = max(ud.screen_size-8,0);
531
 
532
    x1 = scale(ss,xdim,160);
533
    x2 = xdim-x1;
534
 
335 terminx 535
    y1 = ss;
536
    y2 = 200;
333 terminx 537
    if (ud.screen_size > 0 && (gametype_flags[ud.coop]&GAMETYPE_FLAG_FRAGBAR) && ud.multimode > 1)
5 Plagman 538
    {
539
        j = 0;
331 terminx 540
        for (i=connecthead;i>=0;i=connectpoint2[i])
541
            if (i > j) j = i;
5 Plagman 542
 
543
        if (j >= 1) y1 += 8;
544
        if (j >= 4) y1 += 8;
545
        if (j >= 8) y1 += 8;
546
        if (j >= 12) y1 += 8;
547
    }
548
 
423 terminx 549
    if (ud.screen_size >= 8 && !(ud.screen_size == 8 && ud.statusbarmode && bpp > 8))
550
        y2 -= (ss+scale(tilesizy[BOTTOMSTATUSBAR],ud.statusbarscale,100));
5 Plagman 551
 
552
    y1 = scale(y1,ydim,200);
553
    y2 = scale(y2,ydim,200);
554
 
555
    setview(x1,y1,x2-1,y2-1);
556
 
557
    pub = NUMPAGES;
558
    pus = NUMPAGES;
559
}
560
 
432 terminx 561
void pickrandomspot(int snum)
5 Plagman 562
{
564 terminx 563
    player_struct *p = g_player[snum].ps;
432 terminx 564
    int i=snum,j,k;
584 terminx 565
    unsigned int dist,pdist = -1;
5 Plagman 566
 
333 terminx 567
    if (ud.multimode > 1 && !(gametype_flags[ud.coop] & GAMETYPE_FLAG_FIXEDRESPAWN))
268 terminx 568
    {
423 terminx 569
        i = TRAND%numplayersprites;
331 terminx 570
        if (gametype_flags[ud.coop] & GAMETYPE_FLAG_TDMSPAWN)
268 terminx 571
        {
331 terminx 572
            for (j=0;j<ud.multimode;j++)
268 terminx 573
            {
564 terminx 574
                if (j != snum && g_player[j].ps->team == g_player[snum].ps->team && sprite[g_player[j].ps->i].extra > 0)
268 terminx 575
                {
331 terminx 576
                    for (k=0;k<numplayersprites;k++)
268 terminx 577
                    {
564 terminx 578
                        dist = FindDistance2D(g_player[j].ps->posx-g_PlayerSpawnPoints[k].ox,g_player[j].ps->posy-g_PlayerSpawnPoints[k].oy);
331 terminx 579
                        if (dist < pdist)
268 terminx 580
                            i = k, pdist = dist;
581
                    }
582
                    break;
583
                }
584
            }
335 terminx 585
        }
268 terminx 586
    }
5 Plagman 587
 
564 terminx 588
    p->bobposx = p->oposx = p->posx = g_PlayerSpawnPoints[i].ox;
589
    p->bobposy = p->oposy = p->posy = g_PlayerSpawnPoints[i].oy;
590
    p->oposz = p->posz = g_PlayerSpawnPoints[i].oz;
591
    p->ang = g_PlayerSpawnPoints[i].oa;
592
    p->cursectnum = g_PlayerSpawnPoints[i].os;
5 Plagman 593
}
594
 
432 terminx 595
static void resetplayerstats(int snum)
5 Plagman 596
{
564 terminx 597
    player_struct *p = g_player[snum].ps;
5 Plagman 598
 
599
    ud.show_help        = 0;
600
    ud.showallmap       = 0;
601
    p->dead_flag        = 0;
602
    p->wackedbyactor    = -1;
603
    p->falling_counter  = 0;
604
    p->quick_kick       = 0;
605
    p->subweapon        = 0;
606
    p->last_full_weapon = 0;
607
    p->ftq              = 0;
608
    p->fta              = 0;
609
    p->tipincs          = 0;
610
    p->buttonpalette    = 0;
611
    p->actorsqu         =-1;
612
    p->invdisptime      = 0;
613
    p->refresh_inventory= 0;
614
    p->last_pissed_time = 0;
615
    p->holster_weapon   = 0;
616
    p->pycount          = 0;
617
    p->pyoff            = 0;
618
    p->opyoff           = 0;
619
    p->loogcnt          = 0;
620
    p->angvel           = 0;
621
    p->weapon_sway      = 0;
622
    //    p->select_dir       = 0;
623
    p->extra_extra8     = 0;
624
    p->show_empty_weapon= 0;
625
    p->dummyplayersprite=-1;
626
    p->crack_time       = 0;
627
    p->hbomb_hold_delay = 0;
628
    p->transporter_hold = 0;
629
    p->wantweaponfire  = -1;
630
    p->hurt_delay       = 0;
631
    p->footprintcount   = 0;
632
    p->footprintpal     = 0;
633
    p->footprintshade   = 0;
634
    p->jumping_toggle   = 0;
635
    p->ohoriz = p->horiz= 140;
636
    p->horizoff         = 0;
637
    p->bobcounter       = 0;
638
    p->on_ground        = 0;
639
    p->player_par       = 0;
640
    p->return_to_center = 9;
641
    p->airleft          = 15*26;
642
    p->rapid_fire_hold  = 0;
643
    p->toggle_key_flag  = 0;
644
    p->access_spritenum = -1;
331 terminx 645
    if (ud.multimode > 1 && (gametype_flags[ud.coop] & GAMETYPE_FLAG_ACCESSATSTART))
5 Plagman 646
        p->got_access = 7;
647
    else p->got_access      = 0;
648
    p->random_club_frame= 0;
649
    pus = 1;
650
    p->on_warping_sector = 0;
651
    p->spritebridge      = 0;
652
    p->sbs          = 0;
653
    p->palette = (char *) &palette[0];
654
 
333 terminx 655
    if (p->steroids_amount < 400)
5 Plagman 656
    {
657
        p->steroids_amount = 0;
658
        p->inven_icon = 0;
659
    }
660
    p->heat_on =            0;
661
    p->jetpack_on =         0;
662
    p->holoduke_on =       -1;
663
 
664
    p->look_ang          = 512 - ((ud.level_number&1)<<10);
665
 
666
    p->rotscrnang        = 0;
667
    p->orotscrnang       = 1;   // JBF 20031220
668
    p->newowner          =-1;
669
    p->jumping_counter   = 0;
670
    p->hard_landing      = 0;
671
    p->posxv             = 0;
672
    p->posyv             = 0;
673
    p->poszv             = 0;
674
    fricxv            = 0;
675
    fricyv            = 0;
676
    p->somethingonplayer =-1;
677
    p->one_eighty_count  = 0;
678
    p->cheat_phase       = 0;
679
 
680
    p->on_crane          = -1;
681
 
333 terminx 682
    if ((aplWeaponWorksLike[p->curr_weapon][snum] == PISTOL_WEAPON) &&
683
            (aplWeaponReload[p->curr_weapon][snum] > aplWeaponTotalTime[p->curr_weapon][snum]))
5 Plagman 684
        p->kickback_pic  = aplWeaponTotalTime[p->curr_weapon][snum]+1;
685
    else p->kickback_pic = 0;
686
 
687
    p->weapon_pos        = 6;
688
    p->walking_snd_toggle= 0;
689
    p->weapon_ang        = 0;
690
 
691
    p->knuckle_incs      = 1;
692
    p->fist_incs = 0;
693
    p->knee_incs         = 0;
694
    p->jetpack_on        = 0;
695
    p->reloading        = 0;
696
 
172 terminx 697
    p->movement_lock     = 0;
5 Plagman 698
 
699
    setpal(p);
700
    OnEvent(EVENT_RESETPLAYER, p->i, snum, -1);
701
}
702
 
432 terminx 703
void resetweapons(int snum)
5 Plagman 704
{
432 terminx 705
    int weapon;
564 terminx 706
    player_struct *p = g_player[snum].ps;
5 Plagman 707
 
333 terminx 708
    for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS; weapon++)
5 Plagman 709
        p->gotweapon[weapon] = 0;
333 terminx 710
    for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS; weapon++)
5 Plagman 711
        p->ammo_amount[weapon] = 0;
712
 
713
    p->weapon_pos = 6;
714
    p->kickback_pic = 5;
715
    p->curr_weapon = PISTOL_WEAPON;
716
    p->gotweapon[PISTOL_WEAPON] = 1;
717
    p->gotweapon[KNEE_WEAPON] = 1;
718
    p->ammo_amount[PISTOL_WEAPON] = 48;
719
    p->gotweapon[HANDREMOTE_WEAPON] = 1;
720
    p->last_weapon = -1;
721
 
722
    p->show_empty_weapon= 0;
723
    p->last_pissed_time = 0;
724
    p->holster_weapon = 0;
725
    OnEvent(EVENT_RESETWEAPONS, p->i, snum, -1);
726
}
727
 
432 terminx 728
void resetinventory(int snum)
5 Plagman 729
{
564 terminx 730
    player_struct *p = g_player[snum].ps;
5 Plagman 731
 
732
    p->inven_icon       = 0;
733
    p->boot_amount = 0;
335 terminx 734
    p->scuba_on =           0;
735
    p->scuba_amount =         0;
736
    p->heat_amount        = 0;
737
    p->heat_on = 0;
738
    p->jetpack_on =         0;
739
    p->jetpack_amount =       0;
566 terminx 740
    p->shield_amount =      start_armour_amount;
5 Plagman 741
    p->holoduke_on = -1;
742
    p->holoduke_amount =    0;
743
    p->firstaid_amount = 0;
744
    p->steroids_amount = 0;
745
    p->inven_icon = 0;
746
    OnEvent(EVENT_RESETINVENTORY, p->i, snum, -1);
747
}
748
 
432 terminx 749
static void resetprestat(int snum,int g)
5 Plagman 750
{
564 terminx 751
    player_struct *p = g_player[snum].ps;
432 terminx 752
    int i;
5 Plagman 753
 
754
    spriteqloc = 0;
331 terminx 755
    for (i=0;i<spriteqamount;i++) spriteq[i] = -1;
5 Plagman 756
 
757
    p->hbomb_on          = 0;
758
    p->cheat_phase       = 0;
759
    p->pals_time         = 0;
760
    p->toggle_key_flag   = 0;
761
    p->secret_rooms      = 0;
762
    p->max_secret_rooms  = 0;
763
    p->actors_killed     = 0;
764
    p->max_actors_killed = 0;
765
    p->lastrandomspot = 0;
766
    p->weapon_pos = 6;
767
    p->kickback_pic = 5;
768
    p->last_weapon = -1;
769
    p->weapreccnt = 0;
770
    p->interface_toggle_flag = 0;
771
    p->show_empty_weapon= 0;
772
    p->holster_weapon = 0;
773
    p->last_pissed_time = 0;
774
 
775
    p->one_parallax_sectnum = -1;
776
    p->visibility = ud.const_visibility;
777
 
778
    screenpeek              = myconnectindex;
779
    numanimwalls            = 0;
780
    numcyclers              = 0;
781
    animatecnt              = 0;
782
    parallaxtype            = 0;
783
    randomseed              = 17L;
784
    ud.pause_on             = 0;
785
    ud.camerasprite         =-1;
786
    ud.eog                  = 0;
787
    tempwallptr             = 0;
788
    camsprite               =-1;
789
    earthquaketime          = 0;
790
 
791
    numinterpolations = 0;
792
    startofdynamicinterpolations = 0;
793
 
333 terminx 794
    if (((g&MODE_EOL) != MODE_EOL && numplayers < 2) || (!(gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && numplayers > 1))
5 Plagman 795
    {
796
        resetweapons(snum);
797
        resetinventory(snum);
798
    }
331 terminx 799
    else if (p->curr_weapon == HANDREMOTE_WEAPON)
5 Plagman 800
    {
801
        p->ammo_amount[HANDBOMB_WEAPON]++;
802
        p->curr_weapon = HANDBOMB_WEAPON;
803
    }
804
 
805
    p->timebeforeexit   = 0;
806
    p->customexitsound  = 0;
807
 
808
}
809
 
395 terminx 810
static void setupbackdrop(short sky)
5 Plagman 811
{
812
    short i;
813
 
331 terminx 814
    for (i=0;i<MAXPSKYTILES;i++) pskyoff[i]=0;
5 Plagman 815
 
331 terminx 816
    if (parallaxyscale != 65536L)
5 Plagman 817
        parallaxyscale = 32768;
818
 
331 terminx 819
    switch (dynamictostatic[sky])
5 Plagman 820
    {
337 terminx 821
    case CLOUDYOCEAN__STATIC:
822
        parallaxyscale = 65536L;
823
        break;
824
    case MOONSKY1__STATIC :
825
        pskyoff[6]=1;
826
        pskyoff[1]=2;
827
        pskyoff[4]=2;
828
        pskyoff[2]=3;
829
        break;
830
    case BIGORBIT1__STATIC: // orbit
831
        pskyoff[5]=1;
832
        pskyoff[6]=2;
833
        pskyoff[7]=3;
834
        pskyoff[2]=4;
835
        break;
836
    case LA__STATIC:
837
        parallaxyscale = 16384+1024;
838
        pskyoff[0]=1;
839
        pskyoff[1]=2;
840
        pskyoff[2]=1;
841
        pskyoff[3]=3;
842
        pskyoff[4]=4;
843
        pskyoff[5]=0;
844
        pskyoff[6]=2;
845
        pskyoff[7]=3;
846
        break;
5 Plagman 847
    }
848
 
849
    pskybits=3;
850
}
851
 
395 terminx 852
static void prelevel(char g)
5 Plagman 853
{
528 terminx 854
    int i, nexti, j, startwall, endwall, lotaglist;
855
    int lotags[MAXSPRITES];
5 Plagman 856
    int switchpicnum;
857
 
858
 
859
    clearbufbyte(show2dsector,sizeof(show2dsector),0L);
860
    clearbufbyte(show2dwall,sizeof(show2dwall),0L);
861
    clearbufbyte(show2dsprite,sizeof(show2dsprite),0L);
862
 
863
    resetprestat(0,g);
864
    numclouds = 0;
865
 
331 terminx 866
    for (i=0;i<numsectors;i++)
5 Plagman 867
    {
868
        sector[i].extra = 256;
869
 
331 terminx 870
        switch (sector[i].lotag)
5 Plagman 871
        {
337 terminx 872
        case 20:
873
        case 22:
874
            if (sector[i].floorz > sector[i].ceilingz)
875
                sector[i].lotag |= 32768;
876
            continue;
5 Plagman 877
        }
878
 
331 terminx 879
        if (sector[i].ceilingstat&1)
5 Plagman 880
        {
331 terminx 881
            if (waloff[sector[i].ceilingpicnum] == 0)
5 Plagman 882
            {
331 terminx 883
                if (sector[i].ceilingpicnum == LA)
884
                    for (j=0;j<5;j++)
5 Plagman 885
                        tloadtile(sector[i].ceilingpicnum+j, 0);
886
            }
887
            setupbackdrop(sector[i].ceilingpicnum);
888
 
331 terminx 889
            if (sector[i].ceilingpicnum == CLOUDYSKIES && numclouds < 127)
5 Plagman 890
                clouds[numclouds++] = i;
891
 
564 terminx 892
            if (g_player[0].ps->one_parallax_sectnum == -1)
893
                g_player[0].ps->one_parallax_sectnum = i;
5 Plagman 894
        }
895
 
331 terminx 896
        if (sector[i].lotag == 32767) //Found a secret room
5 Plagman 897
        {
564 terminx 898
            g_player[0].ps->max_secret_rooms++;
5 Plagman 899
            continue;
900
        }
901
 
331 terminx 902
        if (sector[i].lotag == -1)
5 Plagman 903
        {
564 terminx 904
            g_player[0].ps->exitx = wall[sector[i].wallptr].x;
905
            g_player[0].ps->exity = wall[sector[i].wallptr].y;
5 Plagman 906
            continue;
907
        }
908
    }
909
 
910
    i = headspritestat[0];
331 terminx 911
    while (i >= 0)
5 Plagman 912
    {
913
        nexti = nextspritestat[i];
299 terminx 914
        ResetActorGameVars(i);
394 terminx 915
        LoadActor(i);
599 terminx 916
        OnEvent(EVENT_LOADACTOR, i, -1, -1);
333 terminx 917
        if (sprite[i].lotag == -1 && (sprite[i].cstat&16))
5 Plagman 918
        {
564 terminx 919
            g_player[0].ps->exitx = SX;
920
            g_player[0].ps->exity = SY;
5 Plagman 921
        }
331 terminx 922
        else switch (dynamictostatic[PN])
5 Plagman 923
            {
337 terminx 924
            case GPSPEED__STATIC:
925
                sector[SECT].extra = SLT;
926
                deletesprite(i);
927
                break;
5 Plagman 928
 
337 terminx 929
            case CYCLER__STATIC:
930
                if (numcyclers >= MAXCYCLERS)
931
                {
932
                    Bsprintf(tempbuf,"\nToo many cycling sectors (%d max).",MAXCYCLERS);
933
                    gameexit(tempbuf);
934
                }
935
                cyclers[numcyclers][0] = SECT;
936
                cyclers[numcyclers][1] = SLT;
937
                cyclers[numcyclers][2] = SS;
938
                cyclers[numcyclers][3] = sector[SECT].floorshade;
939
                cyclers[numcyclers][4] = SHT;
940
                cyclers[numcyclers][5] = (SA == 1536);
941
                numcyclers++;
942
                deletesprite(i);
943
                break;
5 Plagman 944
 
337 terminx 945
            case SECTOREFFECTOR__STATIC:
946
            case ACTIVATOR__STATIC:
947
            case TOUCHPLATE__STATIC:
948
            case ACTIVATORLOCKED__STATIC:
949
            case MUSICANDSFX__STATIC:
950
            case LOCATORS__STATIC:
951
            case MASTERSWITCH__STATIC:
952
            case RESPAWN__STATIC:
953
                sprite[i].cstat = 0;
954
                break;
5 Plagman 955
            }
956
        i = nexti;
957
    }
958
 
331 terminx 959
    for (i=0;i < MAXSPRITES;i++)
5 Plagman 960
    {
331 terminx 961
        if (sprite[i].statnum < MAXSTATUS)
5 Plagman 962
        {
331 terminx 963
            if (PN == SECTOREFFECTOR && SLT == 14)
5 Plagman 964
                continue;
965
            spawn(-1,i);
966
        }
967
    }
968
 
331 terminx 969
    for (i=0;i < MAXSPRITES;i++)
970
        if (sprite[i].statnum < MAXSTATUS)
5 Plagman 971
        {
333 terminx 972
            if (PN == SECTOREFFECTOR && SLT == 14)
5 Plagman 973
                spawn(-1,i);
974
        }
975
 
976
    lotaglist = 0;
977
 
978
    i = headspritestat[0];
331 terminx 979
    while (i >= 0)
5 Plagman 980
    {
331 terminx 981
        switch (dynamictostatic[PN-1])
5 Plagman 982
        {
337 terminx 983
        case DIPSWITCH__STATIC:
984
        case DIPSWITCH2__STATIC:
985
        case PULLSWITCH__STATIC:
986
        case HANDSWITCH__STATIC:
987
        case SLOTDOOR__STATIC:
988
        case LIGHTSWITCH__STATIC:
989
        case SPACELIGHTSWITCH__STATIC:
990
        case SPACEDOORSWITCH__STATIC:
991
        case FRANKENSTINESWITCH__STATIC:
992
        case LIGHTSWITCH2__STATIC:
993
        case POWERSWITCH1__STATIC:
994
        case LOCKSWITCH1__STATIC:
995
        case POWERSWITCH2__STATIC:
996
            for (j=0;j<lotaglist;j++)
997
                if (SLT == lotags[j])
998
                    break;
5 Plagman 999
 
337 terminx 1000
            if (j == lotaglist)
1001
            {
1002
                lotags[lotaglist] = SLT;
1003
                lotaglist++;
528 terminx 1004
                if (lotaglist > MAXSPRITES-1)
1005
                    gameexit("\nToo many switches.");
337 terminx 1006
 
1007
                j = headspritestat[3];
1008
                while (j >= 0)
335 terminx 1009
                {
337 terminx 1010
                    if (sprite[j].lotag == 12 && sprite[j].hitag == SLT)
1011
                        hittype[j].temp_data[0] = 1;
1012
                    j = nextspritestat[j];
5 Plagman 1013
                }
337 terminx 1014
            }
1015
            break;
5 Plagman 1016
        }
1017
        i = nextspritestat[i];
1018
    }
1019
 
1020
    mirrorcnt = 0;
1021
 
333 terminx 1022
    for (i = 0; i < numwalls; i++)
5 Plagman 1023
    {
1024
        walltype *wal;
1025
        wal = &wall[i];
1026
 
331 terminx 1027
        if (wal->overpicnum == MIRROR && (wal->cstat&32) != 0)
5 Plagman 1028
        {
1029
            j = wal->nextsector;
1030
 
331 terminx 1031
            if (mirrorcnt > 63)
5 Plagman 1032
                gameexit("\nToo many mirrors (64 max.)");
333 terminx 1033
            if ((j >= 0) && sector[j].ceilingpicnum != MIRROR)
5 Plagman 1034
            {
1035
                sector[j].ceilingpicnum = MIRROR;
1036
                sector[j].floorpicnum = MIRROR;
1037
                mirrorwall[mirrorcnt] = i;
1038
                mirrorsector[mirrorcnt] = j;
1039
                mirrorcnt++;
1040
                continue;
1041
            }
1042
        }
1043
 
331 terminx 1044
        if (numanimwalls >= MAXANIMWALLS)
5 Plagman 1045
        {
1046
            Bsprintf(tempbuf,"\nToo many 'anim' walls (%d max).",MAXANIMWALLS);
1047
            gameexit(tempbuf);
1048
        }
1049
 
1050
        animwall[numanimwalls].tag = 0;
1051
        animwall[numanimwalls].wallnum = 0;
1052
        switchpicnum = wal->overpicnum;
335 terminx 1053
        if ((wal->overpicnum > W_FORCEFIELD)&&(wal->overpicnum <= W_FORCEFIELD+2))
1054
        {
5 Plagman 1055
            switchpicnum = W_FORCEFIELD;
1056
        }
331 terminx 1057
        switch (dynamictostatic[switchpicnum])
5 Plagman 1058
        {
337 terminx 1059
        case FANSHADOW__STATIC:
1060
        case FANSPRITE__STATIC:
1061
            wall->cstat |= 65;
1062
            animwall[numanimwalls].wallnum = i;
1063
            numanimwalls++;
1064
            break;
5 Plagman 1065
 
337 terminx 1066
        case W_FORCEFIELD__STATIC:
1067
            if (wal->overpicnum==W_FORCEFIELD__STATIC)
1068
                for (j=0;j<3;j++)
1069
                    tloadtile(W_FORCEFIELD+j, 0);
1070
            if (wal->shade > 31)
1071
                wal->cstat = 0;
1072
            else wal->cstat |= 85+256;
5 Plagman 1073
 
1074
 
337 terminx 1075
            if (wal->lotag && wal->nextwall >= 0)
1076
                wall[wal->nextwall].lotag =
1077
                    wal->lotag;
5 Plagman 1078
 
337 terminx 1079
        case BIGFORCE__STATIC:
5 Plagman 1080
 
337 terminx 1081
            animwall[numanimwalls].wallnum = i;
1082
            numanimwalls++;
5 Plagman 1083
 
337 terminx 1084
            continue;
5 Plagman 1085
        }
1086
 
1087
        wal->extra = -1;
1088
 
331 terminx 1089
        switch (dynamictostatic[wal->picnum])
5 Plagman 1090
        {
337 terminx 1091
        case WATERTILE2__STATIC:
1092
            for (j=0;j<3;j++)
1093
                tloadtile(wal->picnum+j, 0);
1094
            break;
5 Plagman 1095
 
337 terminx 1096
        case TECHLIGHT2__STATIC:
1097
        case TECHLIGHT4__STATIC:
1098
            tloadtile(wal->picnum, 0);
1099
            break;
1100
        case W_TECHWALL1__STATIC:
1101
        case W_TECHWALL2__STATIC:
1102
        case W_TECHWALL3__STATIC:
1103
        case W_TECHWALL4__STATIC:
1104
            animwall[numanimwalls].wallnum = i;
1105
            //                animwall[numanimwalls].tag = -1;
1106
            numanimwalls++;
1107
            break;
1108
        case SCREENBREAK6__STATIC:
1109
        case SCREENBREAK7__STATIC:
1110
        case SCREENBREAK8__STATIC:
1111
            for (j=SCREENBREAK6;j<SCREENBREAK9;j++)
1112
                tloadtile(j, 0);
1113
            animwall[numanimwalls].wallnum = i;
1114
            animwall[numanimwalls].tag = -1;
1115
            numanimwalls++;
1116
            break;
5 Plagman 1117
 
337 terminx 1118
        case FEMPIC1__STATIC:
1119
        case FEMPIC2__STATIC:
1120
        case FEMPIC3__STATIC:
5 Plagman 1121
 
337 terminx 1122
            wal->extra = wal->picnum;
1123
            animwall[numanimwalls].tag = -1;
1124
            if (ud.lockout)
1125
            {
1126
                if (wal->picnum == FEMPIC1)
1127
                    wal->picnum = BLANKSCREEN;
1128
                else wal->picnum = SCREENBREAK6;
1129
            }
5 Plagman 1130
 
337 terminx 1131
            animwall[numanimwalls].wallnum = i;
1132
            animwall[numanimwalls].tag = wal->picnum;
1133
            numanimwalls++;
1134
            break;
5 Plagman 1135
 
337 terminx 1136
        case SCREENBREAK1__STATIC:
1137
        case SCREENBREAK2__STATIC:
1138
        case SCREENBREAK3__STATIC:
1139
        case SCREENBREAK4__STATIC:
1140
        case SCREENBREAK5__STATIC:
5 Plagman 1141
 
337 terminx 1142
        case SCREENBREAK9__STATIC:
1143
        case SCREENBREAK10__STATIC:
1144
        case SCREENBREAK11__STATIC:
1145
        case SCREENBREAK12__STATIC:
1146
        case SCREENBREAK13__STATIC:
1147
        case SCREENBREAK14__STATIC:
1148
        case SCREENBREAK15__STATIC:
1149
        case SCREENBREAK16__STATIC:
1150
        case SCREENBREAK17__STATIC:
1151
        case SCREENBREAK18__STATIC:
1152
        case SCREENBREAK19__STATIC:
5 Plagman 1153
 
337 terminx 1154
            animwall[numanimwalls].wallnum = i;
1155
            animwall[numanimwalls].tag = wal->picnum;
1156
            numanimwalls++;
1157
            break;
5 Plagman 1158
        }
1159
    }
1160
 
1161
    //Invalidate textures in sector behind mirror
331 terminx 1162
    for (i=0;i<mirrorcnt;i++)
5 Plagman 1163
    {
1164
        startwall = sector[mirrorsector[i]].wallptr;
1165
        endwall = startwall + sector[mirrorsector[i]].wallnum;
331 terminx 1166
        for (j=startwall;j<endwall;j++)
5 Plagman 1167
        {
1168
            wall[j].picnum = MIRROR;
1169
            wall[j].overpicnum = MIRROR;
1170
        }
1171
    }
1172
}
1173
 
509 terminx 1174
void newgame(int vn,int ln,int sk)
5 Plagman 1175
{
564 terminx 1176
    player_struct *p = g_player[0].ps;
509 terminx 1177
    int i;
5 Plagman 1178
 
423 terminx 1179
    handleevents();
1180
    getpackets();
1181
 
563 terminx 1182
    if (globalskillsound >= 0 && ud.config.FXDevice >= 0 && ud.config.SoundToggle)
423 terminx 1183
    {
335 terminx 1184
        while (issoundplaying(-1,globalskillsound))
1185
        {
1186
            handleevents();
1187
            getpackets();
1188
        }
1189
    }
428 terminx 1190
 
5 Plagman 1191
    globalskillsound = -1;
1192
 
1193
    waitforeverybody();
1194
    ready2send = 0;
1195
 
333 terminx 1196
    if (ud.m_recstat != 2 && ud.last_level >= 0 && ud.multimode > 1 && (ud.coop&GAMETYPE_FLAG_SCORESHEET))
5 Plagman 1197
        dobonus(1);
1198
 
333 terminx 1199
    if (ln == 0 && vn == 3 && ud.multimode < 2 && ud.lockout == 0)
5 Plagman 1200
    {
680 terminx 1201
        playmusic(&env_music_fn[1][0],MAXVOLUMES*MAXLEVELS+1);
5 Plagman 1202
 
1203
        flushperms();
1204
        setview(0,0,xdim-1,ydim-1);
1205
        clearview(0L);
1206
        nextpage();
1207
 
1208
        playanm("vol41a.anm",6);
1209
        clearview(0L);
1210
        nextpage();
1211
 
1212
        playanm("vol42a.anm",7);
1213
        playanm("vol43a.anm",9);
1214
        clearview(0L);
1215
        nextpage();
1216
 
1217
        FX_StopAllSounds();
1218
    }
1219
 
1220
    show_shareware = 26*34;
1221
 
1222
    ud.level_number =   ln;
1223
    ud.volume_number =  vn;
1224
    ud.player_skill =   sk;
1225
    ud.secretlevel =    0;
1226
    ud.from_bonus = 0;
1227
    parallaxyscale = 0;
1228
 
1229
    ud.last_level = -1;
1230
    lastsavedpos = -1;
1231
    p->zoom            = 768;
1232
    p->gm              = 0;
1233
 
423 terminx 1234
    //AddLog("Newgame");
1235
    ResetGameVars();
5 Plagman 1236
 
423 terminx 1237
    InitGameVarPointers();
5 Plagman 1238
 
423 terminx 1239
    ResetSystemDefaults();
5 Plagman 1240
 
862 terminx 1241
    for (i=0;i<(MAXVOLUMES*MAXLEVELS);i++)
1242
        if (map[i].savedstate)
1243
        {
1244
            Bfree(map[i].savedstate);
1245
            map[i].savedstate = NULL;
1246
        }
1247
 
423 terminx 1248
    if (ud.m_coop != 1)
1249
    {
1250
        for (i=0;i<MAX_WEAPONS;i++)
5 Plagman 1251
        {
423 terminx 1252
            if (aplWeaponWorksLike[i][0]==PISTOL_WEAPON)
5 Plagman 1253
            {
423 terminx 1254
                p->curr_weapon = i;
1255
                p->gotweapon[i] = 1;
1256
                p->ammo_amount[i] = 48;
5 Plagman 1257
            }
423 terminx 1258
            else if (aplWeaponWorksLike[i][0]==KNEE_WEAPON)
1259
                p->gotweapon[i] = 1;
1260
            else if (aplWeaponWorksLike[i][0]==HANDREMOTE_WEAPON)
1261
                p->gotweapon[i] = 1;
5 Plagman 1262
        }
423 terminx 1263
        p->last_weapon = -1;
5 Plagman 1264
    }
428 terminx 1265
 
5 Plagman 1266
    display_mirror =        0;
1267
 
333 terminx 1268
    if (ud.multimode > 1)
5 Plagman 1269
    {
331 terminx 1270
        if (numplayers < 2)
5 Plagman 1271
        {
1272
            connecthead = 0;
331 terminx 1273
            for (i=0;i<MAXPLAYERS;i++) connectpoint2[i] = i+1;
5 Plagman 1274
            connectpoint2[ud.multimode-1] = -1;
1275
        }
1276
    }
1277
    else
1278
    {
1279
        connecthead = 0;
1280
        connectpoint2[0] = -1;
1281
    }
1282
}
1283
 
471 terminx 1284
int getteampal(int team)
1285
{
1286
    switch (team)
1287
    {
1288
    case 0:
472 terminx 1289
        return 3;
1290
    case 1:
471 terminx 1291
        return 10;
1292
    case 2:
1293
        return 11;
1294
    case 3:
1295
        return 12;
1296
    }
1297
    return 0;
1298
}
1299
 
395 terminx 1300
static void resetpspritevars(char g)
5 Plagman 1301
{
1302
    short i, j, nexti,circ;
864 terminx 1303
//    int firstx,firsty;
5 Plagman 1304
    spritetype *s;
1305
    char aimmode[MAXPLAYERS],autoaim[MAXPLAYERS],weaponswitch[MAXPLAYERS];
1306
    STATUSBARTYPE tsbar[MAXPLAYERS];
1307
 
564 terminx 1308
    EGS(g_player[0].ps->cursectnum,g_player[0].ps->posx,g_player[0].ps->posy,g_player[0].ps->posz,
1309
        APLAYER,0,0,0,g_player[0].ps->ang,0,0,0,10);
5 Plagman 1310
 
564 terminx 1311
    if (ud.recstat != 2) for (i=0;i<ud.multimode;i++)
5 Plagman 1312
        {
564 terminx 1313
            aimmode[i] = g_player[i].ps->aim_mode;
1314
            autoaim[i] = g_player[i].ps->auto_aim;
1315
            weaponswitch[i] = g_player[i].ps->weaponswitch;
331 terminx 1316
            if (ud.multimode > 1 && (gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
5 Plagman 1317
            {
331 terminx 1318
                for (j=0;j<MAX_WEAPONS;j++)
5 Plagman 1319
                {
564 terminx 1320
                    tsbar[i].ammo_amount[j] = g_player[i].ps->ammo_amount[j];
1321
                    tsbar[i].gotweapon[j] = g_player[i].ps->gotweapon[j];
5 Plagman 1322
                }
1323
 
564 terminx 1324
                tsbar[i].shield_amount = g_player[i].ps->shield_amount;
1325
                tsbar[i].curr_weapon = g_player[i].ps->curr_weapon;
1326
                tsbar[i].inven_icon = g_player[i].ps->inven_icon;
5 Plagman 1327
 
564 terminx 1328
                tsbar[i].firstaid_amount = g_player[i].ps->firstaid_amount;
1329
                tsbar[i].steroids_amount = g_player[i].ps->steroids_amount;
1330
                tsbar[i].holoduke_amount = g_player[i].ps->holoduke_amount;
1331
                tsbar[i].jetpack_amount = g_player[i].ps->jetpack_amount;
1332
                tsbar[i].heat_amount = g_player[i].ps->heat_amount;
1333
                tsbar[i].scuba_amount = g_player[i].ps->scuba_amount;
1334
                tsbar[i].boot_amount = g_player[i].ps->boot_amount;
5 Plagman 1335
            }
1336
        }
1337
 
1338
    resetplayerstats(0);
1339
 
564 terminx 1340
    for (i=1;i<ud.multimode;i++)
565 terminx 1341
        memcpy(g_player[i].ps,g_player[0].ps,sizeof(player_struct));
5 Plagman 1342
 
514 terminx 1343
    if (ud.recstat != 2)
564 terminx 1344
        for (i=0;i<ud.multimode;i++)
5 Plagman 1345
        {
564 terminx 1346
            g_player[i].ps->aim_mode = aimmode[i];
1347
            g_player[i].ps->auto_aim = autoaim[i];
1348
            g_player[i].ps->weaponswitch = weaponswitch[i];
331 terminx 1349
            if (ud.multimode > 1 && (gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
5 Plagman 1350
            {
331 terminx 1351
                for (j=0;j<MAX_WEAPONS;j++)
5 Plagman 1352
                {
564 terminx 1353
                    g_player[i].ps->ammo_amount[j] = tsbar[i].ammo_amount[j];
1354
                    g_player[i].ps->gotweapon[j] = tsbar[i].gotweapon[j];
5 Plagman 1355
                }
564 terminx 1356
                g_player[i].ps->shield_amount = tsbar[i].shield_amount;
1357
                g_player[i].ps->curr_weapon = tsbar[i].curr_weapon;
1358
                g_player[i].ps->inven_icon = tsbar[i].inven_icon;
5 Plagman 1359
 
564 terminx 1360
                g_player[i].ps->firstaid_amount = tsbar[i].firstaid_amount;
1361
                g_player[i].ps->steroids_amount= tsbar[i].steroids_amount;
1362
                g_player[i].ps->holoduke_amount = tsbar[i].holoduke_amount;
1363
                g_player[i].ps->jetpack_amount = tsbar[i].jetpack_amount;
1364
                g_player[i].ps->heat_amount = tsbar[i].heat_amount;
1365
                g_player[i].ps->scuba_amount= tsbar[i].scuba_amount;
1366
                g_player[i].ps->boot_amount = tsbar[i].boot_amount;
5 Plagman 1367
            }
1368
        }
1369
 
1370
    numplayersprites = 0;
1371
    circ = 2048/ud.multimode;
1372
 
1373
    which_palookup = 9;
1374
    j = connecthead;
1375
    i = headspritestat[10];
331 terminx 1376
    while (i >= 0)
5 Plagman 1377
    {
1378
        nexti = nextspritestat[i];
1379
        s = &sprite[i];
1380
 
333 terminx 1381
        if (numplayersprites == MAXPLAYERS)
5 Plagman 1382
            gameexit("\nToo many player sprites (max 16.)");
1383
 
872 terminx 1384
        /*        if (numplayersprites == 0)
1385
                {
1386
                    firstx = g_player[0].ps->posx;
1387
                    firsty = g_player[0].ps->posy;
1388
                }*/
5 Plagman 1389
 
564 terminx 1390
        g_PlayerSpawnPoints[(unsigned char)numplayersprites].ox = s->x;
1391
        g_PlayerSpawnPoints[(unsigned char)numplayersprites].oy = s->y;
1392
        g_PlayerSpawnPoints[(unsigned char)numplayersprites].oz = s->z;
1393
        g_PlayerSpawnPoints[(unsigned char)numplayersprites].oa = s->ang;
1394
        g_PlayerSpawnPoints[(unsigned char)numplayersprites].os = s->sectnum;
5 Plagman 1395
 
1396
        numplayersprites++;
331 terminx 1397
        if (j >= 0)
5 Plagman 1398
        {
1399
            s->owner = i;
1400
            s->shade = 0;
1401
            s->xrepeat = 42;
1402
            s->yrepeat = 36;
1403
            s->cstat = 1+256;
1404
            s->xoffset = 0;
1405
            s->clipdist = 64;
1406
 
564 terminx 1407
            if ((g&MODE_EOL) != MODE_EOL || g_player[j].ps->last_extra == 0)
5 Plagman 1408
            {
566 terminx 1409
                g_player[j].ps->last_extra = g_player[j].ps->max_player_health;
1410
                s->extra = g_player[j].ps->max_player_health;
564 terminx 1411
                g_player[j].ps->runspeed = dukefriction;
5 Plagman 1412
            }
564 terminx 1413
            else s->extra = g_player[j].ps->last_extra;
5 Plagman 1414
 
1415
            s->yvel = j;
1416
 
563 terminx 1417
            if (!g_player[j].pcolor && ud.multimode > 1 && !(gametype_flags[ud.coop] & GAMETYPE_FLAG_TDM))
5 Plagman 1418
            {
331 terminx 1419
                if (s->pal == 0)
53 terminx 1420
                {
514 terminx 1421
                    int k = 0;
56 terminx 1422
 
564 terminx 1423
                    for (;k<ud.multimode;k++)
335 terminx 1424
                    {
564 terminx 1425
                        if (which_palookup == g_player[k].ps->palookup)
335 terminx 1426
                        {
56 terminx 1427
                            which_palookup++;
333 terminx 1428
                            if (which_palookup >= 17)
56 terminx 1429
                                which_palookup = 9;
1430
                            k=0;
1431
                        }
1432
                    }
564 terminx 1433
                    g_player[j].pcolor = s->pal = g_player[j].ps->palookup = which_palookup++;
333 terminx 1434
                    if (which_palookup >= 17)
56 terminx 1435
                        which_palookup = 9;
53 terminx 1436
                }
564 terminx 1437
                else g_player[j].pcolor = g_player[j].ps->palookup = s->pal;
268 terminx 1438
            }
1439
            else
1440
            {
563 terminx 1441
                int k = g_player[j].pcolor;
5 Plagman 1442
 
331 terminx 1443
                if (gametype_flags[ud.coop] & GAMETYPE_FLAG_TDM)
268 terminx 1444
                {
563 terminx 1445
                    k = getteampal(g_player[j].pteam);
564 terminx 1446
                    g_player[j].ps->team = g_player[j].pteam;
268 terminx 1447
                }
564 terminx 1448
                s->pal = g_player[j].ps->palookup = k;
268 terminx 1449
            }
1450
 
564 terminx 1451
            g_player[j].ps->i = i;
1452
            g_player[j].ps->frag_ps = j;
5 Plagman 1453
            hittype[i].owner = i;
1454
 
564 terminx 1455
            hittype[i].bposx = g_player[j].ps->bobposx = g_player[j].ps->oposx = g_player[j].ps->posx =        s->x;
1456
            hittype[i].bposy = g_player[j].ps->bobposy = g_player[j].ps->oposy = g_player[j].ps->posy =        s->y;
1457
            hittype[i].bposz = g_player[j].ps->oposz = g_player[j].ps->posz =        s->z;
1458
            g_player[j].ps->oang  = g_player[j].ps->ang  =        s->ang;
5 Plagman 1459
 
564 terminx 1460
            updatesector(s->x,s->y,&g_player[j].ps->cursectnum);
5 Plagman 1461
 
1462
            j = connectpoint2[j];
1463
 
1464
        }
1465
        else deletesprite(i);
1466
        i = nexti;
1467
    }
1468
}
1469
 
400 terminx 1470
static inline void clearfrags(void)
5 Plagman 1471
{
514 terminx 1472
    int i = 0;
5 Plagman 1473
 
564 terminx 1474
    while (i<ud.multimode)
1475
    {
1476
        g_player[i].ps->frag = g_player[i].ps->fraggedself = 0, i++;
1477
        clearbufbyte(&g_player[i].frags[0],MAXPLAYERS<<1,0L);
1478
    }
5 Plagman 1479
}
1480
 
1481
void resettimevars(void)
1482
{
1483
    vel = svel = angvel = horiz = 0;
1484
 
1485
    totalclock = 0L;
1486
    cloudtotalclock = 0L;
1487
    ototalclock = 0L;
1488
    lockclock = 0L;
1489
    ready2send = 1;
1490
}
1491
 
1492
void waitforeverybody()
1493
{
584 terminx 1494
    int i;
5 Plagman 1495
 
1496
    if (numplayers < 2) return;
1497
    packbuf[0] = 250;
331 terminx 1498
    for (i=connecthead;i>=0;i=connectpoint2[i])
5 Plagman 1499
    {
1500
        if (i != myconnectindex) sendpacket(i,packbuf,1);
1501
        if ((!networkmode) && (myconnectindex != connecthead)) break; //slaves in M/S mode only send to master
1502
    }
563 terminx 1503
    g_player[myconnectindex].playerreadyflag++;
5 Plagman 1504
 
1505
    while (1)
1506
    {
375 terminx 1507
#ifdef _WIN32
1508
        Sleep(10);
1509
#else
1510
        usleep(10);
363 terminx 1511
#endif
375 terminx 1512
        sampletimer();
5 Plagman 1513
        handleevents();
1514
        AudioUpdate();
1515
 
1516
        if (quitevent || keystatus[1]) gameexit("");
1517
 
1518
        getpackets();
1519
 
331 terminx 1520
        for (i=connecthead;i>=0;i=connectpoint2[i])
5 Plagman 1521
        {
563 terminx 1522
            if (g_player[i].playerreadyflag < g_player[myconnectindex].playerreadyflag) break;
335 terminx 1523
            if ((!networkmode) && (myconnectindex != connecthead))
1524
            {
1525
                i = -1;
1526
                break;
1527
            } //slaves in M/S mode only wait for master
331 terminx 1528
 
5 Plagman 1529
        }
1530
        if (i < 0) return;
1531
    }
1532
}
1533
 
469 terminx 1534
extern int jump_input;
368 terminx 1535
 
5 Plagman 1536
void clearfifo(void)
1537
{
564 terminx 1538
    int i = 0;
563 terminx 1539
 
5 Plagman 1540
    syncvaltail = 0L;
1541
    syncvaltottail = 0L;
1542
    syncstat = 0;
1543
    bufferjitter = 1;
1544
    mymaxlag = otherminlag = 0;
368 terminx 1545
    jump_input = 0;
5 Plagman 1546
 
1547
    movefifoplc = movefifosendplc = fakemovefifoplc = 0;
275 terminx 1548
    avgfvel = avgsvel = avgavel = avghorz = avgbits = avgextbits = 0;
5 Plagman 1549
    otherminlag = mymaxlag = 0;
1550
 
564 terminx 1551
    clearbufbyte(&loc,sizeof(input),0L);
1552
    clearbufbyte(&inputfifo,sizeof(input)*MOVEFIFOSIZ*MAXPLAYERS,0L);
1553
    for (;i<MAXPLAYERS;i++)
1554
    {
1555
//              Bmemset(g_player[i].inputfifo,0,sizeof(g_player[i].inputfifo));
1556
        if (g_player[i].sync != NULL)
1557
            Bmemset(g_player[i].sync,0,sizeof(input));
1558
        Bmemset(&g_player[i].movefifoend,0,sizeof(g_player[i].movefifoend));
1559
        Bmemset(&g_player[i].syncvalhead,0,sizeof(g_player[i].syncvalhead));
1560
        Bmemset(&g_player[i].myminlag,0,sizeof(g_player[i].myminlag));
1561
        g_player[i].vote = 0;
1562
        g_player[i].gotvote = 0;
1563
    }
5 Plagman 1564
    //    clearbufbyte(playerquitflag,MAXPLAYERS,0x01);
1565
}
1566
 
1567
void resetmys(void)
1568
{
564 terminx 1569
    myx = omyx = g_player[myconnectindex].ps->posx;
1570
    myy = omyy = g_player[myconnectindex].ps->posy;
1571
    myz = omyz = g_player[myconnectindex].ps->posz;
5 Plagman 1572
    myxvel = myyvel = myzvel = 0;
564 terminx 1573
    myang = omyang = g_player[myconnectindex].ps->ang;
1574
    myhoriz = omyhoriz = g_player[myconnectindex].ps->horiz;
1575
    myhorizoff = omyhorizoff = g_player[myconnectindex].ps->horizoff;
1576
    mycursectnum = g_player[myconnectindex].ps->cursectnum;
1577
    myjumpingcounter = g_player[myconnectindex].ps->jumping_counter;
1578
    myjumpingtoggle = g_player[myconnectindex].ps->jumping_toggle;
1579
    myonground = g_player[myconnectindex].ps->on_ground;
1580
    myhardlanding = g_player[myconnectindex].ps->hard_landing;
1581
    myreturntocenter = g_player[myconnectindex].ps->return_to_center;
5 Plagman 1582
}
1583
 
564 terminx 1584
extern int voting, vote_map, vote_episode;
147 terminx 1585
 
412 terminx 1586
static void getlevelfromfilename(const char *fn, char *volume, char *level)
398 terminx 1587
{
428 terminx 1588
    for (*volume=0;*volume<MAXVOLUMES;(*volume)++)
398 terminx 1589
    {
428 terminx 1590
        for (*level=0;*level<MAXLEVELS;(*level)++)
398 terminx 1591
        {
562 terminx 1592
            if (map[(*volume*MAXLEVELS)+*level].filename != NULL)
1593
                if (!Bstrcasecmp(fn, map[(*volume*MAXLEVELS)+*level].filename))
398 terminx 1594
                    break;
1595
        }
428 terminx 1596
        if (*level != MAXLEVELS)
398 terminx 1597
            break;
1598
    }
1599
}
1600
 
509 terminx 1601
int enterlevel(int g)
5 Plagman 1602
{
485 terminx 1603
    int i;
5 Plagman 1604
    char levname[BMAX_PATH];
1605
 
371 terminx 1606
    ready2send = 1; // HACK: fetch any leftover p2p sync packets
368 terminx 1607
    KB_ClearKeysDown();
1608
    handleevents();
1609
    getpackets();
369 terminx 1610
    waitforeverybody();
375 terminx 1611
    ready2send = 0;
1612
 
564 terminx 1613
    vote_map = vote_episode = voting = -1;
1614
 
333 terminx 1615
    if ((g&MODE_DEMO) != MODE_DEMO) ud.recstat = ud.m_recstat;
5 Plagman 1616
    ud.respawn_monsters = ud.m_respawn_monsters;
1617
    ud.respawn_items    = ud.m_respawn_items;
1618
    ud.respawn_inventory    = ud.m_respawn_inventory;
1619
    ud.monsters_off = ud.m_monsters_off;
1620
    ud.coop = ud.m_coop;
1621
    ud.marker = ud.m_marker;
1622
    ud.ffire = ud.m_ffire;
147 terminx 1623
    ud.noexits = ud.m_noexits;
5 Plagman 1624
 
333 terminx 1625
    if ((g&MODE_DEMO) == 0 && ud.recstat == 2)
5 Plagman 1626
        ud.recstat = 0;
1627
 
1628
    FX_StopAllSounds();
1629
    clearsoundlocks();
1630
    FX_SetReverb(0);
1631
 
392 terminx 1632
    if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
1633
    {
400 terminx 1634
        char volume, level;
398 terminx 1635
 
412 terminx 1636
        Bcorrectfilename(boardfilename,0);
1637
 
400 terminx 1638
        getlevelfromfilename(boardfilename,&volume,&level);
398 terminx 1639
 
392 terminx 1640
        if (level != MAXLEVELS)
1641
        {
1642
            ud.level_number = ud.m_level_number = level;
1643
            ud.volume_number = ud.m_volume_number = volume;
1644
            boardfilename[0] = 0;
1645
        }
1646
    }
1647
 
562 terminx 1648
    if (map[(ud.volume_number*MAXLEVELS)+ud.level_number].name == NULL || map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename == NULL)
382 terminx 1649
    {
385 terminx 1650
        if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
1651
        {
562 terminx 1652
            if (map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename == NULL)
1653
                map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename = Bcalloc(BMAX_PATH,sizeof(char));
1654
            if (map[(ud.volume_number*MAXLEVELS)+ud.level_number].name == NULL)
385 terminx 1655
            {
841 terminx 1656
                map[(ud.volume_number*MAXLEVELS)+ud.level_number].name = Bcalloc(16,sizeof(char));
562 terminx 1657
                Bsprintf(map[(ud.volume_number*MAXLEVELS)+ud.level_number].name,"USER MAP");
385 terminx 1658
            }
1659
        }
1660
        else
1661
        {
865 terminx 1662
            OSD_Printf(OSDTEXT_RED "Map E%dL%d not defined!\n",ud.volume_number+1,ud.level_number+1);
385 terminx 1663
            return 1;
1664
        }
382 terminx 1665
    }
1666
 
5 Plagman 1667
    i = ud.screen_size;
1668
    ud.screen_size = 0;
1669
    dofrontscreens(NULL);
1670
    vscrn();
1671
    ud.screen_size = i;
1672
 
382 terminx 1673
    if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
1674
    {
1675
        Bstrcpy(levname, boardfilename);
886 terminx 1676
        Bsprintf(apptitle,HEAD2 " - %s - %s",levname,duke3dgrpstring);
382 terminx 1677
    }
886 terminx 1678
    else Bsprintf(apptitle,HEAD2 " - %s - %s",map[(ud.volume_number*MAXLEVELS)+ud.level_number].name,duke3dgrpstring);
382 terminx 1679
 
886 terminx 1680
    Bstrcpy(tempbuf,apptitle);
385 terminx 1681
    wm_setapptitle(tempbuf);
1682
 
335 terminx 1683
    if (!VOLUMEONE)
1684
    {
333 terminx 1685
        if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
5 Plagman 1686
        {
564 terminx 1687
            if (loadboard(boardfilename,0,&g_player[0].ps->posx, &g_player[0].ps->posy, &g_player[0].ps->posz, &g_player[0].ps->ang,&g_player[0].ps->cursectnum) == -1)
5 Plagman 1688
            {
514 terminx 1689
                initprintf("Map '%s' not found!\n",boardfilename);
5 Plagman 1690
                //gameexit(tempbuf);
1691
                return 1;
335 terminx 1692
            }
423 terminx 1693
 
335 terminx 1694
            {
5 Plagman 1695
                char *p;
1696
                strcpy(levname, boardfilename);
1697
                p = Bstrrchr(levname,'.');
1698
                if (!p) strcat(levname,".mhk");
335 terminx 1699
                else
1700
                {
1701
                    p[1]='m';
1702
                    p[2]='h';
1703
                    p[3]='k';
1704
                    p[4]=0;
1705
                }
514 terminx 1706
                if (!loadmaphack(levname)) initprintf("Loaded map hack file '%s'\n",levname);
5 Plagman 1707
            }
1708
        }
564 terminx 1709
        else if (loadboard(map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename,0,&g_player[0].ps->posx, &g_player[0].ps->posy, &g_player[0].ps->posz, &g_player[0].ps->ang,&g_player[0].ps->cursectnum) == -1)
5 Plagman 1710
        {
562 terminx 1711
            initprintf("Map %s not found!\n",map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename);
5 Plagman 1712
            //gameexit(tempbuf);
1713
            return 1;
335 terminx 1714
        }
1715
        else
1716
        {
5 Plagman 1717
            char *p;
562 terminx 1718
            strcpy(levname, map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename);
5 Plagman 1719
            p = Bstrrchr(levname,'.');
1720
            if (!p) strcat(levname,".mhk");
335 terminx 1721
            else
1722
            {
1723
                p[1]='m';
1724
                p[2]='h';
1725
                p[3]='k';
1726
                p[4]=0;
1727
            }
514 terminx 1728
            if (!loadmaphack(levname)) initprintf("Loaded map hack file '%s'\n",levname);
5 Plagman 1729
        }
1730
 
335 terminx 1731
    }
1732
    else
1733
    {
5 Plagman 1734
 
562 terminx 1735
        i = strlen(map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename);
1736
        copybufbyte(map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename,&levname[0],i);
485 terminx 1737
        levname[i] = 255;
1738
        levname[i+1] = 0;
5 Plagman 1739
 
564 terminx 1740
        if (loadboard(levname,1,&g_player[0].ps->posx, &g_player[0].ps->posy, &g_player[0].ps->posz, &g_player[0].ps->ang,&g_player[0].ps->cursectnum) == -1)
5 Plagman 1741
        {
562 terminx 1742
            initprintf("Map '%s' not found!\n",map[(ud.volume_number*MAXLEVELS)+ud.level_number].filename);
5 Plagman 1743
            //gameexit(tempbuf);
1744
            return 1;
335 terminx 1745
        }
1746
        else
1747
        {
5 Plagman 1748
            char *p;
1749
            p = Bstrrchr(levname,'.');
1750
            if (!p) strcat(levname,".mhk");
335 terminx 1751
            else
1752
            {
1753
                p[1]='m';
1754
                p[2]='h';
1755
                p[3]='k';
1756
                p[4]=0;
1757
            }
514 terminx 1758
            if (!loadmaphack(levname)) initprintf("Loaded map hack file '%s'\n",levname);
5 Plagman 1759
        }
1760
    }
1761
 
1762
    precachecount = 0;
1763
    clearbufbyte(gotpic,sizeof(gotpic),0L);
1764
    clearbufbyte(precachehightile, sizeof(precachehightile), 0l);
1765
    //clearbufbyte(hittype,sizeof(hittype),0l); // JBF 20040531: yes? no?
1766
 
1767
    prelevel(g);
1768
 
1769
    allignwarpelevators();
1770
    resetpspritevars(g);
1771
 
1772
    cachedebug = 0;
1773
    automapping = 0;
1774
 
1775
    cacheit();
1776
 
331 terminx 1777
    if (ud.recstat != 2)
5 Plagman 1778
    {
381 terminx 1779
        music_select = (ud.volume_number*MAXLEVELS) + ud.level_number;
562 terminx 1780
        if (map[(unsigned char)music_select].musicfn != NULL)
680 terminx 1781
            playmusic(&map[(unsigned char)music_select].musicfn[0],music_select);
5 Plagman 1782
    }
1783
 
333 terminx 1784
    if ((g&MODE_GAME) || (g&MODE_EOL))
564 terminx 1785
        g_player[myconnectindex].ps->gm = MODE_GAME;
331 terminx 1786
    else if (g&MODE_RESTART)
5 Plagman 1787
    {
331 terminx 1788
        if (ud.recstat == 2)
564 terminx 1789
            g_player[myconnectindex].ps->gm = MODE_DEMO;
1790
        else g_player[myconnectindex].ps->gm = MODE_GAME;
5 Plagman 1791
    }
1792
 
333 terminx 1793
    if ((ud.recstat == 1) && (g&MODE_RESTART) != MODE_RESTART)
5 Plagman 1794
        opendemowrite();
1795
 
335 terminx 1796
    if (VOLUMEONE)
1797
    {
564 terminx 1798
        if (ud.level_number == 0 && ud.recstat != 2) FTA(40,g_player[myconnectindex].ps);
5 Plagman 1799
    }
1800
 
331 terminx 1801
    for (i=connecthead;i>=0;i=connectpoint2[i])
564 terminx 1802
        switch (dynamictostatic[sector[sprite[g_player[i].ps->i].sectnum].floorpicnum])
5 Plagman 1803
        {
337 terminx 1804
        case HURTRAIL__STATIC:
1805
        case FLOORSLIME__STATIC:
1806
        case FLOORPLASMA__STATIC:
1807
            resetweapons(i);
1808
            resetinventory(i);
564 terminx 1809
            g_player[i].ps->gotweapon[PISTOL_WEAPON] = 0;
1810
            g_player[i].ps->ammo_amount[PISTOL_WEAPON] = 0;
1811
            g_player[i].ps->curr_weapon = KNEE_WEAPON;
1812
            g_player[i].ps->kickback_pic = 0;
337 terminx 1813
            break;
5 Plagman 1814
        }
1815
 
1816
    //PREMAP.C - replace near the my's at the end of the file
1817
 
1818
    resetmys();
1819
 
564 terminx 1820
    //g_player[myconnectindex].ps->palette = palette;
5 Plagman 1821
    //palto(0,0,0,0);
564 terminx 1822
    setgamepalette(g_player[myconnectindex].ps, palette, 0);    // JBF 20040308
5 Plagman 1823
 
564 terminx 1824
    setpal(g_player[myconnectindex].ps);
5 Plagman 1825
    flushperms();
1826
 
1827
    everyothertime = 0;
1828
    global_random = 0;
1829
 
1830
    ud.last_level = ud.level_number+1;
1831
 
1832
    clearfifo();
1833
 
331 terminx 1834
    for (i=numinterpolations-1;i>=0;i--) bakipos[i] = *curipos[i];
5 Plagman 1835
 
1836
    restorepalette = 1;
1837
 
1838
    flushpackets();
1839
    waitforeverybody();
1840
 
1841
    palto(0,0,0,0);
1842
    vscrn();
1843
    clearview(0L);
1844
    drawbackground();
1845
    displayrooms(myconnectindex,65536);
1846
 
564 terminx 1847
    for (i=0;i<ud.multimode;i++)
1848
        clearbufbyte(&g_player[i].playerquitflag,1,0x01010101);
1849
    g_player[myconnectindex].ps->over_shoulder_on = 0;
5 Plagman 1850
 
1851
    clearfrags();
1852
 
1853
    resettimevars();  // Here we go
1854
 
1855
    //Bsprintf(g_szBuf,"ENTERLEVEL L=%d V=%d",ud.level_number, ud.volume_number);
1856
    //AddLog(g_szBuf);
1857
    // variables are set by pointer...
317 terminx 1858
 
5 Plagman 1859
    OnEvent(EVENT_ENTERLEVEL, -1, -1, -1);
866 terminx 1860
    OSD_Printf(OSDTEXT_YELLOW "E%dL%d: %s\n",ud.volume_number+1,ud.level_number+1,map[(ud.volume_number*MAXLEVELS)+ud.level_number].name);
5 Plagman 1861
    return 0;
1862
}
862 terminx 1863
 
1864
void FreeMapState(int mapnum)
1865
{
1866
    int j;
1867
 
1868
    for (j=0;j<iGameVarCount;j++)
1869
    {
1870
        if (aGameVars[j].dwFlags & GAMEVAR_FLAG_NORESET) continue;
1871
        if (aGameVars[j].dwFlags & GAMEVAR_FLAG_PERPLAYER)
1872
        {
1873
            if (map[mapnum].savedstate->vars[j])
1874
                Bfree(map[mapnum].savedstate->vars[j]);
1875
        }
1876
        else if (aGameVars[j].dwFlags & GAMEVAR_FLAG_PERACTOR)
1877
        {
1878
            if (map[mapnum].savedstate->vars[j])
1879
                Bfree(map[mapnum].savedstate->vars[j]);
1880
        }
1881
    }
1882
    Bfree(map[mapnum].savedstate);
1883
    map[mapnum].savedstate = NULL;
1884
}