Subversion Repositories eduke32

Rev

Rev 426 | Rev 432 | 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
 
526
void pickrandomspot(short snum)
527
{
528
    struct player_struct *p;
268 terminx 529
    short i=0,j,k;
530
    unsigned long dist,pdist = -1;
5 Plagman 531
 
532
    p = &ps[snum];
533
 
423 terminx 534
    i = snum;
333 terminx 535
    if (ud.multimode > 1 && !(gametype_flags[ud.coop] & GAMETYPE_FLAG_FIXEDRESPAWN))
268 terminx 536
    {
423 terminx 537
        i = TRAND%numplayersprites;
331 terminx 538
        if (gametype_flags[ud.coop] & GAMETYPE_FLAG_TDMSPAWN)
268 terminx 539
        {
331 terminx 540
            for (j=0;j<ud.multimode;j++)
268 terminx 541
            {
331 terminx 542
                if (j != snum && ps[j].team == ps[snum].team && sprite[ps[j].i].extra > 0)
268 terminx 543
                {
331 terminx 544
                    for (k=0;k<numplayersprites;k++)
268 terminx 545
                    {
546
                        dist = FindDistance2D(ps[j].posx-po[k].ox,ps[j].posy-po[k].oy);
331 terminx 547
                        if (dist < pdist)
268 terminx 548
                            i = k, pdist = dist;
549
                    }
550
                    break;
551
                }
552
            }
335 terminx 553
        }
268 terminx 554
    }
5 Plagman 555
 
556
    p->bobposx = p->oposx = p->posx = po[i].ox;
557
    p->bobposy = p->oposy = p->posy = po[i].oy;
558
    p->oposz = p->posz = po[i].oz;
559
    p->ang = po[i].oa;
560
    p->cursectnum = po[i].os;
561
}
562
 
395 terminx 563
static void resetplayerstats(short snum)
5 Plagman 564
{
565
    struct player_struct *p;
566
 
567
    p = &ps[snum];
568
 
569
    ud.show_help        = 0;
570
    ud.showallmap       = 0;
571
    p->dead_flag        = 0;
572
    p->wackedbyactor    = -1;
573
    p->falling_counter  = 0;
574
    p->quick_kick       = 0;
575
    p->subweapon        = 0;
576
    p->last_full_weapon = 0;
577
    p->ftq              = 0;
578
    p->fta              = 0;
579
    p->tipincs          = 0;
580
    p->buttonpalette    = 0;
581
    p->actorsqu         =-1;
582
    p->invdisptime      = 0;
583
    p->refresh_inventory= 0;
584
    p->last_pissed_time = 0;
585
    p->holster_weapon   = 0;
586
    p->pycount          = 0;
587
    p->pyoff            = 0;
588
    p->opyoff           = 0;
589
    p->loogcnt          = 0;
590
    p->angvel           = 0;
591
    p->weapon_sway      = 0;
592
    //    p->select_dir       = 0;
593
    p->extra_extra8     = 0;
594
    p->show_empty_weapon= 0;
595
    p->dummyplayersprite=-1;
596
    p->crack_time       = 0;
597
    p->hbomb_hold_delay = 0;
598
    p->transporter_hold = 0;
599
    p->wantweaponfire  = -1;
600
    p->hurt_delay       = 0;
601
    p->footprintcount   = 0;
602
    p->footprintpal     = 0;
603
    p->footprintshade   = 0;
604
    p->jumping_toggle   = 0;
605
    p->ohoriz = p->horiz= 140;
606
    p->horizoff         = 0;
607
    p->bobcounter       = 0;
608
    p->on_ground        = 0;
609
    p->player_par       = 0;
610
    p->return_to_center = 9;
611
    p->airleft          = 15*26;
612
    p->rapid_fire_hold  = 0;
613
    p->toggle_key_flag  = 0;
614
    p->access_spritenum = -1;
331 terminx 615
    if (ud.multimode > 1 && (gametype_flags[ud.coop] & GAMETYPE_FLAG_ACCESSATSTART))
5 Plagman 616
        p->got_access = 7;
617
    else p->got_access      = 0;
618
    p->random_club_frame= 0;
619
    pus = 1;
620
    p->on_warping_sector = 0;
621
    p->spritebridge      = 0;
622
    p->sbs          = 0;
623
    p->palette = (char *) &palette[0];
624
 
333 terminx 625
    if (p->steroids_amount < 400)
5 Plagman 626
    {
627
        p->steroids_amount = 0;
628
        p->inven_icon = 0;
629
    }
630
    p->heat_on =            0;
631
    p->jetpack_on =         0;
632
    p->holoduke_on =       -1;
633
 
634
    p->look_ang          = 512 - ((ud.level_number&1)<<10);
635
 
636
    p->rotscrnang        = 0;
637
    p->orotscrnang       = 1;   // JBF 20031220
638
    p->newowner          =-1;
639
    p->jumping_counter   = 0;
640
    p->hard_landing      = 0;
641
    p->posxv             = 0;
642
    p->posyv             = 0;
643
    p->poszv             = 0;
644
    fricxv            = 0;
645
    fricyv            = 0;
646
    p->somethingonplayer =-1;
647
    p->one_eighty_count  = 0;
648
    p->cheat_phase       = 0;
649
 
650
    p->on_crane          = -1;
651
 
333 terminx 652
    if ((aplWeaponWorksLike[p->curr_weapon][snum] == PISTOL_WEAPON) &&
653
            (aplWeaponReload[p->curr_weapon][snum] > aplWeaponTotalTime[p->curr_weapon][snum]))
5 Plagman 654
        p->kickback_pic  = aplWeaponTotalTime[p->curr_weapon][snum]+1;
655
    else p->kickback_pic = 0;
656
 
657
    p->weapon_pos        = 6;
658
    p->walking_snd_toggle= 0;
659
    p->weapon_ang        = 0;
660
 
661
    p->knuckle_incs      = 1;
662
    p->fist_incs = 0;
663
    p->knee_incs         = 0;
664
    p->jetpack_on        = 0;
665
    p->reloading        = 0;
666
 
172 terminx 667
    p->movement_lock     = 0;
5 Plagman 668
 
669
    setpal(p);
670
    OnEvent(EVENT_RESETPLAYER, p->i, snum, -1);
671
}
672
 
673
void resetweapons(short snum)
674
{
675
    short  weapon;
676
    struct player_struct *p;
677
 
678
    p = &ps[snum];
679
 
333 terminx 680
    for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS; weapon++)
5 Plagman 681
        p->gotweapon[weapon] = 0;
333 terminx 682
    for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS; weapon++)
5 Plagman 683
        p->ammo_amount[weapon] = 0;
684
 
685
    p->weapon_pos = 6;
686
    p->kickback_pic = 5;
687
    p->curr_weapon = PISTOL_WEAPON;
688
    p->gotweapon[PISTOL_WEAPON] = 1;
689
    p->gotweapon[KNEE_WEAPON] = 1;
690
    p->ammo_amount[PISTOL_WEAPON] = 48;
