Subversion Repositories eduke32

Rev

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