Subversion Repositories eduke32

Rev

Rev 274 | Rev 299 | 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];
758
        LoadActor(i, -1, -1);
759
        if(sprite[i].lotag == -1 && (sprite[i].cstat&16) )
760
        {
761
            ps[0].exitx = SX;
762
            ps[0].exity = SY;
763
        }
764
        else switch(dynamictostatic[PN])
765
            {
766
            case GPSPEED__STATIC:
767
                sector[SECT].extra = SLT;
768
                deletesprite(i);
769
                break;
770
 
771
            case CYCLER__STATIC:
772
                if(numcyclers >= MAXCYCLERS)
773
                {
774
                    Bsprintf(tempbuf,"\nToo many cycling sectors (%d max).",MAXCYCLERS);
775
                    gameexit(tempbuf);
776
                }
777
                cyclers[numcyclers][0] = SECT;
778
                cyclers[numcyclers][1] = SLT;
779
                cyclers[numcyclers][2] = SS;
780
                cyclers[numcyclers][3] = sector[SECT].floorshade;
781
                cyclers[numcyclers][4] = SHT;
782
                cyclers[numcyclers][5] = (SA == 1536);
783
                numcyclers++;
784
                deletesprite(i);
785
                break;
786
 
787
            case SECTOREFFECTOR__STATIC:
788
            case ACTIVATOR__STATIC:
789
            case TOUCHPLATE__STATIC:
790
            case ACTIVATORLOCKED__STATIC:
791
            case MUSICANDSFX__STATIC:
792
            case LOCATORS__STATIC:
793
            case MASTERSWITCH__STATIC:
794
            case RESPAWN__STATIC:
795
                sprite[i].cstat = 0;
796
                break;
797
            }
798
        i = nexti;
799
    }
800
 
801
    for(i=0;i < MAXSPRITES;i++)
802
    {
803
        if(sprite[i].statnum < MAXSTATUS)
804
        {
805
            if(PN == SECTOREFFECTOR && SLT == 14)
806
                continue;
807
            spawn(-1,i);
808
        }
809
    }
810
 
811
    for(i=0;i < MAXSPRITES;i++)
812
        if(sprite[i].statnum < MAXSTATUS)
813
        {
814
            if( PN == SECTOREFFECTOR && SLT == 14 )
815
                spawn(-1,i);
816
        }
817
 
818
    lotaglist = 0;
819
 
820
    i = headspritestat[0];
821
    while(i >= 0)
822
    {
823
        switch(dynamictostatic[PN-1])
824
        {
825
        case DIPSWITCH__STATIC:
826
        case DIPSWITCH2__STATIC:
827
        case PULLSWITCH__STATIC:
828
        case HANDSWITCH__STATIC:
829
        case SLOTDOOR__STATIC:
830
        case LIGHTSWITCH__STATIC:
831
        case SPACELIGHTSWITCH__STATIC:
832
        case SPACEDOORSWITCH__STATIC:
833
        case FRANKENSTINESWITCH__STATIC:
834
        case LIGHTSWITCH2__STATIC:
835
        case POWERSWITCH1__STATIC:
836
        case LOCKSWITCH1__STATIC:
837
        case POWERSWITCH2__STATIC:
838
            for(j=0;j<lotaglist;j++)
839
                if( SLT == lotags[j] )
840
                    break;
841
 
842
            if( j == lotaglist )
843
            {
844
                lotags[lotaglist] = SLT;
845
                lotaglist++;
846
                if(lotaglist > 64)
847
                    gameexit("\nToo many switches (64 max).");
848
 
849
                j = headspritestat[3];
850
                while(j >= 0)
851
                {
852
                    if(sprite[j].lotag == 12 && sprite[j].hitag == SLT)
853
                        hittype[j].temp_data[0] = 1;
854
                    j = nextspritestat[j];
855
                }
856
            }
857
            break;
858
        }
859
        i = nextspritestat[i];
860
    }
861
 
862
    mirrorcnt = 0;
863
 
864
    for( i = 0; i < numwalls; i++ )