691
    p->gotweapon[HANDREMOTE_WEAPON] = 1;
692
    p->last_weapon = -1;
693
 
694
    p->show_empty_weapon= 0;
695
    p->last_pissed_time = 0;
696
    p->holster_weapon = 0;
697
    OnEvent(EVENT_RESETWEAPONS, p->i, snum, -1);
698
}
699
 
700
void resetinventory(short snum)
701
{
702
    struct player_struct *p;
703
 
704
    p = &ps[snum];
705
 
706
    p->inven_icon       = 0;
707
    p->boot_amount = 0;
335 terminx 708
    p->scuba_on =           0;
709
    p->scuba_amount =         0;
710
    p->heat_amount        = 0;
711
    p->heat_on = 0;
712
    p->jetpack_on =         0;
713
    p->jetpack_amount =       0;
5 Plagman 714
    p->shield_amount =      max_armour_amount;
715
    p->holoduke_on = -1;
716
    p->holoduke_amount =    0;
717
    p->firstaid_amount = 0;
718
    p->steroids_amount = 0;
719
    p->inven_icon = 0;
720
    OnEvent(EVENT_RESETINVENTORY, p->i, snum, -1);
721
}
722
 
395 terminx 723
static void resetprestat(short snum,char g)
5 Plagman 724
{
725
    struct player_struct *p;
726
    short i;
727
 
728
    p = &ps[snum];
729
 
730
    spriteqloc = 0;
331 terminx 731
    for (i=0;i<spriteqamount;i++) spriteq[i] = -1;
5 Plagman 732
 
733
    p->hbomb_on          = 0;
734
    p->cheat_phase       = 0;
735
    p->pals_time         = 0;
736
    p->toggle_key_flag   = 0;
737
    p->secret_rooms      = 0;
738
    p->max_secret_rooms  = 0;
739
    p->actors_killed     = 0;
740
    p->max_actors_killed = 0;
741
    p->lastrandomspot = 0;
742
    p->weapon_pos = 6;
743
    p->kickback_pic = 5;
744
    p->last_weapon = -1;
745
    p->weapreccnt = 0;
746
    p->interface_toggle_flag = 0;
747
    p->show_empty_weapon= 0;
748
    p->holster_weapon = 0;
749
    p->last_pissed_time = 0;
750
 
751
    p->one_parallax_sectnum = -1;
752
    p->visibility = ud.const_visibility;
753
 
754
    screenpeek              = myconnectindex;
755
    numanimwalls            = 0;
756
    numcyclers              = 0;
757
    animatecnt              = 0;
758
    parallaxtype            = 0;
759
    randomseed              = 17L;
760
    ud.pause_on             = 0;
761
    ud.camerasprite         =-1;
762
    ud.eog                  = 0;
763
    tempwallptr             = 0;
764
    camsprite               =-1;
765
    earthquaketime          = 0;
766
 
767
    numinterpolations = 0;
768
    startofdynamicinterpolations = 0;
769
 
333 terminx 770
    if (((g&MODE_EOL) != MODE_EOL && numplayers < 2) || (!(gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && numplayers > 1))
5 Plagman 771
    {
772
        resetweapons(snum);
773
        resetinventory(snum);
774
    }
331 terminx 775
    else if (p->curr_weapon == HANDREMOTE_WEAPON)
5 Plagman 776
    {
777
        p->ammo_amount[HANDBOMB_WEAPON]++;
778
        p->curr_weapon = HANDBOMB_WEAPON;
779
    }
780
 
781
    p->timebeforeexit   = 0;
782
    p->customexitsound  = 0;
783
 
784
}
785
 
395 terminx 786
static void setupbackdrop(short sky)
5 Plagman 787
{
788
    short i;
789
 
331 terminx 790
    for (i=0;i<MAXPSKYTILES;i++) pskyoff[i]=0;
5 Plagman 791
 
331 terminx 792
    if (parallaxyscale != 65536L)
5 Plagman 793
        parallaxyscale = 32768;
794
 
331 terminx 795
    switch (dynamictostatic[sky])
5 Plagman 796
    {
337 terminx 797
    case CLOUDYOCEAN__STATIC:
798
        parallaxyscale = 65536L;
799
        break;
800
    case MOONSKY1__STATIC :
801
        pskyoff[6]=1;
802
        pskyoff[1]=2;
803
        pskyoff[4]=2;
804
        pskyoff[2]=3;
805
        break;
806
    case BIGORBIT1__STATIC: // orbit
807
        pskyoff[5]=1;
808
        pskyoff[6]=2;
809
        pskyoff[7]=3;
810
        pskyoff[2]=4;
811
        break;
812
    case LA__STATIC:
813
        parallaxyscale = 16384+1024;
814
        pskyoff[0]=1;
815
        pskyoff[1]=2;
816
        pskyoff[2]=1;
817
        pskyoff[3]=3;
818
        pskyoff[4]=4;
819
        pskyoff[5]=0;
820
        pskyoff[6]=2;
821
        pskyoff[7]=3;
822
        break;
5 Plagman 823
    }
824
 
825
    pskybits=3;
826
}
827
 
395 terminx 828
static void prelevel(char g)
5 Plagman 829
{
830
    short i, nexti, j, startwall, endwall, lotaglist;
831
    short lotags[65];
832
    int switchpicnum;
833
 
834
 
835
    clearbufbyte(show2dsector,sizeof(show2dsector),0L);
836
    clearbufbyte(show2dwall,sizeof(show2dwall),0L);
837
    clearbufbyte(show2dsprite,sizeof(show2dsprite),0L);
838
 
839
    resetprestat(0,g);
840
    numclouds = 0;
841
 
331 terminx 842
    for (i=0;i<numsectors;i++)
5 Plagman 843
    {
844
        sector[i].extra = 256;
845
 
331 terminx 846
        switch (sector[i].lotag)
5 Plagman 847
        {
337 terminx 848
        case 20:
849
        case 22:
850
            if (sector[i].floorz > sector[i].ceilingz)
851
                sector[i].lotag |= 32768;
852
            continue;
5 Plagman 853
        }
854
 
331 terminx 855
        if (sector[i].ceilingstat&1)
5 Plagman 856
        {
331 terminx 857
            if (waloff[sector[i].ceilingpicnum] == 0)
5 Plagman 858
            {
331 terminx 859
                if (sector[i].ceilingpicnum == LA)
860
                    for (j=0;j<5;j++)
5 Plagman 861
                        tloadtile(sector[i].ceilingpicnum+j, 0);
862
            }
863
            setupbackdrop(sector[i].ceilingpicnum);
864
 
331 terminx 865
            if (sector[i].ceilingpicnum == CLOUDYSKIES && numclouds < 127)
5 Plagman 866
                clouds[numclouds++] = i;
867
 
331 terminx 868
            if (ps[0].one_parallax_sectnum == -1)
5 Plagman 869
                ps[0].one_parallax_sectnum = i;
870
        }
871
 
331 terminx 872
        if (sector[i].lotag == 32767) //Found a secret room
5 Plagman 873
        {
874
            ps[0].max_secret_rooms++;
875
            continue;
876
        }
877
 
331 terminx 878
        if (sector[i].lotag == -1)
5 Plagman 879
        {
880
            ps[0].exitx = wall[sector[i].wallptr].x;
881
            ps[0].exity = wall[sector[i].wallptr].y;
882
            continue;
883
        }
884
    }
885
 
886
    i = headspritestat[0];
331 terminx 887
    while (i >= 0)
5 Plagman 888
    {
889
        nexti = nextspritestat[i];
299 terminx 890
        ResetActorGameVars(i);
394 terminx 891
        LoadActor(i);
333 terminx 892
        if (sprite[i].lotag == -1 && (sprite[i].cstat&16))
5 Plagman 893
        {
894
            ps[0].exitx = SX;
895
            ps[0].exity = SY;
896
        }
331 terminx 897
        else switch (dynamictostatic[PN])
5 Plagman 898
            {
337 terminx 899
            case GPSPEED__STATIC:
900
                sector[SECT].extra = SLT;
901
                deletesprite(i);
902
                break;
5 Plagman 903
 
337 terminx 904
            case CYCLER__STATIC:
905
                if (numcyclers >= MAXCYCLERS)
906
                {
907
                    Bsprintf(tempbuf,"\nToo many cycling sectors (%d max).",MAXCYCLERS);
908
                    gameexit(tempbuf);
909
                }
910
                cyclers[numcyclers][0] = SECT;
911
                cyclers[numcyclers][1] = SLT;
912
                cyclers[numcyclers][2] = SS;
913
                cyclers[numcyclers][3] = sector[SECT].floorshade;
914
                cyclers[numcyclers][4] = SHT;
915
                cyclers[numcyclers][5] = (SA == 1536);
916
                numcyclers++;
917
                deletesprite(i);
918
                break;
5 Plagman 919
 
337 terminx 920
            case SECTOREFFECTOR__STATIC:
921
            case ACTIVATOR__STATIC:
922
            case TOUCHPLATE__STATIC:
923
            case ACTIVATORLOCKED__STATIC:
924
            case MUSICANDSFX__STATIC:
925
            case LOCATORS__STATIC:
926
            case MASTERSWITCH__STATIC:
927
            case RESPAWN__STATIC:
928
                sprite[i].cstat = 0;
929
                break;
5 Plagman 930
            }
931
        i = nexti;
932
    }
933
 
331 terminx 934
    for (i=0;i < MAXSPRITES;i++)
5 Plagman 935
    {
331 terminx 936
        if (sprite[i].statnum < MAXSTATUS)
5 Plagman 937
        {
331 terminx 938
            if (PN == SECTOREFFECTOR && SLT == 14)
5 Plagman 939
                continue;
940
            spawn(-1,i);
941
        }
942
    }
943
 
331 terminx 944
    for (i=0;i < MAXSPRITES;i++)
945
        if (sprite[i].statnum < MAXSTATUS)
5 Plagman 946
        {
333 terminx 947
            if (PN == SECTOREFFECTOR && SLT == 14)
5 Plagman 948
                spawn(-1,i);
949
        }
950
 
951
    lotaglist = 0;
952
 
953
    i = headspritestat[0];
331 terminx 954
    while (i >= 0)
5 Plagman 955
    {
331 terminx 956
        switch (dynamictostatic[PN-1])
5 Plagman 957
        {
337 terminx 958
        case DIPSWITCH__STATIC:
959
        case DIPSWITCH2__STATIC:
960
        case PULLSWITCH__STATIC:
961
        case HANDSWITCH__STATIC:
962
        case SLOTDOOR__STATIC:
963
        case LIGHTSWITCH__STATIC:
964
        case SPACELIGHTSWITCH__STATIC:
965
        case SPACEDOORSWITCH__STATIC:
966
        case FRANKENSTINESWITCH__STATIC:
967
        case LIGHTSWITCH2__STATIC:
968
        case POWERSWITCH1__STATIC:
969
        case LOCKSWITCH1__STATIC:
970
        case POWERSWITCH2__STATIC:
971
            for (j=0;j<lotaglist;j++)
972
                if (SLT == lotags[j])
973
                    break;
5 Plagman 974
 
337 terminx 975
            if (j == lotaglist)
976
            {
977
                lotags[lotaglist] = SLT;
978
                lotaglist++;
979
                if (lotaglist > 64)
980
                    gameexit("\nToo many switches (64 max).");
981
 
982
                j = headspritestat[3];
983
                while (j >= 0)
335 terminx 984
                {
337 terminx 985
                    if (sprite[j].lotag == 12 && sprite[j].hitag == SLT)
986
                        hittype[j].temp_data[0] = 1;
987
                    j = nextspritestat[j];
5 Plagman 988
                }
337 terminx 989
            }
990
            break;
5 Plagman 991
        }
992
        i = nextspritestat[i];
993
    }
994
 
995
    mirrorcnt = 0;
996
 
333 terminx 997
    for (i = 0; i < numwalls; i++)
5 Plagman 998
    {
999
        walltype *wal;
1000
        wal = &wall[i];
1001
 
331 terminx 1002
        if (wal->overpicnum == MIRROR && (wal->cstat&32) != 0)
5 Plagman 1003
        {
1004
            j = wal->nextsector;
1005
 
331 terminx 1006
            if (mirrorcnt > 63)
5 Plagman 1007
                gameexit("\nToo many mirrors (64 max.)");
333 terminx 1008
            if ((j >= 0) && sector[j].ceilingpicnum != MIRROR)
5 Plagman 1009
            {
1010
                sector[j].ceilingpicnum = MIRROR;
1011
                sector[j].floorpicnum = MIRROR;
1012
                mirrorwall[mirrorcnt] = i;
1013
                mirrorsector[mirrorcnt] = j;
1014
                mirrorcnt++;
1015
                continue;
1016
            }
1017
        }
1018
 
331 terminx 1019
        if (numanimwalls >= MAXANIMWALLS)
5 Plagman 1020
        {
1021
            Bsprintf(tempbuf,"\nToo many 'anim' walls (%d max).",MAXANIMWALLS);
1022
            gameexit(tempbuf);
1023
        }
1024
 
1025
        animwall[numanimwalls].tag = 0;
1026
        animwall[numanimwalls].wallnum = 0;
1027
        switchpicnum = wal->overpicnum;
335 terminx 1028
        if ((wal->overpicnum > W_FORCEFIELD)&&(wal->overpicnum <= W_FORCEFIELD+2))
1029
        {
5 Plagman 1030
            switchpicnum = W_FORCEFIELD;
1031
        }
331 terminx 1032
        switch (dynamictostatic[switchpicnum])
5 Plagman 1033
        {
337 terminx 1034
        case FANSHADOW__STATIC:
1035
        case FANSPRITE__STATIC:
1036
            wall->cstat |= 65;
1037
            animwall[numanimwalls].wallnum = i;
1038
            numanimwalls++;
1039
            break;
5 Plagman 1040
 
337 terminx 1041
        case W_FORCEFIELD__STATIC:
1042
            if (wal->overpicnum==W_FORCEFIELD__STATIC)
1043
                for (j=0;j<3;j++)
1044
                    tloadtile(W_FORCEFIELD+j, 0);
1045
            if (wal->shade > 31)
1046
                wal->cstat = 0;
1047
            else wal->cstat |= 85+256;
5 Plagman 1048
 
1049
 
337 terminx 1050
            if (wal->lotag && wal->nextwall >= 0)
1051
                wall[wal->nextwall].lotag =
1052
                    wal->lotag;
5 Plagman 1053
 
337 terminx 1054
        case BIGFORCE__STATIC:
5 Plagman 1055
 
337 terminx 1056
            animwall[numanimwalls].wallnum = i;
1057
            numanimwalls++;
5 Plagman 1058
 
337 terminx 1059
            continue;
5 Plagman 1060
        }
1061
 
1062
        wal->extra = -1;
1063
 
331 terminx 1064
        switch (dynamictostatic[wal->picnum])
5 Plagman 1065
        {
337 terminx 1066
        case WATERTILE2__STATIC:
1067
            for (j=0;j<3;j++)
1068
                tloadtile(wal->picnum+j, 0);
1069
            break;
5 Plagman 1070
 
337 terminx 1071
        case TECHLIGHT2__STATIC:
1072
        case TECHLIGHT4__STATIC:
1073
            tloadtile(wal->picnum, 0);
1074
            break;
1075
        case W_TECHWALL1__STATIC:
1076
        case W_TECHWALL2__STATIC:
1077
        case W_TECHWALL3__STATIC:
1078
        case W_TECHWALL4__STATIC:
1079
            animwall[numanimwalls].wallnum = i;
1080
            //                animwall[numanimwalls].tag = -1;
1081
            numanimwalls++;
1082
            break;
1083
        case SCREENBREAK6__STATIC:
1084
        case SCREENBREAK7__STATIC:
1085
        case SCREENBREAK8__STATIC:
1086
            for (j=SCREENBREAK6;j<SCREENBREAK9;j++)
1087
                tloadtile(j, 0);
1088
            animwall[numanimwalls].wallnum = i;
1089
            animwall[numanimwalls].tag = -1;
1090
            numanimwalls++;
1091
            break;
5 Plagman 1092
 
337 terminx 1093
        case FEMPIC1__STATIC:
1094
        case FEMPIC2__STATIC:
1095
        case FEMPIC3__STATIC:
5 Plagman 1096
 
337 terminx 1097
            wal->extra = wal->picnum;
1098
            animwall[numanimwalls].tag = -1;
1099
            if (ud.lockout)
1100
            {
1101
                if (wal->picnum == FEMPIC1)
1102
                    wal->picnum = BLANKSCREEN;
1103
                else wal->picnum = SCREENBREAK6;
1104
            }
5 Plagman 1105
 
337 terminx 1106
            animwall[numanimwalls].wallnum = i;
1107
            animwall[numanimwalls].tag = wal->picnum;
1108
            numanimwalls++;
1109
            break;
5 Plagman 1110
 
337 terminx 1111
        case SCREENBREAK1__STATIC:
1112
        case SCREENBREAK2__STATIC:
1113
        case SCREENBREAK3__STATIC:
1114
        case SCREENBREAK4__STATIC:
1115
        case SCREENBREAK5__STATIC:
5 Plagman 1116
 
337 terminx 1117
        case SCREENBREAK9__STATIC:
1118
        case SCREENBREAK10__STATIC:
1119
        case SCREENBREAK11__STATIC:
1120
        case SCREENBREAK12__STATIC:
1121
        case SCREENBREAK13__STATIC:
1122
        case SCREENBREAK14__STATIC:
1123
        case SCREENBREAK15__STATIC:
1124
        case SCREENBREAK16__STATIC:
1125
        case SCREENBREAK17__STATIC:
1126
        case SCREENBREAK18__STATIC:
1127
        case SCREENBREAK19__STATIC:
5 Plagman 1128
 
337 terminx 1129
            animwall[numanimwalls].wallnum = i;
1130
            animwall[numanimwalls].tag = wal->picnum;
1131
            numanimwalls++;
1132
            break;
5 Plagman 1133
        }
1134
    }
1135
 
1136
    //Invalidate textures in sector behind mirror
331 terminx 1137
    for (i=0;i<mirrorcnt;i++)
5 Plagman 1138
    {
1139
        startwall = sector[mirrorsector[i]].wallptr;
1140
        endwall = startwall + sector[mirrorsector[i]].wallnum;
331 terminx 1141
        for (j=startwall;j<endwall;j++)
5 Plagman 1142
        {
1143
            wall[j].picnum = MIRROR;
1144
            wall[j].overpicnum = MIRROR;
1145
        }
1146
    }
1147
}
1148
 
1149
void newgame(char vn,char ln,char sk)
1150
{
1151
    struct player_struct *p = &ps[0];
1152
    short i;
1153
 
423 terminx 1154
    handleevents();
1155
    getpackets();
1156
 
331 terminx 1157
    if (globalskillsound >= 0 && FXDevice >= 0 && SoundToggle)
423 terminx 1158
    {
335 terminx 1159
        while (issoundplaying(-1,globalskillsound))
1160
        {
1161
            handleevents();
1162
            getpackets();
1163
        }
1164
    }
428 terminx 1165
 
5 Plagman 1166
    globalskillsound = -1;
1167
 
1168
    waitforeverybody();
1169
    ready2send = 0;
1170
 
333 terminx 1171
    if (ud.m_recstat != 2 && ud.last_level >= 0 && ud.multimode > 1 && (ud.coop&GAMETYPE_FLAG_SCORESHEET))
5 Plagman 1172
        dobonus(1);
1173
 
333 terminx 1174
    if (ln == 0 && vn == 3 && ud.multimode < 2 && ud.lockout == 0)
5 Plagman 1175
    {
1176
        playmusic(&env_music_fn[1][0]);
1177
 
1178
        flushperms();
1179
        setview(0,0,xdim-1,ydim-1);
1180
        clearview(0L);
1181
        nextpage();
1182
 
1183
        playanm("vol41a.anm",6);
1184
        clearview(0L);
1185
        nextpage();
1186
 
1187
        playanm("vol42a.anm",7);
1188
        playanm("vol43a.anm",9);
1189
        clearview(0L);
1190
        nextpage();
1191
 
1192
        FX_StopAllSounds();
1193
    }
1194
 
1195
    show_shareware = 26*34;
1196
 
1197
    ud.level_number =   ln;
1198
    ud.volume_number =  vn;
1199
    ud.player_skill =   sk;
1200
    ud.secretlevel =    0;
1201
    ud.from_bonus = 0;
1202
    parallaxyscale = 0;
1203
 
1204
    ud.last_level = -1;
1205
    lastsavedpos = -1;
1206
    p->zoom            = 768;
1207
    p->gm              = 0;
1208
 
423 terminx 1209
    //AddLog("Newgame");
1210
    ResetGameVars();
5 Plagman 1211
 
423 terminx 1212
    InitGameVarPointers();
5 Plagman 1213
 
423 terminx 1214
    ResetSystemDefaults();
5 Plagman 1215
 
423 terminx 1216
    if (ud.m_coop != 1)
1217
    {
1218
        for (i=0;i<MAX_WEAPONS;i++)
5 Plagman 1219
        {
423 terminx 1220
            if (aplWeaponWorksLike[i][0]==PISTOL_WEAPON)
5 Plagman 1221
            {
423 terminx 1222
                p->curr_weapon = i;
1223
                p->gotweapon[i] = 1;
1224
                p->ammo_amount[i] = 48;
5 Plagman 1225
            }
423 terminx 1226
            else if (aplWeaponWorksLike[i][0]==KNEE_WEAPON)
1227
                p->gotweapon[i] = 1;
1228
            else if (aplWeaponWorksLike[i][0]==HANDREMOTE_WEAPON)
1229
                p->gotweapon[i] = 1;
5 Plagman 1230
        }
423 terminx 1231
        p->last_weapon = -1;
5 Plagman 1232
    }
428 terminx 1233
 
5 Plagman 1234
    display_mirror =        0;
1235
 
333 terminx 1236
    if (ud.multimode > 1)
5 Plagman 1237
    {
331 terminx 1238
        if (numplayers < 2)
5 Plagman 1239
        {
1240
            connecthead = 0;
331 terminx 1241
            for (i=0;i<MAXPLAYERS;i++) connectpoint2[i] = i+1;
5 Plagman 1242
            connectpoint2[ud.multimode-1] = -1;
1243
        }
1244
    }
1245
    else
1246
    {
1247
        connecthead = 0;
1248
        connectpoint2[0] = -1;
1249
    }
1250
}
1251
 
395 terminx 1252
static void resetpspritevars(char g)
5 Plagman 1253
{
1254
    short i, j, nexti,circ;
1255
    long firstx,firsty;
1256
    spritetype *s;
1257
    char aimmode[MAXPLAYERS],autoaim[MAXPLAYERS],weaponswitch[MAXPLAYERS];
1258
    STATUSBARTYPE tsbar[MAXPLAYERS];
1259
 
1260
    EGS(ps[0].cursectnum,ps[0].posx,ps[0].posy,ps[0].posz,
1261
        APLAYER,0,0,0,ps[0].ang,0,0,0,10);
1262
 
331 terminx 1263
    if (ud.recstat != 2) for (i=0;i<MAXPLAYERS;i++)
5 Plagman 1264
        {
1265
            aimmode[i] = ps[i].aim_mode;
1266
            autoaim[i] = ps[i].auto_aim;
29 terminx 1267
            weaponswitch[i] = ps[i].weaponswitch;
331 terminx 1268
            if (ud.multimode > 1 && (gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
5 Plagman 1269
            {
331 terminx 1270
                for (j=0;j<MAX_WEAPONS;j++)
5 Plagman 1271
                {
1272
                    tsbar[i].ammo_amount[j] = ps[i].ammo_amount[j];
1273
                    tsbar[i].gotweapon[j] = ps[i].gotweapon[j];
1274
                }
1275
 
1276
                tsbar[i].shield_amount = ps[i].shield_amount;
1277
                tsbar[i].curr_weapon = ps[i].curr_weapon;
1278
                tsbar[i].inven_icon = ps[i].inven_icon;
1279
 
1280
                tsbar[i].firstaid_amount = ps[i].firstaid_amount;
1281
                tsbar[i].steroids_amount = ps[i].steroids_amount;
1282
                tsbar[i].holoduke_amount = ps[i].holoduke_amount;
1283
                tsbar[i].jetpack_amount = ps[i].jetpack_amount;
1284
                tsbar[i].heat_amount = ps[i].heat_amount;
1285
                tsbar[i].scuba_amount = ps[i].scuba_amount;
1286
                tsbar[i].boot_amount = ps[i].boot_amount;
1287
            }
1288
        }
1289
 
1290
    resetplayerstats(0);
1291
 
331 terminx 1292
    for (i=1;i<MAXPLAYERS;i++)
5 Plagman 1293
        memcpy(&ps[i],&ps[0],sizeof(ps[0]));
1294
 
331 terminx 1295
    if (ud.recstat != 2) for (i=0;i<MAXPLAYERS;i++)
5 Plagman 1296
        {
1297
            ps[i].aim_mode = aimmode[i];
1298
            ps[i].auto_aim = autoaim[i];
29 terminx 1299
            ps[i].weaponswitch = weaponswitch[i];
331 terminx 1300
            if (ud.multimode > 1 && (gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
5 Plagman 1301
            {
331 terminx 1302
                for (j=0;j<MAX_WEAPONS;j++)
5 Plagman 1303
                {
1304
                    ps[i].ammo_amount[j] = tsbar[i].ammo_amount[j];
1305
                    ps[i].gotweapon[j] = tsbar[i].gotweapon[j];
1306
                }
1307
                ps[i].shield_amount = tsbar[i].shield_amount;
1308
                ps[i].curr_weapon = tsbar[i].curr_weapon;
1309
                ps[i].inven_icon = tsbar[i].inven_icon;
1310
 
1311
                ps[i].firstaid_amount = tsbar[i].firstaid_amount;
1312
                ps[i].steroids_amount= tsbar[i].steroids_amount;
1313
                ps[i].holoduke_amount = tsbar[i].holoduke_amount;
1314
                ps[i].jetpack_amount = tsbar[i].jetpack_amount;
1315
                ps[i].heat_amount = tsbar[i].heat_amount;
1316
                ps[i].scuba_amount= tsbar[i].scuba_amount;
1317
                ps[i].boot_amount = tsbar[i].boot_amount;
1318
            }
1319
        }
1320
 
1321
    numplayersprites = 0;
1322
    circ = 2048/ud.multimode;
1323
 
1324
    which_palookup = 9;
1325
    j = connecthead;
1326
    i = headspritestat[10];
331 terminx 1327
    while (i >= 0)
5 Plagman 1328
    {
1329
        nexti = nextspritestat[i];
1330
        s = &sprite[i];
1331
 
333 terminx 1332
        if (numplayersprites == MAXPLAYERS)
5 Plagman 1333
            gameexit("\nToo many player sprites (max 16.)");
1334
 
331 terminx 1335
        if (numplayersprites == 0)
5 Plagman 1336
        {
1337
            firstx = ps[0].posx;
1338
            firsty = ps[0].posy;
1339
        }
1340
 
352 terminx 1341
        po[(unsigned char)numplayersprites].ox = s->x;
1342
        po[(unsigned char)numplayersprites].oy = s->y;
1343
        po[(unsigned char)numplayersprites].oz = s->z;
1344
        po[(unsigned char)numplayersprites].oa = s->ang;
1345
        po[(unsigned char)numplayersprites].os = s->sectnum;
5 Plagman 1346
 
1347
        numplayersprites++;
331 terminx 1348
        if (j >= 0)
5 Plagman 1349
        {
1350
            s->owner = i;
1351
            s->shade = 0;
1352
            s->xrepeat = 42;
1353
            s->yrepeat = 36;
1354
            s->cstat = 1+256;
1355
            s->xoffset = 0;
1356
            s->clipdist = 64;
1357
 
333 terminx 1358
            if ((g&MODE_EOL) != MODE_EOL || ps[j].last_extra == 0)
5 Plagman 1359
            {
1360
                ps[j].last_extra = max_player_health;
1361
                s->extra = max_player_health;
1362
                ps[j].runspeed = dukefriction;
1363
            }
1364
            else s->extra = ps[j].last_extra;
1365
 
1366
            s->yvel = j;
1367
 
331 terminx 1368
            if (!ud.pcolor[j] && ud.multimode > 1 && !(gametype_flags[ud.coop] & GAMETYPE_FLAG_TDM))
5 Plagman 1369
            {
331 terminx 1370
                if (s->pal == 0)
53 terminx 1371
                {
56 terminx 1372
                    int k;
1373
 
335 terminx 1374
                    for (k=0;k<MAXPLAYERS;k++)
1375
                    {
1376
                        if (which_palookup == ps[k].palookup)
1377
                        {
56 terminx 1378
                            which_palookup++;
333 terminx 1379
                            if (which_palookup >= 17)
56 terminx 1380
                                which_palookup = 9;
1381
                            k=0;
1382
                        }
1383
                    }
273 terminx 1384
                    ud.pcolor[j] = s->pal = ps[j].palookup = which_palookup++;
333 terminx 1385
                    if (which_palookup >= 17)
56 terminx 1386
                        which_palookup = 9;
53 terminx 1387
                }
273 terminx 1388
                else ud.pcolor[j] = ps[j].palookup = s->pal;
268 terminx 1389
            }
1390
            else
1391
            {
1392
                int k = ud.pcolor[j];
5 Plagman 1393
 
331 terminx 1394
                if (gametype_flags[ud.coop] & GAMETYPE_FLAG_TDM)
268 terminx 1395
                {
331 terminx 1396
                    switch (ud.pteam[j])
268 terminx 1397
                    {
337 terminx 1398
                    case 0:
1399
                        k = 3;
1400
                        break;
1401
                    case 1:
1402
                        k = 21;
1403
                        break;
268 terminx 1404
                    }
1405
                    ps[j].team = ud.pteam[j];
1406
                }
1407
                s->pal = ps[j].palookup = k;
1408
            }
1409
 
5 Plagman 1410
            ps[j].i = i;
1411
            ps[j].frag_ps = j;
1412
            hittype[i].owner = i;
1413
 
1414
            hittype[i].bposx = ps[j].bobposx = ps[j].oposx = ps[j].posx =        s->x;
1415
            hittype[i].bposy = ps[j].bobposy = ps[j].oposy = ps[j].posy =        s->y;
1416
            hittype[i].bposz = ps[j].oposz = ps[j].posz =        s->z;
1417
            ps[j].oang  = ps[j].ang  =        s->ang;
1418
 
376 terminx 1419
            updatesector(s->x,s->y,&ps[j].cursectnum);
5 Plagman 1420
 
1421
            j = connectpoint2[j];
1422
 
1423
        }
1424
        else deletesprite(i);
1425
        i = nexti;
1426
    }
1427
}
1428
 
400 terminx 1429
static inline void clearfrags(void)
5 Plagman 1430
{
400 terminx 1431
    short i = 0;
5 Plagman 1432
 
400 terminx 1433
    while (i<MAXPLAYERS)
1434
        ps[i].frag = ps[i].fraggedself = 0, i++;
5 Plagman 1435
    clearbufbyte(&frags[0][0],(MAXPLAYERS*MAXPLAYERS)<<1,0L);
1436
}
1437
 
1438
void resettimevars(void)
1439
{
1440
    vel = svel = angvel = horiz = 0;
1441
 
1442
    totalclock = 0L;
1443
    cloudtotalclock = 0L;
1444
    ototalclock = 0L;
1445
    lockclock = 0L;
1446
    ready2send = 1;
1447
}
1448
 
1449
void waitforeverybody()
1450
{
1451
    long i;
1452
 
1453
    if (numplayers < 2) return;
1454
    packbuf[0] = 250;
331 terminx 1455
    for (i=connecthead;i>=0;i=connectpoint2[i])
5 Plagman 1456
    {
1457
        if (i != myconnectindex) sendpacket(i,packbuf,1);
1458
        if ((!networkmode) && (myconnectindex != connecthead)) break; //slaves in M/S mode only send to master
1459
    }
1460
    playerreadyflag[myconnectindex]++;
1461
 
1462
    while (1)
1463
    {
375 terminx 1464
#ifdef _WIN32
1465
        Sleep(10);
1466
#else
1467
        usleep(10);
363 terminx 1468
#endif
375 terminx 1469
        sampletimer();
5 Plagman 1470
        handleevents();
1471
        AudioUpdate();
1472
 
1473
        if (quitevent || keystatus[1]) gameexit("");
1474
 
1475
        getpackets();
1476
 
331 terminx 1477
        for (i=connecthead;i>=0;i=connectpoint2[i])
5 Plagman 1478
        {
1479
            if (playerreadyflag[i] < playerreadyflag[myconnectindex]) break;
335 terminx 1480
            if ((!networkmode) && (myconnectindex != connecthead))
1481
            {
1482
                i = -1;
1483
                break;
1484
            } //slaves in M/S mode only wait for master
331 terminx 1485
 
5 Plagman 1486
        }
1487
        if (i < 0) return;
1488
    }
1489
}
1490
 
368 terminx 1491
extern char jump_input;
1492
 
5 Plagman 1493
void clearfifo(void)
1494
{
1495
    syncvaltail = 0L;
1496
    syncvaltottail = 0L;
1497
    syncstat = 0;
1498
    bufferjitter = 1;
1499
    mymaxlag = otherminlag = 0;
368 terminx 1500
    jump_input = 0;
5 Plagman 1501
 
1502
    movefifoplc = movefifosendplc = fakemovefifoplc = 0;
275 terminx 1503
    avgfvel = avgsvel = avgavel = avghorz = avgbits = avgextbits = 0;
5 Plagman 1504
    otherminlag = mymaxlag = 0;
1505
 
1506
    clearbufbyte(myminlag,MAXPLAYERS<<2,0L);
1507
    clearbufbyte(&loc,sizeof(input),0L);
1508
    clearbufbyte(&sync[0],sizeof(sync),0L);
1509
    clearbufbyte(inputfifo,sizeof(input)*MOVEFIFOSIZ*MAXPLAYERS,0L);
1510
 
1511
    clearbuf(movefifoend,MAXPLAYERS,0L);
1512
    clearbuf(syncvalhead,MAXPLAYERS,0L);
1513
    clearbuf(myminlag,MAXPLAYERS,0L);
1514
 
1515
    //    clearbufbyte(playerquitflag,MAXPLAYERS,0x01);
1516
}
1517
 
1518
void resetmys(void)
1519
{
1520
    myx = omyx = ps[myconnectindex].posx;
1521
    myy = omyy = ps[myconnectindex].posy;
1522
    myz = omyz = ps[myconnectindex].posz;
1523
    myxvel = myyvel = myzvel = 0;
1524
    myang = omyang = ps[myconnectindex].ang;
1525
    myhoriz = omyhoriz = ps[myconnectindex].horiz;
1526
    myhorizoff = omyhorizoff = ps[myconnectindex].horizoff;
1527
    mycursectnum = ps[myconnectindex].cursectnum;
1528
    myjumpingcounter = ps[myconnectindex].jumping_counter;
1529
    myjumpingtoggle = ps[myconnectindex].jumping_toggle;
1530
    myonground = ps[myconnectindex].on_ground;
1531
    myhardlanding = ps[myconnectindex].hard_landing;
1532
    myreturntocenter = ps[myconnectindex].return_to_center;
1533
}
1534
 
1535
extern void adduserquote(char *daquote);
1536
 
274 terminx 1537
extern int gotvote[MAXPLAYERS], votes[MAXPLAYERS], voting, vote_map, vote_episode;
147 terminx 1538
 
412 terminx 1539
static void getlevelfromfilename(const char *fn, char *volume, char *level)
398 terminx 1540
{
428 terminx 1541
    for (*volume=0;*volume<MAXVOLUMES;(*volume)++)
398 terminx 1542
    {
428 terminx 1543
        for (*level=0;*level<MAXLEVELS;(*level)++)
398 terminx 1544
        {
428 terminx 1545
            if (level_file_names[(*volume*MAXLEVELS)+*level] != NULL)
1546
                if (!Bstrcasecmp(fn, level_file_names[(*volume*MAXLEVELS)+*level]))
398 terminx 1547
                    break;
1548
        }
428 terminx 1549
        if (*level != MAXLEVELS)
398 terminx 1550
            break;
1551
    }
1552
}
1553
 
5 Plagman 1554
int enterlevel(char g)
1555
{
91 terminx 1556
    short i;
5 Plagman 1557
    long l;
1558
    char levname[BMAX_PATH];
1559
 
371 terminx 1560
    ready2send = 1; // HACK: fetch any leftover p2p sync packets
368 terminx 1561
    KB_ClearKeysDown();
1562
    handleevents();
1563
    getpackets();
369 terminx 1564
    waitforeverybody();
375 terminx 1565
    ready2send = 0;
1566
 
333 terminx 1567
    if ((g&MODE_DEMO) != MODE_DEMO) ud.recstat = ud.m_recstat;
5 Plagman 1568
    ud.respawn_monsters = ud.m_respawn_monsters;
1569
    ud.respawn_items    = ud.m_respawn_items;
1570
    ud.respawn_inventory    = ud.m_respawn_inventory;
1571
    ud.monsters_off = ud.m_monsters_off;
1572
    ud.coop = ud.m_coop;
1573
    ud.marker = ud.m_marker;
1574
    ud.ffire = ud.m_ffire;
147 terminx 1575
    ud.noexits = ud.m_noexits;
5 Plagman 1576
 
274 terminx 1577
    vote_map = vote_episode = voting = -1;
147 terminx 1578
    Bmemset(votes,0,sizeof(votes));
1579
    Bmemset(gotvote,0,sizeof(gotvote));
1580
 
333 terminx 1581
    if ((g&MODE_DEMO) == 0 && ud.recstat == 2)
5 Plagman 1582
        ud.recstat = 0;
1583
 
1584
    FX_StopAllSounds();
1585
    clearsoundlocks();
1586
    FX_SetReverb(0);
1587
 
392 terminx 1588
    if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
1589
    {
400 terminx 1590
        char volume, level;
398 terminx 1591
 
412 terminx 1592
        Bcorrectfilename(boardfilename,0);
1593
 
400 terminx 1594
        getlevelfromfilename(boardfilename,&volume,&level);
398 terminx 1595
 
392 terminx 1596
        if (level != MAXLEVELS)
1597
        {
1598
            ud.level_number = ud.m_level_number = level;
1599
            ud.volume_number = ud.m_volume_number = volume;
1600
            boardfilename[0] = 0;
1601
        }
1602
    }
1603
 
382 terminx 1604
    if (level_names[(ud.volume_number*MAXLEVELS)+ud.level_number] == NULL || level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number] == NULL)
1605
    {
385 terminx 1606
        if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
1607
        {
1608
            if (level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number] == NULL)
1609
                level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number] = Bcalloc(BMAX_PATH,sizeof(char));
1610
            if (level_names[(ud.volume_number*MAXLEVELS)+ud.level_number] == NULL)
1611
            {
426 terminx 1612
                level_names[(ud.volume_number*MAXLEVELS)+ud.level_number] = Bcalloc(9,sizeof(char));
385 terminx 1613
                Bsprintf(level_names[(ud.volume_number*MAXLEVELS)+ud.level_number],"USER MAP");
1614
            }
1615
        }
1616
        else
1617
        {
1618
            initprintf("Map E%ldL%ld not defined!\n",ud.volume_number+1,ud.level_number+1);
1619
            return 1;
1620
        }
382 terminx 1621
    }
1622
 
5 Plagman 1623
    i = ud.screen_size;
1624
    ud.screen_size = 0;
1625
    dofrontscreens(NULL);
1626
    vscrn();
1627
    ud.screen_size = i;
1628
 
382 terminx 1629
    if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
1630
    {
1631
        Bstrcpy(levname, boardfilename);
1632
        Bsprintf(apptitle," - %s",levname);
1633
    }
1634
    else Bsprintf(apptitle," - %s",level_names[(ud.volume_number*MAXLEVELS)+ud.level_number]);
1635
 
385 terminx 1636
    if (VOLUMEALL) Bsprintf(tempbuf,HEAD2);
1637
    else Bsprintf(tempbuf,HEAD);
1638
 
1639
    Bstrcat(tempbuf,apptitle);
1640
    wm_setapptitle(tempbuf);
1641
 
335 terminx 1642
    if (!VOLUMEONE)
1643
    {
333 terminx 1644
        if (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0)
5 Plagman 1645
        {
333 terminx 1646
            if (loadboard(boardfilename,0,&ps[0].posx, &ps[0].posy, &ps[0].posz, &ps[0].ang,&ps[0].cursectnum) == -1)
5 Plagman 1647
            {
1648
                initprintf("Map %s not found!\n",boardfilename);
1649
                //gameexit(tempbuf);
1650
                return 1;
335 terminx 1651
            }
423 terminx 1652
 
335 terminx 1653
            {
5 Plagman 1654
                char *p;
1655
                strcpy(levname, boardfilename);
1656
                p = Bstrrchr(levname,'.');
1657
                if (!p) strcat(levname,".mhk");
335 terminx 1658
                else
1659
                {
1660
                    p[1]='m';
1661
                    p[2]='h';
1662
                    p[3]='k';
1663
                    p[4]=0;
1664
                }
5 Plagman 1665
                if (!loadmaphack(levname)) initprintf("Loaded map hack file %s\n",levname);
1666
            }
1667
        }
381 terminx 1668
        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 1669
        {
381 terminx 1670
            initprintf("Map %s not found!\n",level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number]);
5 Plagman 1671
            //gameexit(tempbuf);
1672
            return 1;
335 terminx 1673
        }
1674
        else
1675
        {
5 Plagman 1676
            char *p;
381 terminx 1677
            strcpy(levname, level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number]);
5 Plagman 1678
            p = Bstrrchr(levname,'.');
1679
            if (!p) strcat(levname,".mhk");
335 terminx 1680
            else
1681
            {
1682
                p[1]='m';
1683
                p[2]='h';
1684
                p[3]='k';
1685
                p[4]=0;
1686
            }
5 Plagman 1687
            if (!loadmaphack(levname)) initprintf("Loaded map hack file %s\n",levname);
1688
        }
1689
 
335 terminx 1690
    }
1691
    else
1692
    {
5 Plagman 1693
 
381 terminx 1694
        l = strlen(level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number]);
1695
        copybufbyte(level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number],&levname[0],l);
