Subversion Repositories eduke32

Rev

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