865
    {
866
        walltype *wal;
867
        wal = &wall[i];
868
 
869
        if(wal->overpicnum == MIRROR && (wal->cstat&32) != 0)
870
        {
871
            j = wal->nextsector;
872
 
873
            if(mirrorcnt > 63)
874
                gameexit("\nToo many mirrors (64 max.)");
875
            if ( (j >= 0) && sector[j].ceilingpicnum != MIRROR )
876
            {
877
                sector[j].ceilingpicnum = MIRROR;
878
                sector[j].floorpicnum = MIRROR;
879
                mirrorwall[mirrorcnt] = i;
880
                mirrorsector[mirrorcnt] = j;
881
                mirrorcnt++;
882
                continue;
883
            }
884
        }
885
 
886
        if(numanimwalls >= MAXANIMWALLS)
887
        {
888
            Bsprintf(tempbuf,"\nToo many 'anim' walls (%d max).",MAXANIMWALLS);
889
            gameexit(tempbuf);
890
        }
891
 
892
        animwall[numanimwalls].tag = 0;
893
        animwall[numanimwalls].wallnum = 0;
894
        switchpicnum = wal->overpicnum;
895
        if ((wal->overpicnum > W_FORCEFIELD)&&(wal->overpicnum <= W_FORCEFIELD+2)) {
896
            switchpicnum = W_FORCEFIELD;
897
        }
898
        switch(dynamictostatic[switchpicnum])
899
        {
900
        case FANSHADOW__STATIC:
901
        case FANSPRITE__STATIC:
902
            wall->cstat |= 65;
903
            animwall[numanimwalls].wallnum = i;
904
            numanimwalls++;
905
            break;
906
 
907
        case W_FORCEFIELD__STATIC:
908
            if (wal->overpicnum==W_FORCEFIELD__STATIC)
909
                for(j=0;j<3;j++)
910
                    tloadtile(W_FORCEFIELD+j, 0);
911
            if(wal->shade > 31)
912
                wal->cstat = 0;
913
            else wal->cstat |= 85+256;
914
 
915
 
916
            if(wal->lotag && wal->nextwall >= 0)
917
                wall[wal->nextwall].lotag =
918
                    wal->lotag;
919
 
920
        case BIGFORCE__STATIC:
921
 
922
            animwall[numanimwalls].wallnum = i;
923
            numanimwalls++;
924
 
925
            continue;
926
        }
927
 
928
        wal->extra = -1;
929
 
930
        switch(dynamictostatic[wal->picnum])
931
        {
932
        case WATERTILE2__STATIC:
933
            for(j=0;j<3;j++)
934
                tloadtile(wal->picnum+j, 0);
935
            break;
936
 
937
        case TECHLIGHT2__STATIC:
938
        case TECHLIGHT4__STATIC:
939
            tloadtile(wal->picnum, 0);
940
            break;
941
        case W_TECHWALL1__STATIC:
942
        case W_TECHWALL2__STATIC:
943
        case W_TECHWALL3__STATIC:
944
        case W_TECHWALL4__STATIC:
945
            animwall[numanimwalls].wallnum = i;
946
            //                animwall[numanimwalls].tag = -1;
947
            numanimwalls++;
948
            break;
949
        case SCREENBREAK6__STATIC:
950
        case SCREENBREAK7__STATIC:
951
        case SCREENBREAK8__STATIC:
952
            for(j=SCREENBREAK6;j<SCREENBREAK9;j++)
953
                tloadtile(j, 0);
954
            animwall[numanimwalls].wallnum = i;
955
            animwall[numanimwalls].tag = -1;
956
            numanimwalls++;
957
            break;
958
 
959
        case FEMPIC1__STATIC:
960
        case FEMPIC2__STATIC:
961
        case FEMPIC3__STATIC:
962
 
963
            wal->extra = wal->picnum;
964
            animwall[numanimwalls].tag = -1;
965
            if(ud.lockout)
966
            {
967
                if(wal->picnum == FEMPIC1)
968
                    wal->picnum = BLANKSCREEN;
969
                else wal->picnum = SCREENBREAK6;
970
            }
971
 
972
            animwall[numanimwalls].wallnum = i;
973
            animwall[numanimwalls].tag = wal->picnum;
974
            numanimwalls++;
975
            break;
976
 
977
        case SCREENBREAK1__STATIC:
978
        case SCREENBREAK2__STATIC:
979
        case SCREENBREAK3__STATIC:
980
        case SCREENBREAK4__STATIC:
981
        case SCREENBREAK5__STATIC:
982
 
983
        case SCREENBREAK9__STATIC:
984
        case SCREENBREAK10__STATIC:
985
        case SCREENBREAK11__STATIC:
986
        case SCREENBREAK12__STATIC:
987
        case SCREENBREAK13__STATIC:
988
        case SCREENBREAK14__STATIC:
989
        case SCREENBREAK15__STATIC:
990
        case SCREENBREAK16__STATIC:
991
        case SCREENBREAK17__STATIC:
992
        case SCREENBREAK18__STATIC:
993
        case SCREENBREAK19__STATIC:
994
 
995
            animwall[numanimwalls].wallnum = i;
996
            animwall[numanimwalls].tag = wal->picnum;
997
            numanimwalls++;
998
            break;
999
        }
1000
    }
1001
 
1002
    //Invalidate textures in sector behind mirror
1003
    for(i=0;i<mirrorcnt;i++)
1004
    {
1005
        startwall = sector[mirrorsector[i]].wallptr;
1006
        endwall = startwall + sector[mirrorsector[i]].wallnum;
1007
        for(j=startwall;j<endwall;j++)
1008
        {
1009
            wall[j].picnum = MIRROR;
1010
            wall[j].overpicnum = MIRROR;
1011
        }
1012
    }