5 Plagman 1696
        levname[l] = 255;
1697
        levname[l+1] = 0;
1698
 
333 terminx 1699
        if (loadboard(levname,1,&ps[0].posx, &ps[0].posy, &ps[0].posz, &ps[0].ang,&ps[0].cursectnum) == -1)
5 Plagman 1700
        {
381 terminx 1701
            initprintf("Map %s not found!\n",level_file_names[(ud.volume_number*MAXLEVELS)+ud.level_number]);
5 Plagman 1702
            //gameexit(tempbuf);
1703
            return 1;
335 terminx 1704
        }
1705
        else
1706
        {
5 Plagman 1707
            char *p;
1708
            p = Bstrrchr(levname,'.');
1709
            if (!p) strcat(levname,".mhk");
335 terminx 1710
            else
1711
            {
1712
                p[1]='m';
1713
                p[2]='h';
1714
                p[3]='k';
1715
                p[4]=0;
1716
            }
5 Plagman 1717
            if (!loadmaphack(levname)) initprintf("Loaded map hack file %s\n",levname);
1718
        }
1719
    }
1720
 
1721
    precachecount = 0;
1722
    clearbufbyte(gotpic,sizeof(gotpic),0L);
1723
    clearbufbyte(precachehightile, sizeof(precachehightile), 0l);
