Subversion Repositories eduke32

Rev

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