1013
}
1014
 
1015
void newgame(char vn,char ln,char sk)
1016
{
1017
    struct player_struct *p = &ps[0];
1018
    short i;
1019
 
181 terminx 1020
    if(globalskillsound >= 0 && FXDevice >= 0 && SoundToggle)
179 terminx 1021
    while(issoundplaying(-1,globalskillsound)) { handleevents(); getpackets(); }
194 terminx 1022
    else { handleevents(); getpackets(); }
5 Plagman 1023
    globalskillsound = -1;
1024
 
1025
    waitforeverybody();
1026
    ready2send = 0;
1027
 
1028
    if( ud.m_recstat != 2 && ud.last_level >= 0 && ud.multimode > 1 && (ud.coop&GAMETYPE_FLAG_SCORESHEET))
1029
        dobonus(1);
1030
 
1031
    if( ln == 0 && vn == 3 && ud.multimode < 2 && ud.lockout == 0)
1032
    {
1033
        playmusic(&env_music_fn[1][0]);
1034
 
1035
        flushperms();
1036
        setview(0,0,xdim-1,ydim-1);
1037
        clearview(0L);
1038
        nextpage();
1039
 
1040
        playanm("vol41a.anm",6);
1041
        clearview(0L);
1042
        nextpage();
1043
 
1044
        playanm("vol42a.anm",7);
1045
        playanm("vol43a.anm",9);
1046
        clearview(0L);
1047
        nextpage();
1048
 
1049
        FX_StopAllSounds();
1050
    }
1051
 
1052
    show_shareware = 26*34;
1053
 
1054
    ud.level_number =   ln;
1055
    ud.volume_number =  vn;
1056
    ud.player_skill =   sk;
1057
    ud.secretlevel =    0;
1058
    ud.from_bonus = 0;
1059
    parallaxyscale = 0;
1060
 
1061
    ud.last_level = -1;
1062
    lastsavedpos = -1;
1063
    p->zoom            = 768;
1064
    p->gm              = 0;
1065
 
1066
    {
1067
        //AddLog("Newgame");
1068
        ResetGameVars();
1069
 
1070
        InitGameVarPointers();
1071
 
1072
        ResetSystemDefaults();
1073
 
1074
        if(ud.m_coop != 1)
1075
        {
1076
            for(i=0;i<MAX_WEAPONS;i++)
1077
            {
1078
                if(aplWeaponWorksLike[i][0]==PISTOL_WEAPON)
1079
                {
1080
                    p->curr_weapon = i;
1081
                    p->gotweapon[i] = 1;
1082
                    p->ammo_amount[i] = 48;
1083
                }
1084
                else if(aplWeaponWorksLike[i][0]==KNEE_WEAPON)
1085
                    p->gotweapon[i] = 1;
1086
                else if(aplWeaponWorksLike[i][0]==HANDREMOTE_WEAPON)
1087
                    p->gotweapon[i] = 1;
1088
            }
1089
            p->last_weapon = -1;
1090
        }
1091
    }
1092
    display_mirror =        0;
1093
 
1094
    if(ud.multimode > 1 )
1095
    {
1096
        if(numplayers < 2)
1097
        {
1098
            connecthead = 0;
1099
            for(i=0;i<MAXPLAYERS;i++) connectpoint2[i] = i+1;
1100
            connectpoint2[ud.multimode-1] = -1;
1101
        }
1102
    }
1103
    else
1104
    {
1105
        connecthead = 0;
1106
        connectpoint2[0] = -1;
1107
    }
1108
}
1109
 
