Subversion Repositories eduke32

Rev

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