Subversion Repositories eduke32

Rev

Rev 428 | Rev 433 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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