1110
void resetpspritevars(char g)
1111
{
1112
    short i, j, nexti,circ;
1113
    long firstx,firsty;
1114
    spritetype *s;
1115
    char aimmode[MAXPLAYERS],autoaim[MAXPLAYERS],weaponswitch[MAXPLAYERS];
1116
    STATUSBARTYPE tsbar[MAXPLAYERS];
1117
 
1118
    EGS(ps[0].cursectnum,ps[0].posx,ps[0].posy,ps[0].posz,
1119
        APLAYER,0,0,0,ps[0].ang,0,0,0,10);
1120
 
1121
    if(ud.recstat != 2) for(i=0;i<MAXPLAYERS;i++)
1122
        {
1123
            aimmode[i] = ps[i].aim_mode;
1124
            autoaim[i] = ps[i].auto_aim;
29 terminx 1125
            weaponswitch[i] = ps[i].weaponswitch;
5 Plagman 1126
            if(ud.multimode > 1 && (gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
1127
            {
1128
                for(j=0;j<MAX_WEAPONS;j++)
1129
                {
1130
                    tsbar[i].ammo_amount[j] = ps[i].ammo_amount[j];
1131
                    tsbar[i].gotweapon[j] = ps[i].gotweapon[j];
1132
                }
1133
 
1134
                tsbar[i].shield_amount = ps[i].shield_amount;
1135
                tsbar[i].curr_weapon = ps[i].curr_weapon;
1136
                tsbar[i].inven_icon = ps[i].inven_icon;
1137
 
1138
                tsbar[i].firstaid_amount = ps[i].firstaid_amount;
1139
                tsbar[i].steroids_amount = ps[i].steroids_amount;
1140
                tsbar[i].holoduke_amount = ps[i].holoduke_amount;
1141
                tsbar[i].jetpack_amount = ps[i].jetpack_amount;
1142
                tsbar[i].heat_amount = ps[i].heat_amount;
1143
                tsbar[i].scuba_amount = ps[i].scuba_amount;
1144
                tsbar[i].boot_amount = ps[i].boot_amount;
1145
            }
1146
        }
1147
 
1148
    resetplayerstats(0);
1149
 
1150
    for(i=1;i<MAXPLAYERS;i++)
1151
        memcpy(&ps[i],&ps[0],sizeof(ps[0]));
1152
 
1153
    if(ud.recstat != 2) for(i=0;i<MAXPLAYERS;i++)
1154
        {
1155
            ps[i].aim_mode = aimmode[i];
1156
            ps[i].auto_aim = autoaim[i];
29 terminx 1157
            ps[i].weaponswitch = weaponswitch[i];
5 Plagman 1158
            if(ud.multimode > 1 && (gametype_flags[ud.coop]&GAMETYPE_FLAG_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
1159
            {
1160
                for(j=0;j<MAX_WEAPONS;j++)
1161
                {
1162
                    ps[i].ammo_amount[j] = tsbar[i].ammo_amount[j];
1163
                    ps[i].gotweapon[j] = tsbar[i].gotweapon[j];
1164
                }
1165
                ps[i].shield_amount = tsbar[i].shield_amount;
1166
                ps[i].curr_weapon = tsbar[i].curr_weapon;
1167
                ps[i].inven_icon = tsbar[i].inven_icon;
1168
 
1169
                ps[i].firstaid_amount = tsbar[i].firstaid_amount;
1170
                ps[i].steroids_amount= tsbar[i].steroids_amount;
1171
                ps[i].holoduke_amount = tsbar[i].holoduke_amount;
1172
                ps[i].jetpack_amount = tsbar[i].jetpack_amount;
1173
                ps[i].heat_amount = tsbar[i].heat_amount;
1174
                ps[i].scuba_amount= tsbar[i].scuba_amount;
1175
                ps[i].boot_amount = tsbar[i].boot_amount;
1176
            }
1177
        }
1178
 
1179
    numplayersprites = 0;
1180
    circ = 2048/ud.multimode;
1181
 
1182
    which_palookup = 9;
1183
    j = connecthead;
1184
    i = headspritestat[10];
1185
    while(i >= 0)
1186
    {
1187
        nexti = nextspritestat[i];
1188
        s = &sprite[i];
1189
 
1190
        if( numplayersprites == MAXPLAYERS)
1191
            gameexit("\nToo many player sprites (max 16.)");
1192
 
1193
        if(numplayersprites == 0)
1194
        {
1195
            firstx = ps[0].posx;
1196
            firsty = ps[0].posy;
1197
        }
1198
 
1199
        po[numplayersprites].ox = s->x;
1200
        po[numplayersprites].oy = s->y;
1201
        po[numplayersprites].oz = s->z;
1202
        po[numplayersprites].oa = s->ang;
1203
        po[numplayersprites].os = s->sectnum;
1204
 
1205
        numplayersprites++;
1206
        if(j >= 0)
1207
        {
1208
            s->owner = i;
1209
            s->shade = 0;
1210
            s->xrepeat = 42;
1211
            s->yrepeat = 36;
1212
            s->cstat = 1+256;
1213
            s->xoffset = 0;
1214
            s->clipdist = 64;
1215
 
1216
            if( (g&MODE_EOL) != MODE_EOL || ps[j].last_extra == 0)
1217
            {
1218
                ps[j].last_extra = max_player_health;
1219
                s->extra = max_player_health;
1220
                ps[j].runspeed = dukefriction;
1221
            }
1222
            else s->extra = ps[j].last_extra;
1223
 
1224
            s->yvel = j;
1225
 
268 terminx 1226
            if(!ud.pcolor[j] && ud.multimode > 1 && !(gametype_flags[ud.coop] & GAMETYPE_FLAG_TDM))
5 Plagman 1227
            {
53 terminx 1228
                if(s->pal == 0)
1229
                {
56 terminx 1230
                    int k;
1231
 
101 terminx 1232
                    for(k=0;k<MAXPLAYERS;k++) {
57 terminx 1233
                        if(which_palookup == ps[k].palookup) {
56 terminx 1234
                            which_palookup++;
1235
                            if( which_palookup >= 17 )
1236
                                which_palookup = 9;
1237
                            k=0;
1238
                        }
1239
                    }
273 terminx 1240
                    ud.pcolor[j] = s->pal = ps[j].palookup = which_palookup++;
56 terminx 1241
                    if( which_palookup >= 17 )
1242
                        which_palookup = 9;
53 terminx 1243
                }
273 terminx 1244
                else ud.pcolor[j] = ps[j].palookup = s->pal;
268 terminx 1245
            }
1246
            else
1247
            {
1248
                int k = ud.pcolor[j];
5 Plagman 1249
 
268 terminx 1250
                if(gametype_flags[ud.coop] & GAMETYPE_FLAG_TDM)
1251
                {
1252
                    switch(ud.pteam[j])
1253
                    {
1254
                    case 0: k = 3; break;
272 terminx 1255
                    case 1: k = 21; break;
268 terminx 1256
                    }
1257
                    ps[j].team = ud.pteam[j];
1258
                }
1259
                s->pal = ps[j].palookup = k;
1260
            }
1261
 
5 Plagman 1262
            ps[j].i = i;
1263
            ps[j].frag_ps = j;
1264
            hittype[i].owner = i;
1265
 
1266
            hittype[i].bposx = ps[j].bobposx = ps[j].oposx = ps[j].posx =        s->x;
1267
            hittype[i].bposy = ps[j].bobposy = ps[j].oposy = ps[j].posy =        s->y;
1268
            hittype[i].bposz = ps[j].oposz = ps[j].posz =        s->z;
1269
            ps[j].oang  = ps[j].ang  =        s->ang;
1270
 
1271
            updatesector(s->x,s->y,&ps[j].cursectnum);
1272
 
1273
            j = connectpoint2[j];
1274
 
1275
        }
1276
        else deletesprite(i);
1277
        i = nexti;
1278
    }
1279
}
1280
 
1281
void clearfrags(void)
1282
{
1283
    short i;
1284
 
1285
    for(i = 0;i<MAXPLAYERS;i++)
1286
        ps[i].frag = ps[i].fraggedself = 0;
1287
    clearbufbyte(&frags[0][0],(MAXPLAYERS*MAXPLAYERS)<<1,0L);
1288
}
1289
 
1290
void resettimevars(void)
1291
{
1292
    vel = svel = angvel = horiz = 0;
1293
 
1294
    totalclock = 0L;
1295
    cloudtotalclock = 0L;
1296
    ototalclock = 0L;
1297
    lockclock = 0L;
1298
    ready2send = 1;
1299
}
1300
 
1301
void genspriteremaps(void)
1302
{
1303
    long j,fp;
1304
    signed char look_pos;
1305
    char *lookfn = "lookup.dat";
1306
 
1307
    fp = kopen4load(lookfn,0);
1308
    if(fp != -1)
1309
        kread(fp,(char *)&numl,1);
1310
    else
1311
        gameexit("\nERROR: File 'LOOKUP.DAT' not found.");
1312
 
1313
    for(j=0;j < numl;j++)
1314
    {
1315
        kread(fp,(signed char *)&look_pos,1);
1316
        kread(fp,tempbuf,256);
1317
        makepalookup((long)look_pos,tempbuf,0,0,0,1);
1318
    }
1319
 
1320
    for (j = 0; j < 256; j++)
1321
        tempbuf[j] = j;
1322
    numl++;
1323
    makepalookup(numl, tempbuf, 15, 15, 15, 1);
1324
    numl++;
1325
    makepalookup(numl, tempbuf, 15, 0, 0, 1);
1326
    numl++;
1327
    makepalookup(numl, tempbuf, 0, 15, 0, 1);
1328
    numl++;
1329
    makepalookup(numl, tempbuf, 0, 0, 15, 1);
1330
 
1331
    numl -= 3;
1332
    kread(fp,&waterpal[0],768);
1333
    kread(fp,&slimepal[0],768);
1334
    kread(fp,&titlepal[0],768);
1335
    kread(fp,&drealms[0],768);
1336
    kread(fp,&endingpal[0],768);
1337
 
1338
    palette[765] = palette[766] = palette[767] = 0;
1339
    slimepal[765] = slimepal[766] = slimepal[767] = 0;
1340
    waterpal[765] = waterpal[766] = waterpal[767] = 0;
1341
 
1342
    kclose(fp);
1343
}
1344
 
1345
void waitforeverybody()
1346
{
1347
    long i;
1348
 
1349
    if (numplayers < 2) return;
1350
    packbuf[0] = 250;
1351
    for(i=connecthead;i>=0;i=connectpoint2[i])
1352
    {
1353
        if (i != myconnectindex) sendpacket(i,packbuf,1);
1354
        if ((!networkmode) && (myconnectindex != connecthead)) break; //slaves in M/S mode only send to master
1355
    }
1356
    playerreadyflag[myconnectindex]++;
1357
 
1358
    while (1)
1359
    {
1360
        handleevents();
1361
        AudioUpdate();
1362
 
1363
        if (quitevent || keystatus[1]) gameexit("");
1364
 
1365
        getpackets();
1366
 
1367
        for(i=connecthead;i>=0;i=connectpoint2[i])
1368
        {
1369
            if (playerreadyflag[i] < playerreadyflag[myconnectindex]) break;
1370
        if ((!networkmode) && (myconnectindex != connecthead)) { i = -1; break; } //slaves in M/S mode only wait for master
1371
        }
1372
        if (i < 0) return;
1373
    }
1374
}
1375
 
1376
void dofrontscreens(char *statustext)
1377
{
119 terminx 1378
    long i=0,j;
5 Plagman 1379
 
1380
    if(ud.recstat != 2)
1381
    {
1382
        if (!statustext) {
1383
            //ps[myconnectindex].palette = palette;
1384
            setgamepalette(&ps[myconnectindex], palette, 1);    // JBF 20040308
1385
            fadepal(0,0,0, 0,64,7);
1386
            i = ud.screen_size;
1387
            ud.screen_size = 0;
1388
            vscrn();
1389
            clearview(0L);
1390
        }
1391
 
1392
        SetGameVarID(g_iReturnVarID,LOADSCREEN, -1, -1);
1393
        OnEvent(EVENT_GETLOADTILE, -1, -1, -1);
119 terminx 1394
        j = GetGameVarID(g_iReturnVarID, -1, -1);
1395
        rotatesprite(320<<15,200<<15,65536L,0,j > MAXTILES-1?j-MAXTILES:j,0,0,2+8+64,0,0,xdim-1,ydim-1);
1396
        if(j > MAXTILES-1)
1397
        {
1398
            nextpage();
1399
            return;
1400
        }
5 Plagman 1401
        if( boardfilename[0] != 0 && ud.level_number == 7 && ud.volume_number == 0 )
1402
        {
1403
            menutext(160,90,0,0,"ENTERING USER MAP");
1404
            gametextpal(160,90+10,boardfilename,14,2);
1405
        }
1406
        else
1407
        {
1408
            menutext(160,90,0,0,"ENTERING");
1409
            menutext(160,90+16+8,0,0,level_names[(ud.volume_number*11) + ud.level_number]);
1410
        }
1411
 
1412
        if (statustext) gametext(160,180,statustext,0,2+8+16);
1413
 
1414
        nextpage();
1415
 
1416
        if (!statustext)
1417
        {
1418
            fadepal(0,0,0, 63,0,-7);
1419
 
1420
            KB_FlushKeyboardQueue();
1421
            ud.screen_size = i;
1422
        }
1423
    }
1424
    else
1425
    {
1426
        if (!statustext)
1427
        {
1428
            clearview(0L);
1429
            //ps[myconnectindex].palette = palette;
1430
            //palto(0,0,0,0);
1431
            setgamepalette(&ps[myconnectindex], palette, 0);    // JBF 20040308
1432
        }
1433
        SetGameVarID(g_iReturnVarID,LOADSCREEN, -1, -1);
1434
        OnEvent(EVENT_GETLOADTILE, -1, -1, -1);
119 terminx 1435
        j = GetGameVarID(g_iReturnVarID, -1, -1);
1436
        rotatesprite(320<<15,200<<15,65536L,0,j > MAXTILES-1?j-MAXTILES:j,0,0,2+8+64,0,0,xdim-1,ydim-1);
1437
        if(j > MAXTILES-1)
1438
        {
1439
            nextpage();
1440
            return;
1441
        }
5 Plagman 1442
        menutext(160,105,0,0,"LOADING...");
1443
        if (statustext) gametext(160,180,statustext,0,2+8+16);
1444
        nextpage();
1445
    }
1446
}
1447
 
1448
void clearfifo(void)
1449
{
1450
    syncvaltail = 0L;
1451
    syncvaltottail = 0L;
1452
    syncstat = 0;
1453
    bufferjitter = 1;
1454
    mymaxlag = otherminlag = 0;
1455
 
1456
    movefifoplc = movefifosendplc = fakemovefifoplc = 0;
275 terminx 1457
    avgfvel = avgsvel = avgavel = avghorz = avgbits = avgextbits = 0;
5 Plagman 1458
    otherminlag = mymaxlag = 0;
1459
 
1460
    clearbufbyte(myminlag,MAXPLAYERS<<2,0L);
1461
    clearbufbyte(&loc,sizeof(input),0L);
1462
    clearbufbyte(&sync[0],sizeof(sync),0L);
1463
    clearbufbyte(inputfifo,sizeof(input)*MOVEFIFOSIZ*MAXPLAYERS,0L);
1464
 
1465
    clearbuf(movefifoend,MAXPLAYERS,0L);
1466
    clearbuf(syncvalhead,MAXPLAYERS,0L);
1467
    clearbuf(myminlag,MAXPLAYERS,0L);
1468
 
1469
    //    clearbufbyte(playerquitflag,MAXPLAYERS,0x01);
1470
}
1471
 
1472
void resetmys(void)
1473
{
1474
    myx = omyx = ps[myconnectindex].posx;
1475
    myy = omyy = ps[myconnectindex].posy;
1476
    myz = omyz = ps[myconnectindex].posz;
1477
    myxvel = myyvel = myzvel = 0;
1478
    myang = omyang = ps[myconnectindex].ang;
1479
    myhoriz = omyhoriz = ps[myconnectindex].horiz;
1480
    myhorizoff = omyhorizoff = ps[myconnectindex].horizoff;
1481
    mycursectnum = ps[myconnectindex].cursectnum;
1482
    myjumpingcounter = ps[myconnectindex].jumping_counter;
1483
    myjumpingtoggle = ps[myconnectindex].jumping_toggle;
1484
    myonground = ps[myconnectindex].on_ground;
1485
    myhardlanding = ps[myconnectindex].hard_landing;
1486
    myreturntocenter = ps[myconnectindex].return_to_center;
1487
}
1488
 
1489
extern void adduserquote(char *daquote);
1490
 
274 terminx 1491
extern int gotvote[MAXPLAYERS], votes[MAXPLAYERS], voting, vote_map, vote_episode;
147 terminx 1492
 
5 Plagman 1493
int enterlevel(char g)
1494
{
91 terminx 1495
    short i;
5 Plagman 1496
    long l;
1497
    char levname[BMAX_PATH];
1498
 
1499
    if( (g&MODE_DEMO) != MODE_DEMO ) ud.recstat = ud.m_recstat;
1500
    ud.respawn_monsters = ud.m_respawn_monsters;
1501
    ud.respawn_items    = ud.m_respawn_items;
1502
    ud.respawn_inventory    = ud.m_respawn_inventory;
1503
    ud.monsters_off = ud.m_monsters_off;
1504
    ud.coop = ud.m_coop;
1505
    ud.marker = ud.m_marker;
1506
    ud.ffire = ud.m_ffire;
147 terminx 1507
    ud.noexits = ud.m_noexits;
5 Plagman 1508
 
274 terminx 1509
    vote_map = vote_episode = voting = -1;
147 terminx 1510
    Bmemset(votes,0,sizeof(votes));
1511
    Bmemset(gotvote,0,sizeof(gotvote));
1512
 
5 Plagman 1513
    if( (g&MODE_DEMO) == 0 && ud.recstat == 2)
1514
        ud.recstat = 0;
1515
 
1516
    FX_StopAllSounds();
1517
    clearsoundlocks();
1518
    FX_SetReverb(0);
1519
 
1520
    i = ud.screen_size;
1521
    ud.screen_size = 0;
1522
    dofrontscreens(NULL);
1523
    vscrn();
1524
    ud.screen_size = i;
1525
 
1526
    if (!VOLUMEONE) {
1527
 
1528
        if( boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0 )
1529
        {
1530
            if ( loadboard( boardfilename,0,&ps[0].posx, &ps[0].posy, &ps[0].posz, &ps[0].ang,&ps[0].cursectnum ) == -1 )
1531
            {
1532
                initprintf("Map %s not found!\n",boardfilename);
1533
                //gameexit(tempbuf);
1534
                return 1;
1535
            } else {
1536
                char *p;
1537
                strcpy(levname, boardfilename);
1538
                p = Bstrrchr(levname,'.');
1539
                if (!p) strcat(levname,".mhk");
1540
            else { p[1]='m'; p[2]='h'; p[3]='k'; p[4]=0; }
1541
                if (!loadmaphack(levname)) initprintf("Loaded map hack file %s\n",levname);
1542
            }
1543
        }
1544
        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)
1545
        {
1546
            initprintf("Map %s not found!\n",level_file_names[(ud.volume_number*11)+ud.level_number]);
1547
            //gameexit(tempbuf);
1548
            return 1;
1549
        } else {
1550
            char *p;
1551
            strcpy(levname, level_file_names[ (ud.volume_number*11)+ud.level_number]);
1552
            p = Bstrrchr(levname,'.');
1553
            if (!p) strcat(levname,".mhk");
1554
        else { p[1]='m'; p[2]='h'; p[3]='k'; p[4]=0; }
1555
            if (!loadmaphack(levname)) initprintf("Loaded map hack file %s\n",levname);
1556
        }
1557
 
1558
    } else {
1559
 
1560
        l = strlen(level_file_names[ (ud.volume_number*11)+ud.level_number]);
1561
        copybufbyte( level_file_names[ (ud.volume_number*11)+ud.level_number],&levname[0],l);
1562
        levname[l] = 255;
1563
        levname[l+1] = 0;
1564
 
1565
        if ( loadboard( levname,1,&ps[0].posx, &ps[0].posy, &ps[0].posz, &ps[0].ang,&ps[0].cursectnum ) == -1)
1566
        {
1567
            initprintf("Map %s not found!\n",level_file_names[(ud.volume_number*11)+ud.level_number]);
1568
            //gameexit(tempbuf);
1569
            return 1;
1570
        } else {
1571
            char *p;
1572
            p = Bstrrchr(levname,'.');
1573
            if (!p) strcat(levname,".mhk");
1574
        else { p[1]='m'; p[2]='h'; p[3]='k'; p[4]=0; }
1575
            if (!loadmaphack(levname)) initprintf("Loaded map hack file %s\n",levname);
1576
        }
1577
    }
1578
 
1579
    precachecount = 0;
1580
    clearbufbyte(gotpic,sizeof(gotpic),0L);
1581
    clearbufbyte(precachehightile, sizeof(precachehightile), 0l);
1582
    //clearbufbyte(hittype,sizeof(hittype),0l); // JBF 20040531: yes? no?
1583
 
1584
    prelevel(g);
1585
 
1586
    allignwarpelevators();
1587
    resetpspritevars(g);
1588
 
1589
    cachedebug = 0;
1590
    automapping = 0;
1591
 
1592
    if(ud.recstat != 2) MUSIC_StopSong();
1593
 
1594
    cacheit();
1595
 
1596
    if(ud.recstat != 2)
1597
    {
1598
        music_select = (ud.volume_number*11) + ud.level_number;
1599
        playmusic(&music_fn[0][music_select][0]);
1600
    }
1601
 
1602
    if( (g&MODE_GAME) || (g&MODE_EOL) )
1603
        ps[myconnectindex].gm = MODE_GAME;
1604
    else if(g&MODE_RESTART)
1605
    {
1606
        if(ud.recstat == 2)
1607
            ps[myconnectindex].gm = MODE_DEMO;
1608
        else ps[myconnectindex].gm = MODE_GAME;
1609
    }
1610
 
1611
    if( (ud.recstat == 1) && (g&MODE_RESTART) != MODE_RESTART )
1612
        opendemowrite();
1613
 
1614
    if (VOLUMEONE) {
1615
        if(ud.level_number == 0 && ud.recstat != 2) FTA(40,&ps[myconnectindex]);
1616
    }
1617
 
1618
    for(i=connecthead;i>=0;i=connectpoint2[i])
1619
        switch(dynamictostatic[sector[sprite[ps[i].i].sectnum].floorpicnum])
1620
        {
1621
        case HURTRAIL__STATIC:
1622
        case FLOORSLIME__STATIC:
1623
        case FLOORPLASMA__STATIC:
1624
            resetweapons(i);
1625
            resetinventory(i);
1626
            ps[i].gotweapon[PISTOL_WEAPON] = 0;
1627
            ps[i].ammo_amount[PISTOL_WEAPON] = 0;
1628
            ps[i].curr_weapon = KNEE_WEAPON;
1629
            ps[i].kickback_pic = 0;
1630
            break;
1631
        }
1632
 
1633
    //PREMAP.C - replace near the my's at the end of the file
1634
 
1635
    resetmys();
1636
 
1637
    //ps[myconnectindex].palette = palette;
1638
    //palto(0,0,0,0);
1639
    setgamepalette(&ps[myconnectindex], palette, 0);    // JBF 20040308
1640
 
1641
    setpal(&ps[myconnectindex]);
1642
    flushperms();
1643
 
1644
    everyothertime = 0;
1645
    global_random = 0;
1646
 
1647
    ud.last_level = ud.level_number+1;
1648
 
1649
    clearfifo();
1650
 
1651
    for(i=numinterpolations-1;i>=0;i--) bakipos[i] = *curipos[i];
1652
 
1653
    restorepalette = 1;
1654
 
1655
    flushpackets();
1656
    waitforeverybody();
1657
 
1658
    palto(0,0,0,0);
1659
    vscrn();
1660
    clearview(0L);
1661
    drawbackground();
1662
    displayrooms(myconnectindex,65536);
1663
 
1664
    clearbufbyte(playerquitflag,MAXPLAYERS,0x01010101);
1665
    ps[myconnectindex].over_shoulder_on = 0;
1666
 
1667
    clearfrags();
1668
 
1669
    resettimevars();  // Here we go
1670
 
1671
    //Bsprintf(g_szBuf,"ENTERLEVEL L=%d V=%d",ud.level_number, ud.volume_number);
1672
    //AddLog(g_szBuf);
1673
    // variables are set by pointer...
1674
    OnEvent(EVENT_ENTERLEVEL, -1, -1, -1);
1675
    return 0;
1676
}