Subversion Repositories eduke32

Rev

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