Subversion Repositories eduke32

Rev

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