Subversion Repositories eduke32

Rev

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

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