1724
    //clearbufbyte(hittype,sizeof(hittype),0l); // JBF 20040531: yes? no?
1725
 
1726
    prelevel(g);
1727
 
1728
    allignwarpelevators();
1729
    resetpspritevars(g);
1730
 
1731
    cachedebug = 0;
1732
    automapping = 0;
1733
 
331 terminx 1734
    if (ud.recstat != 2) MUSIC_StopSong();
5 Plagman 1735
 
1736
    cacheit();
1737
 
331 terminx 1738
    if (ud.recstat != 2)
5 Plagman 1739
    {
381 terminx 1740
        music_select = (ud.volume_number*MAXLEVELS) + ud.level_number;
389 terminx 1741
        if (music_fn[0][(unsigned char)music_select] != NULL)
1742
            playmusic(&music_fn[0][(unsigned char)music_select][0]);
5 Plagman 1743
    }
1744
 
333 terminx 1745
    if ((g&MODE_GAME) || (g&MODE_EOL))
5 Plagman 1746
        ps[myconnectindex].gm = MODE_GAME;
331 terminx 1747
    else if (g&MODE_RESTART)
5 Plagman 1748
    {
331 terminx 1749
        if (ud.recstat == 2)
5 Plagman 1750
            ps[myconnectindex].gm = MODE_DEMO;
1751
        else ps[myconnectindex].gm = MODE_GAME;
1752
    }
