Subversion Repositories eduke32

Rev

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