Subversion Repositories eduke32

Rev

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

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