1753
 
333 terminx 1754
    if ((ud.recstat == 1) && (g&MODE_RESTART) != MODE_RESTART)
5 Plagman 1755
        opendemowrite();
1756
 
335 terminx 1757
    if (VOLUMEONE)
1758
    {
331 terminx 1759
        if (ud.level_number == 0 && ud.recstat != 2) FTA(40,&ps[myconnectindex]);
5 Plagman 1760
    }
1761
 
331 terminx 1762
    for (i=connecthead;i>=0;i=connectpoint2[i])
1763
        switch (dynamictostatic[sector[sprite[ps[i].i].sectnum].floorpicnum])
5 Plagman 1764
        {
337 terminx 1765
        case HURTRAIL__STATIC:
1766
        case FLOORSLIME__STATIC:
1767
        case FLOORPLASMA__STATIC:
1768
            resetweapons(i);
1769
            resetinventory(i);
1770
            ps[i].gotweapon[PISTOL_WEAPON] = 0;
1771
            ps[i].ammo_amount[PISTOL_WEAPON] = 0;
1772
            ps[i].curr_weapon = KNEE_WEAPON;
1773
            ps[i].kickback_pic = 0;
1774
            break;
5 Plagman 1775
        }
1776
 
1777
    //PREMAP.C - replace near the my's at the end of the file
1778
 
1779
    resetmys();
1780
 
1781
    //ps[myconnectindex].palette = palette;
1782
    //palto(0,0,0,0);
1783
    setgamepalette(&ps[myconnectindex], palette, 0);    // JBF 20040308
1784
 
1785
    setpal(&ps[myconnectindex]);
1786
    flushperms();
1787
 
1788
    everyothertime = 0;
1789
    global_random = 0;
1790
 
1791
    ud.last_level = ud.level_number+1;
1792
 
1793
    clearfifo();
1794
 
331 terminx 1795
    for (i=numinterpolations-1;i>=0;i--) bakipos[i] = *curipos[i];
5 Plagman 1796
 
1797
    restorepalette = 1;
1798
 
1799
    flushpackets();
1800
    waitforeverybody();
1801
 
1802
    palto(0,0,0,0);
1803
    vscrn();
1804
    clearview(0L);
1805
    drawbackground();
1806
    displayrooms(myconnectindex,65536);
1807
 
1808
    clearbufbyte(playerquitflag,MAXPLAYERS,0x01010101);
1809
    ps[myconnectindex].over_shoulder_on = 0;
1810
 
1811
    clearfrags();
1812
 
1813
    resettimevars();  // Here we go
1814
 
1815
    //Bsprintf(g_szBuf,"ENTERLEVEL L=%d V=%d",ud.level_number, ud.volume_number);
1816
    //AddLog(g_szBuf);
1817
    // variables are set by pointer...
317 terminx 1818
 
5 Plagman 1819
    OnEvent(EVENT_ENTERLEVEL, -1, -1, -1);
392 terminx 1820
    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 1821
    return 0;
1822
}