Subversion Repositories eduke32

Rev

Rev 4986 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 Plagman 1
//-------------------------------------------------------------------------
2
/*
1652 terminx 3
Copyright (C) 2010 EDuke32 developers and contributors
5 Plagman 4
 
1652 terminx 5
This file is part of EDuke32.
5 Plagman 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
4541 hendricks2 19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
5 Plagman 20
*/
21
//-------------------------------------------------------------------------
22
 
23
#include "duke3d.h"
2290 helixhorne 24
#include "game.h"
3220 hendricks2 25
#include "common_game.h"
5 Plagman 26
#include "osd.h"
1560 terminx 27
#include "gamedef.h"
1677 terminx 28
#include "premap.h"
29
#include "sounds.h"
4449 helixhorne 30
#include "fx_man.h"
1677 terminx 31
#include "gameexec.h"
32
#include "anim.h"
33
#include "menus.h"
1678 terminx 34
#include "demo.h"
5 Plagman 35
 
2721 helixhorne 36
#ifdef LUNATIC
3148 helixhorne 37
# include "lunatic_game.h"
2034 helixhorne 38
#endif
39
 
3421 helixhorne 40
halfdimen_t g_halfScreen;
41
int32_t g_halveScreenArea = 0;
42
 
1205 terminx 43
static int32_t g_whichPalForPlayer = 9;
5 Plagman 44
 
1228 terminx 45
static uint8_t precachehightile[2][MAXTILES>>3];
1205 terminx 46
static int32_t  g_precacheCount;
5 Plagman 47
 
1205 terminx 48
extern int32_t g_levelTextTime;
886 terminx 49
 
3204 helixhorne 50
static void flag_precache(int32_t tile, int32_t type)
51
{
52
    if (!(gotpic[tile>>3] & pow2char[tile&7]))
53
        g_precacheCount++;
54
    gotpic[tile>>3] |= pow2char[tile&7];
55
    precachehightile[type][tile>>3] |= pow2char[tile&7];
56
}
57
 
1205 terminx 58
static void tloadtile(int32_t tilenume, int32_t type)
5 Plagman 59
{
3204 helixhorne 60
    int32_t i,j;
61
 
62
    if ((picanm[tilenume].sf&PICANM_ANIMTYPE_MASK)==PICANM_ANIMTYPE_BACK)
335 terminx 63
    {
3204 helixhorne 64
        i = tilenume - picanm[tilenume].num;
65
        j = tilenume;
423 terminx 66
    }
3204 helixhorne 67
    else
423 terminx 68
    {
3204 helixhorne 69
        i = tilenume;
70
        j = tilenume + picanm[tilenume].num;
71
    }
5 Plagman 72
 
3204 helixhorne 73
    for (; i<=j; i++)
74
        flag_precache(i, type);
5 Plagman 75
}
76
 
1205 terminx 77
static void G_CacheSpriteNum(int32_t i)
5 Plagman 78
{
79
    char maxc;
1205 terminx 80
    int32_t j;
5 Plagman 81
 
1143 terminx 82
    if (ud.monsters_off && A_CheckEnemySprite(&sprite[i])) return;
5 Plagman 83
 
84
    maxc = 1;
85
 
4291 helixhorne 86
    for (j = PN; j <= g_tile[PN].cacherange; j++)
87
        tloadtile(j,1);
5 Plagman 88
 
2297 helixhorne 89
    switch (DYNAMICTILEMAP(PN))
5 Plagman 90
    {
337 terminx 91
    case HYDRENT__STATIC:
92
        tloadtile(BROKEFIREHYDRENT,1);
93
        for (j = TOILETWATER; j < (TOILETWATER+4); j++) tloadtile(j,1);
94
        break;
95
    case TOILET__STATIC:
96
        tloadtile(TOILETBROKE,1);
97
        for (j = TOILETWATER; j < (TOILETWATER+4); j++) tloadtile(j,1);
98
        break;
99
    case STALL__STATIC:
100
        tloadtile(STALLBROKE,1);
101
        for (j = TOILETWATER; j < (TOILETWATER+4); j++) tloadtile(j,1);
102
        break;
103
    case RUBBERCAN__STATIC:
104
        maxc = 2;
105
        break;
106
    case TOILETWATER__STATIC:
107
        maxc = 4;
108
        break;
109
    case FEMPIC1__STATIC:
110
        maxc = 44;
111
        break;
112
    case LIZTROOP__STATIC:
113
    case LIZTROOPRUNNING__STATIC:
114
    case LIZTROOPSHOOT__STATIC:
115
    case LIZTROOPJETPACK__STATIC:
116
    case LIZTROOPONTOILET__STATIC:
117
    case LIZTROOPDUCKING__STATIC:
118
        for (j = LIZTROOP; j < (LIZTROOP+72); j++) tloadtile(j,1);
1229 terminx 119
        for (j=HEADJIB1; j<LEGJIB1+3; j++) tloadtile(j,1);
337 terminx 120
        maxc = 0;
121
        break;
122
    case WOODENHORSE__STATIC:
123
        maxc = 5;
124
        for (j = HORSEONSIDE; j < (HORSEONSIDE+4); j++) tloadtile(j,1);
125
        break;
126
    case NEWBEAST__STATIC:
127
    case NEWBEASTSTAYPUT__STATIC:
128
        maxc = 90;
129
        break;
130
    case BOSS1__STATIC:
131
    case BOSS2__STATIC:
132
    case BOSS3__STATIC:
133
        maxc = 30;
134
        break;
135
    case OCTABRAIN__STATIC:
136
    case OCTABRAINSTAYPUT__STATIC:
137
    case COMMANDER__STATIC:
138
    case COMMANDERSTAYPUT__STATIC:
139
        maxc = 38;
140
        break;
141
    case RECON__STATIC:
142
        maxc = 13;
143
        break;
144
    case PIGCOP__STATIC:
145
    case PIGCOPDIVE__STATIC:
146
        maxc = 61;
147
        break;
148
    case SHARK__STATIC:
149
        maxc = 30;
150
        break;
151
    case LIZMAN__STATIC:
152
    case LIZMANSPITTING__STATIC:
153
    case LIZMANFEEDING__STATIC:
154
    case LIZMANJUMP__STATIC:
1229 terminx 155
        for (j=LIZMANHEAD1; j<LIZMANLEG1+3; j++) tloadtile(j,1);
337 terminx 156
        maxc = 80;
157
        break;
158
    case APLAYER__STATIC:
159
        maxc = 0;
1587 terminx 160
        if ((g_netServer || ud.multimode > 1))
337 terminx 161
        {
5 Plagman 162
            maxc = 5;
1229 terminx 163
            for (j = 1420; j < 1420+106; j++) tloadtile(j,1);
337 terminx 164
        }
165
        break;
166
    case ATOMICHEALTH__STATIC:
167
        maxc = 14;
168
        break;
169
    case DRONE__STATIC:
170
        maxc = 10;
171
        break;
172
    case EXPLODINGBARREL__STATIC:
173
    case SEENINE__STATIC:
174
    case OOZFILTER__STATIC:
175
        maxc = 3;
176
        break;
177
    case NUKEBARREL__STATIC:
178
    case CAMERA1__STATIC:
179
        maxc = 5;
180
        break;
181
        // caching of HUD sprites for weapons that may be in the level
182
    case CHAINGUNSPRITE__STATIC:
183
        for (j=CHAINGUN; j<=CHAINGUN+7; j++) tloadtile(j,1);
184
        break;
185
    case RPGSPRITE__STATIC:
186
        for (j=RPGGUN; j<=RPGGUN+2; j++) tloadtile(j,1);
187
        break;
188
    case FREEZESPRITE__STATIC:
189
        for (j=FREEZE; j<=FREEZE+5; j++) tloadtile(j,1);
190
        break;
191
    case GROWSPRITEICON__STATIC:
192
    case SHRINKERSPRITE__STATIC:
193
        for (j=SHRINKER-2; j<=SHRINKER+5; j++) tloadtile(j,1);
194
        break;
195
    case HBOMBAMMO__STATIC:
196
    case HEAVYHBOMB__STATIC:
197
        for (j=HANDREMOTE; j<=HANDREMOTE+5; j++) tloadtile(j,1);
198
        break;
199
    case TRIPBOMBSPRITE__STATIC:
200
        for (j=HANDHOLDINGLASER; j<=HANDHOLDINGLASER+4; j++) tloadtile(j,1);
201
        break;
202
    case SHOTGUNSPRITE__STATIC:
203
        tloadtile(SHOTGUNSHELL,1);
204
        for (j=SHOTGUN; j<=SHOTGUN+6; j++) tloadtile(j,1);
205
        break;
206
    case DEVISTATORSPRITE__STATIC:
207
        for (j=DEVISTATOR; j<=DEVISTATOR+1; j++) tloadtile(j,1);
208
        break;
5 Plagman 209
 
210
    }
211
 
331 terminx 212
    for (j = PN; j < (PN+maxc); j++) tloadtile(j,1);
5 Plagman 213
}
214
 
1143 terminx 215
static void G_PrecacheSprites(void)
5 Plagman 216
{
1205 terminx 217
    int32_t i,j;
5 Plagman 218
 
1229 terminx 219
    for (i=0; i<MAXTILES; i++)
5 Plagman 220
    {
4351 helixhorne 221
        if (g_tile[i].flags & SFLAG_PROJECTILE)
5 Plagman 222
            tloadtile(i,1);
3102 terminx 223
 
4351 helixhorne 224
        if (A_CheckSpriteTileFlags(i, SFLAG_CACHE))
3169 helixhorne 225
            for (j = i; j <= g_tile[i].cacherange; j++)
5 Plagman 226
                tloadtile(j,1);
227
    }
228
    tloadtile(BOTTOMSTATUSBAR,1);
1587 terminx 229
    if ((g_netServer || ud.multimode > 1))
5 Plagman 230
        tloadtile(FRAGBAR,1);
231
 
232
    tloadtile(VIEWSCREEN,1);
233
 
1229 terminx 234
    for (i=STARTALPHANUM; i<ENDALPHANUM+1; i++) tloadtile(i,1);
3606 hendricks2 235
    for (i=BIGALPHANUM-11; i<BIGALPHANUM+82; i++) tloadtile(i,1);
236
    for (i=MINIFONT; i<MINIFONT+93; i++) tloadtile(i,1);
5 Plagman 237
 
1229 terminx 238
    for (i=FOOTPRINTS; i<FOOTPRINTS+3; i++) tloadtile(i,1);
5 Plagman 239
 
333 terminx 240
    for (i = BURNING; i < BURNING+14; i++) tloadtile(i,1);
241
    for (i = BURNING2; i < BURNING2+14; i++) tloadtile(i,1);
5 Plagman 242
 
333 terminx 243
    for (i = CRACKKNUCKLES; i < CRACKKNUCKLES+4; i++) tloadtile(i,1);
5 Plagman 244
 
333 terminx 245
    for (i = FIRSTGUN; i < FIRSTGUN+3 ; i++) tloadtile(i,1);
246
    for (i = FIRSTGUNRELOAD; i < FIRSTGUNRELOAD+8 ; i++) tloadtile(i,1);
5 Plagman 247
 
333 terminx 248
    for (i = EXPLOSION2; i < EXPLOSION2+21 ; i++) tloadtile(i,1);
5 Plagman 249
 
511 terminx 250
    for (i = COOLEXPLOSION1; i < COOLEXPLOSION1+21 ; i++) tloadtile(i,1);
251
 
5 Plagman 252
    tloadtile(BULLETHOLE,1);
511 terminx 253
    tloadtile(BLOODPOOL,1);
254
    for (i = TRANSPORTERBEAM; i < (TRANSPORTERBEAM+6); i++) tloadtile(i,1);
522 terminx 255
 
333 terminx 256
    for (i = SMALLSMOKE; i < (SMALLSMOKE+4); i++) tloadtile(i,1);
710 terminx 257
    for (i = SHOTSPARK1; i < (SHOTSPARK1+4); i++) tloadtile(i,1);
5 Plagman 258
 
511 terminx 259
    for (i = BLOOD; i < (BLOOD+4); i++) tloadtile(i,1);
333 terminx 260
    for (i = JIBS1; i < (JIBS5+5); i++) tloadtile(i,1);
710 terminx 261
    for (i = JIBS6; i < (JIBS6+8); i++) tloadtile(i,1);
262
 
511 terminx 263
    for (i = SCRAP1; i < (SCRAP1+29); i++) tloadtile(i,1);
5 Plagman 264
 
511 terminx 265
    tloadtile(FIRELASER,1);
1754 helixhorne 266
    for (i=TRANSPORTERSTAR; i<TRANSPORTERSTAR+6; i++) tloadtile(i,1);
511 terminx 267
    for (i=FORCERIPPLE; i<(FORCERIPPLE+9); i++) tloadtile(i,1);
268
 
269
    for (i=MENUSCREEN; i<DUKECAR; i++) tloadtile(i,1);
522 terminx 270
 
5 Plagman 271
    for (i=RPG; i<RPG+7; i++) tloadtile(i,1);
272
    for (i=FREEZEBLAST; i<FREEZEBLAST+3; i++) tloadtile(i,1);
273
    for (i=SHRINKSPARK; i<SHRINKSPARK+4; i++) tloadtile(i,1);
274
    for (i=GROWSPARK; i<GROWSPARK+4; i++) tloadtile(i,1);
275
    for (i=SHRINKEREXPLOSION; i<SHRINKEREXPLOSION+4; i++) tloadtile(i,1);
1610 helixhorne 276
    for (i=MORTER; i<MORTER+4; i++) tloadtile(i,1);
1010 terminx 277
    for (i=0; i<=60; i++) tloadtile(i,1);
5 Plagman 278
}
279
 
1143 terminx 280
// FIXME: this function is a piece of shit, needs specific sounds listed
1229 terminx 281
static int32_t G_CacheSound(uint32_t num)
5 Plagman 282
{
1205 terminx 283
    int16_t fp = -1;
284
    int32_t   l;
5 Plagman 285
 
4986 terminx 286
    if (num >= MAXSOUNDS || !ud.config.SoundToggle) return 0;
5 Plagman 287
 
4948 hendricks2 288
    if (EDUKE32_PREDICT_FALSE(!g_sounds[num].filename)) return 0;
289
 
290
#if defined HAVE_FLAC || defined HAVE_VORBIS
291
    fp = S_UpgradeFormat(g_sounds[num].filename, g_loadFromGroupOnly);
1007 hnt_ts 292
    if (fp == -1)
4948 hendricks2 293
#endif
294
        fp = kopen4loadfrommod(g_sounds[num].filename,g_loadFromGroupOnly);
295
    if (fp == -1)
1007 hnt_ts 296
    {
1143 terminx 297
//        OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n",g_sounds[num].filename,num);
1007 hnt_ts 298
        return 0;
299
    }
5 Plagman 300
 
333 terminx 301
    l = kfilelength(fp);
580 terminx 302
    g_sounds[num].soundsiz = l;
5 Plagman 303
 
333 terminx 304
    if ((ud.level_number == 0 && ud.volume_number == 0 && (num == 189 || num == 232 || num == 99 || num == 233 || num == 17)) ||
305
            (l < 12288))
5 Plagman 306
    {
1457 terminx 307
        g_soundlocks[num] = 199;
308
        allocache((intptr_t *)&g_sounds[num].ptr,l,(char *)&g_soundlocks[num]);
580 terminx 309
        if (g_sounds[num].ptr != NULL)
310
            kread(fp, g_sounds[num].ptr , l);
5 Plagman 311
    }
333 terminx 312
    kclose(fp);
5 Plagman 313
    return 1;
314
}
315
 
1143 terminx 316
static void G_PrecacheSounds(void)
5 Plagman 317
{
4986 terminx 318
    int32_t i, j = 0;
5 Plagman 319
 
1657 terminx 320
    for (i=MAXSOUNDS-1; i>=0; i--)
580 terminx 321
        if (g_sounds[i].ptr == 0)
5 Plagman 322
        {
323
            j++;
333 terminx 324
            if ((j&7) == 0)
2996 helixhorne 325
                G_HandleAsync();
326
 
1229 terminx 327
            G_CacheSound(i);
5 Plagman 328
        }
329
}
330
 
2796 helixhorne 331
static void G_DoLoadScreen(const char *statustext, int32_t percent)
398 terminx 332
{
1205 terminx 333
    int32_t i=0,j;
398 terminx 334
 
335
    if (ud.recstat != 2)
336
    {
4745 terminx 337
        j = VM_OnEventWithReturn(EVENT_GETLOADTILE, -1, myconnectindex, LOADSCREEN);
1625 terminx 338
 
2223 helixhorne 339
        //g_player[myconnectindex].ps->palette = palette;
3025 helixhorne 340
        P_SetGamePalette(g_player[myconnectindex].ps, BASEPAL, 1);    // JBF 20040308
2223 helixhorne 341
 
398 terminx 342
        if (!statustext)
343
        {
344
            i = ud.screen_size;
345
            ud.screen_size = 0;
1143 terminx 346
            G_UpdateScreenArea();
2924 helixhorne 347
            clearallviews(0L);
398 terminx 348
        }
349
 
1969 helixhorne 350
        if ((uint32_t)j < 2*MAXTILES)
398 terminx 351
        {
2924 helixhorne 352
            clearallviews(0);
2912 helixhorne 353
 
2308 helixhorne 354
            rotatesprite_fs(320<<15,200<<15,65536L,0, j > MAXTILES-1?j-MAXTILES:j,0,0,
4982 terminx 355
                            2+8+64+BGSTRETCH);
1969 helixhorne 356
        }
357
        else
358
        {
398 terminx 359
            nextpage();
360
            return;
361
        }
1969 helixhorne 362
 
398 terminx 363
        if (boardfilename[0] != 0 && ud.level_number == 7 && ud.volume_number == 0)
364
        {
2540 hendricks2 365
            menutext(160,90,0,0,"Loading User Map");
398 terminx 366
            gametextpal(160,90+10,boardfilename,14,2);
367
        }
368
        else
369
        {
2540 hendricks2 370
            menutext(160,90,0,0,"Loading");
1143 terminx 371
            if (MapInfo[(ud.volume_number*MAXLEVELS) + ud.level_number].name != NULL)
372
                menutext(160,90+16+8,0,0,MapInfo[(ud.volume_number*MAXLEVELS) + ud.level_number].name);
398 terminx 373
        }
374
 
375
        if (statustext) gametext(160,180,statustext,0,2+8+16);
1625 terminx 376
 
377
        if (percent != -1)
1187 terminx 378
        {
1327 terminx 379
            int32_t ii = scale(scale(xdim-1,288,320),percent,100);
380
            rotatesprite(31<<16,145<<16,65536,0,929,15,0,2+8+16,0,0,ii,ydim-1);
381
            rotatesprite(159<<16,145<<16,65536,0,929,15,0,2+8+16,0,0,ii,ydim-1);
382
            rotatesprite(30<<16,144<<16,65536,0,929,0,0,2+8+16,0,0,ii,ydim-1);
383
            rotatesprite(158<<16,144<<16,65536,0,929,0,0,2+8+16,0,0,ii,ydim-1);
1187 terminx 384
        }
1625 terminx 385
 
4900 terminx 386
        VM_OnEventWithReturn(EVENT_DISPLAYLOADINGSCREEN, g_player[screenpeek].ps->i, screenpeek, percent);
398 terminx 387
        nextpage();
388
 
389
        if (!statustext)
390
        {
391
            KB_FlushKeyboardQueue();
392
            ud.screen_size = i;
393
        }
394
    }
395
    else
396
    {
397
        if (!statustext)
398
        {
2924 helixhorne 399
            clearallviews(0L);
564 terminx 400
            //g_player[myconnectindex].ps->palette = palette;
1143 terminx 401
            //G_FadePalette(0,0,0,0);
1772 plagman 402
            P_SetGamePalette(g_player[myconnectindex].ps, BASEPAL, 0);    // JBF 20040308
398 terminx 403
        }
1215 terminx 404
        /*Gv_SetVar(g_iReturnVarID,LOADSCREEN, -1, -1);*/
1969 helixhorne 405
 
4745 terminx 406
        j = VM_OnEventWithReturn(EVENT_GETLOADTILE, -1, myconnectindex, LOADSCREEN);
1969 helixhorne 407
 
408
        if ((uint32_t)j < 2*MAXTILES)
398 terminx 409
        {
2308 helixhorne 410
            rotatesprite_fs(320<<15,200<<15,65536L, 0,j > MAXTILES-1?j-MAXTILES:j,0,0,
4982 terminx 411
                            2+8+64+BGSTRETCH);
1969 helixhorne 412
        }
413
        else
414
        {
398 terminx 415
            nextpage();
416
            return;
417
        }
1969 helixhorne 418
 
2540 hendricks2 419
        menutext(160,105,0,0,"Loading...");
398 terminx 420
        if (statustext) gametext(160,180,statustext,0,2+8+16);
4900 terminx 421
        VM_OnEventWithReturn(EVENT_DISPLAYLOADINGSCREEN, g_player[screenpeek].ps->i, screenpeek, percent);
398 terminx 422
        nextpage();
423
    }
424
}
425
 
1205 terminx 426
extern void G_SetCrosshairColor(int32_t r, int32_t g, int32_t b);
1143 terminx 427
extern palette_t CrosshairColors;
938 terminx 428
 
1143 terminx 429
void G_CacheMapData(void)
5 Plagman 430
{
1205 terminx 431
    int32_t i,j,pc=0;
432
    int32_t tc;
433
    uint32_t starttime, endtime;
5 Plagman 434
 
331 terminx 435
    if (ud.recstat == 2)
29 terminx 436
        return;
437
 
1468 terminx 438
    S_PauseMusic(1);
4588 helixhorne 439
    if (MapInfo[MUS_LOADING].musicfn)
693 terminx 440
    {
1468 terminx 441
        S_StopMusic();
4948 hendricks2 442
        S_PlayMusic(MapInfo[MUS_LOADING].musicfn);
693 terminx 443
    }
645 terminx 444
 
29 terminx 445
    starttime = getticks();
446
 
1143 terminx 447
    G_PrecacheSounds();
448
    G_PrecacheSprites();
5 Plagman 449
 
1229 terminx 450
    for (i=0; i<numwalls; i++)
5 Plagman 451
    {
452
        tloadtile(wall[i].picnum, 0);
453
 
335 terminx 454
        if (wall[i].overpicnum >= 0)
455
        {
5 Plagman 456
            tloadtile(wall[i].overpicnum, 0);
457
        }
458
    }
459
 
1229 terminx 460
    for (i=0; i<numsectors; i++)
5 Plagman 461
    {
333 terminx 462
        tloadtile(sector[i].floorpicnum, 0);
463
        tloadtile(sector[i].ceilingpicnum, 0);
1540 terminx 464
        if (sector[i].ceilingpicnum == LA)  // JBF 20040509: if( waloff[sector[i].ceilingpicnum] == LA) WTF?!?!?!?
5 Plagman 465
        {
466
            tloadtile(LA+1, 0);
467
            tloadtile(LA+2, 0);
468
        }
469
 
4291 helixhorne 470
        for (SPRITES_OF_SECT(i, j))
331 terminx 471
            if (sprite[j].xrepeat != 0 && sprite[j].yrepeat != 0 && (sprite[j].cstat&32768) == 0)
1143 terminx 472
                G_CacheSpriteNum(j);
5 Plagman 473
    }
474
 
29 terminx 475
    tc = totalclock;
5 Plagman 476
    j = 0;
477
 
1229 terminx 478
    for (i=0; i<MAXTILES; i++)
335 terminx 479
    {
480
        if (!(i&7) && !gotpic[i>>3])
481
        {
29 terminx 482
            i+=7;
483
            continue;
484
        }
335 terminx 485
        if (gotpic[i>>3] & pow2char[i&7])
486
        {
29 terminx 487
            if (waloff[i] == 0)
1205 terminx 488
                loadtile((int16_t)i);
5 Plagman 489
 
1820 terminx 490
#ifdef USE_OPENGL
1754 helixhorne 491
// PRECACHE
3115 terminx 492
            if (ud.config.useprecache && bpp > 8)
335 terminx 493
            {
1610 helixhorne 494
                int32_t k,type;
855 terminx 495
 
1610 helixhorne 496
                for (type=0; type<=1; type++)
497
                    if (precachehightile[type][i>>3] & pow2char[i&7])
498
                    {
2045 helixhorne 499
                        k = 0;
1610 helixhorne 500
                        for (k=0; k<MAXPALOOKUPS-RESERVEDPALS && !KB_KeyPressed(sc_Space); k++)
2045 helixhorne 501
                        {
4226 helixhorne 502
                            // this is the CROSSHAIR_PAL, see comment in game.c
2045 helixhorne 503
                            if (k == MAXPALOOKUPS-RESERVEDPALS-1)
504
                                break;
3467 helixhorne 505
#ifdef POLYMER
3784 terminx 506
                            if (getrendermode() != REND_POLYMER || !polymer_havehighpalookup(0, k))
3467 helixhorne 507
#endif
2045 helixhorne 508
                                polymost_precache(i,k,type);
509
                        }
510
 
1610 helixhorne 511
                        if (r_detailmapping && !KB_KeyPressed(sc_Space))
512
                            polymost_precache(i,DETAILPAL,type);
513
                        if (r_glowmapping && !KB_KeyPressed(sc_Space))
514
                            polymost_precache(i,GLOWPAL,type);
1660 terminx 515
#ifdef POLYMER
3784 terminx 516
                        if (getrendermode() == REND_POLYMER)
1610 helixhorne 517
                        {
518
                            if (pr_specularmapping && !KB_KeyPressed(sc_Space))
519
                                polymost_precache(i,SPECULARPAL,type);
520
                            if (pr_normalmapping && !KB_KeyPressed(sc_Space))
521
                                polymost_precache(i,NORMALPAL,type);
522
                        }
1660 terminx 523
#endif
1610 helixhorne 524
                    }
29 terminx 525
            }
259 terminx 526
#endif
29 terminx 527
            j++;
528
            pc++;
335 terminx 529
        }
530
        else continue;
5 Plagman 531
 
1473 terminx 532
        MUSIC_Update();
533
 
335 terminx 534
        if ((j&7) == 0)
2996 helixhorne 535
            G_HandleAsync();
536
 
3115 terminx 537
        if (bpp > 8 && totalclock - tc > TICRATE/4)
335 terminx 538
        {
1217 terminx 539
            /*Bsprintf(tempbuf,"%d resources remaining\n",g_precacheCount-pc+1);*/
4658 terminx 540
            tc = min(100, tabledivide32_noinline(100 * pc, g_precacheCount));
1217 terminx 541
            Bsprintf(tempbuf,"Loaded %d%% (%d/%d textures)\n",tc,pc,g_precacheCount);
542
            G_DoLoadScreen(tempbuf, tc);
29 terminx 543
            tc = totalclock;
544
        }
5 Plagman 545
    }
546
 
4257 helixhorne 547
    Bmemset(gotpic, 0, sizeof(gotpic));
5 Plagman 548
 
29 terminx 549
    endtime = getticks();
550
    OSD_Printf("Cache time: %dms\n", endtime-starttime);
5 Plagman 551
}
552
 
1143 terminx 553
void G_UpdateScreenArea(void)
5 Plagman 554
{
3823 helixhorne 555
    if (!in3dmode())
3402 helixhorne 556
        return;
5 Plagman 557
 
2697 helixhorne 558
    ud.screen_size = clamp(ud.screen_size, 0, 64);
3402 helixhorne 559
    if (ud.screen_size == 0)
560
        flushperms();
5 Plagman 561
 
3402 helixhorne 562
    {
563
        const int32_t ss = max(ud.screen_size-8,0);
5 Plagman 564
 
3402 helixhorne 565
        const int32_t x1 = scale(ss,xdim,160);
3421 helixhorne 566
        int32_t x2 = xdim-x1;
5 Plagman 567
 
3402 helixhorne 568
        int32_t y1 = ss;
569
        int32_t y2 = 200;
5 Plagman 570
 
3402 helixhorne 571
        if (ud.screen_size > 0 && (GametypeFlags[ud.coop]&GAMETYPE_FRAGBAR) && (g_netServer || ud.multimode > 1))
572
        {
573
            int32_t i, j = 0;
5 Plagman 574
 
3402 helixhorne 575
            for (TRAVERSE_CONNECT(i))
576
                if (i > j) j = i;
5 Plagman 577
 
3402 helixhorne 578
            if (j >= 1) y1 += 8;
579
            if (j >= 4) y1 += 8;
580
            if (j >= 8) y1 += 8;
581
            if (j >= 12) y1 += 8;
582
        }
5 Plagman 583
 
3402 helixhorne 584
        if (ud.screen_size >= 8 && ud.statusbarmode==0)
4623 terminx 585
            y2 -= (ss+scale(tilesiz[BOTTOMSTATUSBAR].y,ud.statusbarscale,100));
5 Plagman 586
 
3402 helixhorne 587
        y1 = scale(y1,ydim,200);
3421 helixhorne 588
        y2 = scale(y2,ydim,200)+(getrendermode() != REND_CLASSIC);
3402 helixhorne 589
 
3421 helixhorne 590
        if (g_halveScreenArea)
591
        {
592
            int32_t ourxdimen=x2-x1, ourydimen=y2-y1;
593
 
594
            g_halfScreen.x1 = x1;
595
            g_halfScreen.y1 = y1;
596
            g_halfScreen.xdimen = (ourxdimen>>1);
597
            g_halfScreen.ydimen = (ourydimen>>1);
598
 
599
            x2 = x1 + (ourxdimen>>1);
600
            y2 = y1 + (ourydimen>>1);
601
        }
602
 
603
        setview(x1,y1,x2-1,y2-1);
3402 helixhorne 604
    }
605
 
1143 terminx 606
    G_GetCrosshairColor();
607
    G_SetCrosshairColor(CrosshairColors.r, CrosshairColors.g, CrosshairColors.b);
940 terminx 608
 
5 Plagman 609
    pub = NUMPAGES;
610
    pus = NUMPAGES;
611
}
612
 
1205 terminx 613
void P_RandomSpawnPoint(int32_t snum)
5 Plagman 614
{
1143 terminx 615
    DukePlayer_t *p = g_player[snum].ps;
1205 terminx 616
    int32_t i=snum,j,k;
617
    uint32_t dist,pdist = -1;
5 Plagman 618
 
1587 terminx 619
    if ((g_netServer || ud.multimode > 1) && !(GametypeFlags[ud.coop] & GAMETYPE_FIXEDRESPAWN))
268 terminx 620
    {
1143 terminx 621
        i = krand()%g_numPlayerSprites;
622
        if (GametypeFlags[ud.coop] & GAMETYPE_TDMSPAWN)
268 terminx 623
        {
1229 terminx 624
            for (j=0; j<ud.multimode; j++)
268 terminx 625
            {
969 terminx 626
                if (j != snum && g_player[j].ps->team == p->team && sprite[g_player[j].ps->i].extra > 0)
268 terminx 627
                {
1229 terminx 628
                    for (k=0; k<g_numPlayerSprites; k++)
268 terminx 629
                    {
4703 terminx 630
                        dist = FindDistance2D(g_player[j].ps->pos.x-g_playerSpawnPoints[k].pos.x,g_player[j].ps->pos.y-g_playerSpawnPoints[k].pos.y);
331 terminx 631
                        if (dist < pdist)
268 terminx 632
                            i = k, pdist = dist;
633
                    }
634
                    break;
635
                }
636
            }
335 terminx 637
        }
268 terminx 638
    }
5 Plagman 639
 
4703 terminx 640
    p->bobpos.x = p->opos.x = p->pos.x = g_playerSpawnPoints[i].pos.x;
641
    p->bobpos.y = p->opos.y = p->pos.y = g_playerSpawnPoints[i].pos.y;
642
    p->opos.z = p->pos.z = g_playerSpawnPoints[i].pos.z;
643
    p->ang = g_playerSpawnPoints[i].ang;
644
    p->cursectnum = g_playerSpawnPoints[i].sect;
1560 terminx 645
    sprite[p->i].cstat = 1+256;
5 Plagman 646
}
647
 
3893 helixhorne 648
static inline void P_ResetTintFade(DukePlayer_t *ps)
649
{
650
    ps->pals.f = 0;
651
#ifdef LUNATIC
652
    ps->palsfadeprio = 0;
653
#endif
654
}
655
 
1560 terminx 656
void P_ResetPlayer(int32_t snum)
657
{
658
    vec3_t tmpvect;
2781 helixhorne 659
    DukePlayer_t *const pl = g_player[snum].ps;
660
    spritetype *const sp = &sprite[pl->i];
1560 terminx 661
 
2781 helixhorne 662
    tmpvect.x = pl->pos.x;
663
    tmpvect.y = pl->pos.y;
664
    tmpvect.z = pl->pos.z+PHEIGHT;
1560 terminx 665
    P_RandomSpawnPoint(snum);
4703 terminx 666
    sp->x = actor[pl->i].bpos.x = pl->bobpos.x = pl->opos.x = pl->pos.x;
667
    sp->y = actor[pl->i].bpos.y = pl->bobpos.y = pl->opos.y = pl->pos.y;
3314 helixhorne 668
    sp->z = actor[pl->i].bpos.y = pl->opos.z =pl->pos.z;
2781 helixhorne 669
    updatesector(pl->pos.x,pl->pos.y,&pl->cursectnum);
670
    setsprite(pl->i,&tmpvect);
1560 terminx 671
    sp->cstat = 257;
672
 
673
    sp->shade = -12;
674
    sp->clipdist = 64;
675
    sp->xrepeat = 42;
676
    sp->yrepeat = 36;
2781 helixhorne 677
    sp->owner = pl->i;
1560 terminx 678
    sp->xoffset = 0;
2781 helixhorne 679
    sp->pal = pl->palookup;
1560 terminx 680
 
2781 helixhorne 681
    pl->last_extra = sp->extra = pl->max_player_health;
682
    pl->wantweaponfire = -1;
683
    pl->horiz = 100;
684
    pl->on_crane = -1;
685
    pl->frag_ps = snum;
686
    pl->horizoff = 0;
687
    pl->opyoff = 0;
688
    pl->wackedbyactor = -1;
689
    pl->inv_amount[GET_SHIELD] = g_startArmorAmount;
690
    pl->dead_flag = 0;
691
    pl->footprintcount = 0;
692
    pl->weapreccnt = 0;
693
    pl->fta = 0;
694
    pl->ftq = 0;
695
    pl->vel.x = pl->vel.y = 0;
696
    pl->rotscrnang = 0;
697
    pl->runspeed = g_playerFriction;
698
    pl->falling_counter = 0;
1560 terminx 699
 
3893 helixhorne 700
    P_ResetTintFade(pl);
701
 
2781 helixhorne 702
    actor[pl->i].extra = -1;
703
    actor[pl->i].owner = pl->i;
1560 terminx 704
 
2781 helixhorne 705
    actor[pl->i].cgg = 0;
706
    actor[pl->i].movflag = 0;
707
    actor[pl->i].tempang = 0;
708
    actor[pl->i].actorstayput = -1;
709
    actor[pl->i].dispicnum = 0;
710
    actor[pl->i].owner = pl->i;
1560 terminx 711
 
2781 helixhorne 712
    actor[pl->i].t_data[4] = 0;
1560 terminx 713
 
714
    P_ResetInventory(snum);
715
    P_ResetWeapons(snum);
716
 
2781 helixhorne 717
    pl->reloading = 0;
1560 terminx 718
 
2781 helixhorne 719
    pl->movement_lock = 0;
1560 terminx 720
 
4745 terminx 721
    VM_OnEvent(EVENT_RESETPLAYER, pl->i, snum);
1560 terminx 722
}
723
 
1552 terminx 724
void P_ResetStatus(int32_t snum)
5 Plagman 725
{
1143 terminx 726
    DukePlayer_t *p = g_player[snum].ps;
5 Plagman 727
 
728
    ud.show_help        = 0;
729
    ud.showallmap       = 0;
730
    p->dead_flag        = 0;
731
    p->wackedbyactor    = -1;
732
    p->falling_counter  = 0;
733
    p->quick_kick       = 0;
734
    p->subweapon        = 0;
735
    p->last_full_weapon = 0;
736
    p->ftq              = 0;
737
    p->fta              = 0;
738
    p->tipincs          = 0;
739
    p->buttonpalette    = 0;
740
    p->actorsqu         =-1;
741
    p->invdisptime      = 0;
742
    p->refresh_inventory= 0;
743
    p->last_pissed_time = 0;
744
    p->holster_weapon   = 0;
745
    p->pycount          = 0;
746
    p->pyoff            = 0;
747
    p->opyoff           = 0;
748
    p->loogcnt          = 0;
749
    p->angvel           = 0;
750
    p->weapon_sway      = 0;
751
    p->extra_extra8     = 0;
752
    p->show_empty_weapon= 0;
753
    p->dummyplayersprite=-1;
754
    p->crack_time       = 0;
755
    p->hbomb_hold_delay = 0;
756
    p->transporter_hold = 0;
757
    p->wantweaponfire  = -1;
758
    p->hurt_delay       = 0;
759
    p->footprintcount   = 0;
760
    p->footprintpal     = 0;
761
    p->footprintshade   = 0;
762
    p->jumping_toggle   = 0;
763
    p->ohoriz = p->horiz= 140;
764
    p->horizoff         = 0;
765
    p->bobcounter       = 0;
766
    p->on_ground        = 0;
767
    p->player_par       = 0;
768
    p->return_to_center = 9;
1346 terminx 769
    p->airleft          = 15*GAMETICSPERSEC;
5 Plagman 770
    p->rapid_fire_hold  = 0;
771
    p->toggle_key_flag  = 0;
772
    p->access_spritenum = -1;
1587 terminx 773
    if ((g_netServer || ud.multimode > 1) && (GametypeFlags[ud.coop] & GAMETYPE_ACCESSATSTART))
5 Plagman 774
        p->got_access = 7;
775
    else p->got_access      = 0;
776
    p->random_club_frame= 0;
777
    pus = 1;
778
    p->on_warping_sector = 0;
779
    p->spritebridge      = 0;
780
    p->sbs          = 0;
1772 plagman 781
    p->palette = BASEPAL;
5 Plagman 782
 
1572 terminx 783
    if (p->inv_amount[GET_STEROIDS] < 400)
5 Plagman 784
    {
1572 terminx 785
        p->inv_amount[GET_STEROIDS] = 0;
3115 terminx 786
        p->inven_icon = ICON_NONE;
5 Plagman 787
    }
788
    p->heat_on =            0;
789
    p->jetpack_on =         0;
790
    p->holoduke_on =       -1;
791
 
792
    p->look_ang          = 512 - ((ud.level_number&1)<<10);
793
 
794
    p->rotscrnang        = 0;
795
    p->orotscrnang       = 1;   // JBF 20031220
796
    p->newowner          =-1;
797
    p->jumping_counter   = 0;
798
    p->hard_landing      = 0;
1802 terminx 799
    p->vel.x             = 0;
800
    p->vel.y             = 0;
801
    p->vel.z             = 0;
4703 terminx 802
    p->fric.x        = 0;
803
    p->fric.y        = 0;
5 Plagman 804
    p->somethingonplayer =-1;
805
    p->one_eighty_count  = 0;
806
    p->cheat_phase       = 0;
807
 
808
    p->on_crane          = -1;
809
 
3328 helixhorne 810
    if ((PWEAPON(snum, p->curr_weapon, WorksLike) == PISTOL_WEAPON) &&
811
            (PWEAPON(snum, p->curr_weapon, Reload) > PWEAPON(snum, p->curr_weapon, TotalTime)))
812
        p->kickback_pic  = PWEAPON(snum, p->curr_weapon, TotalTime);
5 Plagman 813
    else p->kickback_pic = 0;
814
 
3903 terminx 815
    p->weapon_pos        = WEAPON_POS_START;
5 Plagman 816
    p->walking_snd_toggle= 0;
817
    p->weapon_ang        = 0;
818
 
819
    p->knuckle_incs      = 1;
820
    p->fist_incs = 0;
821
    p->knee_incs         = 0;
822
    p->jetpack_on        = 0;
823
    p->reloading        = 0;
824
 
172 terminx 825
    p->movement_lock     = 0;
5 Plagman 826
 
1582 terminx 827
    p->frag_ps          = snum;
828
 
1143 terminx 829
    P_UpdateScreenPal(p);
4745 terminx 830
    VM_OnEvent(EVENT_RESETPLAYER, p->i, snum);
5 Plagman 831
}
832
 
1205 terminx 833
void P_ResetWeapons(int32_t snum)
5 Plagman 834
{
1205 terminx 835
    int32_t weapon;
1143 terminx 836
    DukePlayer_t *p = g_player[snum].ps;
5 Plagman 837
 
333 terminx 838
    for (weapon = PISTOL_WEAPON; weapon < MAX_WEAPONS; weapon++)
5 Plagman 839
        p->ammo_amount[weapon] = 0;
840
 
3903 terminx 841
    p->weapon_pos = WEAPON_POS_START;
5 Plagman 842
    p->curr_weapon = PISTOL_WEAPON;
3328 helixhorne 843
    p->kickback_pic = PWEAPON(snum, p->curr_weapon, TotalTime);
1625 terminx 844
    p->gotweapon = ((1<<PISTOL_WEAPON) | (1<<KNEE_WEAPON) | (1<<HANDREMOTE_WEAPON));
1444 terminx 845
    p->ammo_amount[PISTOL_WEAPON] = min(p->max_ammo_amount[PISTOL_WEAPON], 48);
5 Plagman 846
    p->last_weapon = -1;
847
 
848
    p->show_empty_weapon= 0;
849
    p->last_pissed_time = 0;
850
    p->holster_weapon = 0;
4745 terminx 851
    VM_OnEvent(EVENT_RESETWEAPONS, p->i, snum);
5 Plagman 852
}
853
 
1205 terminx 854
void P_ResetInventory(int32_t snum)
5 Plagman 855
{
1143 terminx 856
    DukePlayer_t *p = g_player[snum].ps;
5 Plagman 857
 
1572 terminx 858
    Bmemset(p->inv_amount, 0, sizeof(p->inv_amount));
859
 
335 terminx 860
    p->scuba_on =           0;
861
    p->heat_on = 0;
862
    p->jetpack_on =         0;
5 Plagman 863
    p->holoduke_on = -1;
1572 terminx 864
    p->inv_amount[GET_SHIELD] =      g_startArmorAmount;
3115 terminx 865
    p->inven_icon = ICON_NONE;
4745 terminx 866
    VM_OnEvent(EVENT_RESETINVENTORY, p->i, snum);
5 Plagman 867
}
868
 
1205 terminx 869
static void resetprestat(int32_t snum,int32_t g)
5 Plagman 870
{
1143 terminx 871
    DukePlayer_t *p = g_player[snum].ps;
1205 terminx 872
    int32_t i;
5 Plagman 873
 
1143 terminx 874
    g_spriteDeleteQueuePos = 0;
1229 terminx 875
    for (i=0; i<g_spriteDeleteQueueSize; i++) SpriteDeletionQueue[i] = -1;
5 Plagman 876
 
877
    p->hbomb_on          = 0;
878
    p->cheat_phase       = 0;
879
    p->toggle_key_flag   = 0;
880
    p->secret_rooms      = 0;
881
    p->max_secret_rooms  = 0;
882
    p->actors_killed     = 0;
883
    p->max_actors_killed = 0;
3903 terminx 884
    p->lastrandomspot    = 0;
885
    p->weapon_pos = WEAPON_POS_START;
1642 terminx 886
 
3893 helixhorne 887
    P_ResetTintFade(p);
888
 
3328 helixhorne 889
    if ((PWEAPON(snum, p->curr_weapon, WorksLike) == PISTOL_WEAPON) &&
890
            (PWEAPON(snum, p->curr_weapon, Reload) > PWEAPON(snum, p->curr_weapon, TotalTime)))
891
        p->kickback_pic  = PWEAPON(snum, p->curr_weapon, TotalTime);
1642 terminx 892
    else p->kickback_pic = 0;
893
 
5 Plagman 894
    p->last_weapon = -1;
895
    p->weapreccnt = 0;
896
    p->interface_toggle_flag = 0;
897
    p->show_empty_weapon= 0;
898
    p->holster_weapon = 0;
899
    p->last_pissed_time = 0;
900
 
901
    p->one_parallax_sectnum = -1;
902
    p->visibility = ud.const_visibility;
903
 
904
    screenpeek              = myconnectindex;
1143 terminx 905
    g_numAnimWalls            = 0;
906
    g_numCyclers              = 0;
907
    g_animateCount              = 0;
5 Plagman 908
    parallaxtype            = 0;
1143 terminx 909
    randomseed              = 1996;
5 Plagman 910
    ud.pause_on             = 0;
911
    ud.camerasprite         =-1;
912
    ud.eog                  = 0;
913
    tempwallptr             = 0;
4220 helixhorne 914
    g_curViewscreen               =-1;
1143 terminx 915
    g_earthquakeTime          = 0;
5 Plagman 916
 
1143 terminx 917
    g_numInterpolations = 0;
5 Plagman 918
    startofdynamicinterpolations = 0;
919
 
1625 terminx 920
    if (((g&MODE_EOL) != MODE_EOL && numplayers < 2 && !g_netServer) ||
1677 terminx 921
            (!(GametypeFlags[ud.coop]&GAMETYPE_PRESERVEINVENTORYDEATH) && numplayers > 1))
5 Plagman 922
    {
1143 terminx 923
        P_ResetWeapons(snum);
924
        P_ResetInventory(snum);
5 Plagman 925
    }
4650 terminx 926
    else if (PWEAPON(snum, p->curr_weapon, WorksLike) == HANDREMOTE_WEAPON)
5 Plagman 927
    {
928
        p->ammo_amount[HANDBOMB_WEAPON]++;
929
        p->curr_weapon = HANDBOMB_WEAPON;
930
    }
931
 
932
    p->timebeforeexit   = 0;
933
    p->customexitsound  = 0;
934
}
935
 
4353 helixhorne 936
// Tweak sprites contained in moving sectors with these SE lotags.
937
#define FIXSPR_SELOTAGP(k) \
938
    ((k)==SE_0_ROTATING_SECTOR \
939
     || (k)==SE_6_SUBWAY \
940
     || (k)==SE_14_SUBWAY_CAR)
1913 helixhorne 941
 
3322 helixhorne 942
// Set up sprites in moving sectors that are to be fixed wrt a certain pivot
1913 helixhorne 943
// position and should not diverge from it due to roundoff error in the future.
3322 helixhorne 944
// Has to be after the spawning stuff.
945
static void G_SetupRotfixedSprites(void)
1913 helixhorne 946
{
3322 helixhorne 947
    int32_t i;
1913 helixhorne 948
 
949
    for (i=headspritestat[STAT_EFFECTOR]; i>=0; i=nextspritestat[i])
950
    {
951
        if (FIXSPR_SELOTAGP(sprite[i].lotag))
952
        {
1924 helixhorne 953
#ifdef YAX_ENABLE
954
            int32_t firstrun = 1;
955
#endif
3322 helixhorne 956
            int32_t j = headspritesect[sprite[i].sectnum];
4353 helixhorne 957
 
1924 helixhorne 958
            while (j>=0)
1913 helixhorne 959
            {
4353 helixhorne 960
                const spritetype *const spr = &sprite[j];
961
 
1913 helixhorne 962
                // TRIPBOMB uses t_data[7] for its own purposes. Wouldn't be
963
                // too useful with moving sectors anyway
4353 helixhorne 964
                if ((ROTFIXSPR_STATNUMP(spr->statnum) && spr->picnum!=TRIPBOMB) ||
965
                    ((spr->statnum==STAT_ACTOR || spr->statnum==STAT_ZOMBIEACTOR) &&
966
                     A_CheckSpriteTileFlags(spr->picnum, SFLAG_ROTFIXED)))
1913 helixhorne 967
                {
3322 helixhorne 968
                    int32_t pivot = i;
969
 
1913 helixhorne 970
                    if (sprite[i].lotag==0)
971
                        pivot = sprite[i].owner;
1924 helixhorne 972
                    if (j!=i && j!=pivot && pivot>=0 && pivot<MAXSPRITES)
973
                    {
974
                        // let's hope we don't step on anyone's toes here
3316 helixhorne 975
                        actor[j].t_data[7] = ROTFIXSPR_MAGIC | pivot; // 'rs' magic + pivot SE sprite index
4353 helixhorne 976
                        actor[j].t_data[8] = spr->x - sprite[pivot].x;
977
                        actor[j].t_data[9] = spr->y - sprite[pivot].y;
1924 helixhorne 978
                    }
1913 helixhorne 979
                }
1924 helixhorne 980
 
981
                j = nextspritesect[j];
982
#ifdef YAX_ENABLE
983
                if (j<0 && firstrun)
3008 helixhorne 984
                    if (sprite[i].lotag==SE_6_SUBWAY || sprite[i].lotag==SE_14_SUBWAY_CAR)
1924 helixhorne 985
                    {
986
                        firstrun = 0;
987
                        j = actor[i].t_data[9];
988
                        if (j >= 0)
989
                            j = headspritesect[j];
990
                    }
991
#endif
1913 helixhorne 992
            }
993
        }
994
    }
995
}
996
 
4045 helixhorne 997
static inline int32_t G_CheckExitSprite(int32_t i)
998
{
999
    return (sprite[i].lotag == UINT16_MAX && (sprite[i].cstat&16));
1000
}
1001
 
4703 terminx 1002
static void prelevel(char g)
5 Plagman 1003
{
1969 helixhorne 1004
    int32_t i, nexti, j, startwall, endwall;
1205 terminx 1005
    int32_t switchpicnum;
4491 helixhorne 1006
    uint8_t *tagbitmap = (uint8_t *)Xcalloc(65536>>3, 1);
5 Plagman 1007
 
2532 helixhorne 1008
    Bmemset(show2dsector, 0, sizeof(show2dsector));
4228 helixhorne 1009
#ifdef LEGACY_ROR
1450 terminx 1010
    Bmemset(ror_protectedsectors, 0, MAXSECTORS);
4228 helixhorne 1011
#endif
5 Plagman 1012
    resetprestat(0,g);
1143 terminx 1013
    g_numClouds = 0;
5 Plagman 1014
 
3976 helixhorne 1015
    G_SetupGlobalPsky();
1016
 
1229 terminx 1017
    for (i=0; i<numsectors; i++)
5 Plagman 1018
    {
1019
        sector[i].extra = 256;
1020
 
331 terminx 1021
        switch (sector[i].lotag)
5 Plagman 1022
        {
2970 helixhorne 1023
        case ST_20_CEILING_DOOR:
1024
        case ST_22_SPLITTING_DOOR:
337 terminx 1025
            if (sector[i].floorz > sector[i].ceilingz)
1026
                sector[i].lotag |= 32768;
1027
            continue;
5 Plagman 1028
        }
1029
 
331 terminx 1030
        if (sector[i].ceilingstat&1)
5 Plagman 1031
        {
331 terminx 1032
            if (waloff[sector[i].ceilingpicnum] == 0)
5 Plagman 1033
            {
331 terminx 1034
                if (sector[i].ceilingpicnum == LA)
1229 terminx 1035
                    for (j=0; j<5; j++)
5 Plagman 1036
                        tloadtile(sector[i].ceilingpicnum+j, 0);
1037
            }
1038
 
1143 terminx 1039
            if (sector[i].ceilingpicnum == CLOUDYSKIES && g_numClouds < 127)
1040
                clouds[g_numClouds++] = i;
5 Plagman 1041
 
564 terminx 1042
            if (g_player[0].ps->one_parallax_sectnum == -1)
1043
                g_player[0].ps->one_parallax_sectnum = i;
5 Plagman 1044
        }
1045
 
331 terminx 1046
        if (sector[i].lotag == 32767) //Found a secret room
5 Plagman 1047
        {
564 terminx 1048
            g_player[0].ps->max_secret_rooms++;
5 Plagman 1049
            continue;
1050
        }
1051
 
3174 helixhorne 1052
        if (sector[i].lotag == UINT16_MAX)
5 Plagman 1053
        {
564 terminx 1054
            g_player[0].ps->exitx = wall[sector[i].wallptr].x;
1055
            g_player[0].ps->exity = wall[sector[i].wallptr].y;
5 Plagman 1056
            continue;
1057
        }
1058
    }
1059
 
4045 helixhorne 1060
    // NOTE: must be safe loop because callbacks could delete sprites.
1061
    for (SPRITES_OF_STAT_SAFE(STAT_DEFAULT, i, nexti))
5 Plagman 1062
    {
4119 helixhorne 1063
        A_ResetVars(i);
3410 helixhorne 1064
#if !defined LUNATIC
3514 helixhorne 1065
        A_LoadActor(i);
3354 helixhorne 1066
#endif
4745 terminx 1067
        VM_OnEvent(EVENT_LOADACTOR, i, -1);
4045 helixhorne 1068
        if (G_CheckExitSprite(i))
5 Plagman 1069
        {
564 terminx 1070
            g_player[0].ps->exitx = SX;
1071
            g_player[0].ps->exity = SY;
5 Plagman 1072
        }
2297 helixhorne 1073
        else switch (DYNAMICTILEMAP(PN))
5 Plagman 1074
            {
337 terminx 1075
            case GPSPEED__STATIC:
4045 helixhorne 1076
                // DELETE_AFTER_LOADACTOR. Must not change statnum.
337 terminx 1077
                sector[SECT].extra = SLT;
1078
                break;
5 Plagman 1079
 
337 terminx 1080
            case CYCLER__STATIC:
4045 helixhorne 1081
                // DELETE_AFTER_LOADACTOR. Must not change statnum.
1143 terminx 1082
                if (g_numCyclers >= MAXCYCLERS)
337 terminx 1083
                {
1084
                    Bsprintf(tempbuf,"\nToo many cycling sectors (%d max).",MAXCYCLERS);
1143 terminx 1085
                    G_GameExit(tempbuf);
337 terminx 1086
                }
1143 terminx 1087
                cyclers[g_numCyclers][0] = SECT;
1088
                cyclers[g_numCyclers][1] = SLT;
1089
                cyclers[g_numCyclers][2] = SS;
1090
                cyclers[g_numCyclers][3] = sector[SECT].floorshade;
1091
                cyclers[g_numCyclers][4] = SHT;
1092
                cyclers[g_numCyclers][5] = (SA == 1536);
1093
                g_numCyclers++;
337 terminx 1094
                break;
5 Plagman 1095
 
337 terminx 1096
            case SECTOREFFECTOR__STATIC:
1097
            case ACTIVATOR__STATIC:
1098
            case TOUCHPLATE__STATIC:
1099
            case ACTIVATORLOCKED__STATIC:
1100
            case MUSICANDSFX__STATIC:
1101
            case LOCATORS__STATIC:
1102
            case MASTERSWITCH__STATIC:
1103
            case RESPAWN__STATIC:
1349 terminx 1104
                sprite[i].cstat &= ~(1|256);
337 terminx 1105
                break;
5 Plagman 1106
            }
1107
    }
1108
 
4045 helixhorne 1109
    // Delete some effector / effector modifier sprites AFTER the loop running
1110
    // the LOADACTOR events. DELETE_AFTER_LOADACTOR.
1111
    for (SPRITES_OF_STAT_SAFE(STAT_DEFAULT, i, nexti))
1112
        if (!G_CheckExitSprite(i))
1113
            switch (DYNAMICTILEMAP(PN))
1114
            {
1115
            case GPSPEED__STATIC:
1116
            case CYCLER__STATIC:
1117
                A_DeleteSprite(i);
1118
                break;
1119
            }
1120
 
4703 terminx 1121
    for (i = 0; i < MAXSPRITES; i++)
5 Plagman 1122
    {
4703 terminx 1123
        if (sprite[i].statnum < MAXSTATUS && (PN != SECTOREFFECTOR || SLT != SE_14_SUBWAY_CAR))
1124
            A_Spawn(-1, i);
5 Plagman 1125
    }
1126
 
4703 terminx 1127
    for (i = 0; i < MAXSPRITES; i++)
1128
    {
1129
        if (sprite[i].statnum < MAXSTATUS && PN == SECTOREFFECTOR && SLT == SE_14_SUBWAY_CAR)
1130
            A_Spawn(-1, i);
1131
    }
5 Plagman 1132
 
3322 helixhorne 1133
    G_SetupRotfixedSprites();
1913 helixhorne 1134
 
1969 helixhorne 1135
    for (i=headspritestat[STAT_DEFAULT]; i>=0; i=nextspritestat[i])
5 Plagman 1136
    {
1972 helixhorne 1137
        int32_t ii;
2981 helixhorne 1138
 
2174 helixhorne 1139
        if (PN <= 0)  // oob safety for switch below
1140
            continue;
1141
 
1968 helixhorne 1142
        for (ii=0; ii<2; ii++)
2297 helixhorne 1143
            switch (DYNAMICTILEMAP(PN-1+ii))
337 terminx 1144
            {
1968 helixhorne 1145
            case DIPSWITCH__STATIC:
1146
            case DIPSWITCH2__STATIC:
1147
            case PULLSWITCH__STATIC:
1148
            case HANDSWITCH__STATIC:
1149
            case SLOTDOOR__STATIC:
1150
            case LIGHTSWITCH__STATIC:
1151
            case SPACELIGHTSWITCH__STATIC:
1152
            case SPACEDOORSWITCH__STATIC:
1153
            case FRANKENSTINESWITCH__STATIC:
1154
            case LIGHTSWITCH2__STATIC:
1155
            case POWERSWITCH1__STATIC:
1156
            case LOCKSWITCH1__STATIC:
1157
            case POWERSWITCH2__STATIC:
2981 helixhorne 1158
                // the lower code only for the 'on' state (*)
1969 helixhorne 1159
                if (ii==0)
1968 helixhorne 1160
                {
3592 helixhorne 1161
                    j = sprite[i].lotag;
1969 helixhorne 1162
                    tagbitmap[j>>3] |= 1<<(j&7);
1163
                }
1968 helixhorne 1164
 
1165
                break;
337 terminx 1166
            }
1969 helixhorne 1167
    }
1968 helixhorne 1168
 
1969 helixhorne 1169
    // initially 'on' SE 12 light (*)
1170
    for (j=headspritestat[STAT_EFFECTOR]; j>=0; j=nextspritestat[j])
1171
    {
3592 helixhorne 1172
        int32_t t = sprite[j].hitag;
1969 helixhorne 1173
 
3008 helixhorne 1174
        if (sprite[j].lotag == SE_12_LIGHT_SWITCH && tagbitmap[t>>3]&(1<<(t&7)))
1969 helixhorne 1175
            actor[j].t_data[0] = 1;
5 Plagman 1176
    }
1177
 
3592 helixhorne 1178
    Bfree(tagbitmap);
1969 helixhorne 1179
 
1143 terminx 1180
    g_mirrorCount = 0;
5 Plagman 1181
 
333 terminx 1182
    for (i = 0; i < numwalls; i++)
5 Plagman 1183
    {
1184
        walltype *wal;
1185
        wal = &wall[i];
1186
 
331 terminx 1187
        if (wal->overpicnum == MIRROR && (wal->cstat&32) != 0)
5 Plagman 1188
        {
1189
            j = wal->nextsector;
1190
 
333 terminx 1191
            if ((j >= 0) && sector[j].ceilingpicnum != MIRROR)
5 Plagman 1192
            {
2233 helixhorne 1193
                if (g_mirrorCount > 63)
1194
                    G_GameExit("\nToo many mirrors (64 max.)");
1195
 
5 Plagman 1196
                sector[j].ceilingpicnum = MIRROR;
1197
                sector[j].floorpicnum = MIRROR;
1143 terminx 1198
                g_mirrorWall[g_mirrorCount] = i;
1199
                g_mirrorSector[g_mirrorCount] = j;
1200
                g_mirrorCount++;
5 Plagman 1201
                continue;
1202
            }
1203
        }
1204
 
1143 terminx 1205
        if (g_numAnimWalls >= MAXANIMWALLS)
5 Plagman 1206
        {
1207
            Bsprintf(tempbuf,"\nToo many 'anim' walls (%d max).",MAXANIMWALLS);
1143 terminx 1208
            G_GameExit(tempbuf);
5 Plagman 1209
        }
1210
 
1143 terminx 1211
        animwall[g_numAnimWalls].tag = 0;
1212
        animwall[g_numAnimWalls].wallnum = 0;
3010 helixhorne 1213
 
4221 helixhorne 1214
        switchpicnum = G_GetForcefieldPicnum(i);
2172 helixhorne 1215
 
1216
        if (switchpicnum >= 0)
5 Plagman 1217
        {
2297 helixhorne 1218
            switch (DYNAMICTILEMAP(switchpicnum))
2172 helixhorne 1219
            {
1220
            case FANSHADOW__STATIC:
1221
            case FANSPRITE__STATIC:
1222
                wall->cstat |= 65;
1223
                animwall[g_numAnimWalls].wallnum = i;
1224
                g_numAnimWalls++;
1225
                break;
5 Plagman 1226
 
2172 helixhorne 1227
            case W_FORCEFIELD__STATIC:
1228
                if (wal->overpicnum==W_FORCEFIELD__STATIC)
1229
                    for (j=0; j<3; j++)
1230
                        tloadtile(W_FORCEFIELD+j, 0);
1231
                if (wal->shade > 31)
1232
                    wal->cstat = 0;
4221 helixhorne 1233
                else wal->cstat |= FORCEFIELD_CSTAT|256;
5 Plagman 1234
 
1235
 
2172 helixhorne 1236
                if (wal->lotag && wal->nextwall >= 0)
1237
                    wall[wal->nextwall].lotag =
1238
                        wal->lotag;
5 Plagman 1239
 
2172 helixhorne 1240
            case BIGFORCE__STATIC:
1241
                animwall[g_numAnimWalls].wallnum = i;
1242
                g_numAnimWalls++;
5 Plagman 1243
 
2172 helixhorne 1244
                continue;
1245
            }
5 Plagman 1246
        }
1247
 
1248
        wal->extra = -1;
1249
 
2297 helixhorne 1250
        switch (DYNAMICTILEMAP(wal->picnum))
5 Plagman 1251
        {
337 terminx 1252
        case WATERTILE2__STATIC:
1229 terminx 1253
            for (j=0; j<3; j++)
337 terminx 1254
                tloadtile(wal->picnum+j, 0);
1255
            break;
5 Plagman 1256
 
337 terminx 1257
        case TECHLIGHT2__STATIC:
1258
        case TECHLIGHT4__STATIC:
1259
            tloadtile(wal->picnum, 0);
1260
            break;
1261
        case W_TECHWALL1__STATIC:
1262
        case W_TECHWALL2__STATIC:
1263
        case W_TECHWALL3__STATIC:
1264
        case W_TECHWALL4__STATIC:
1143 terminx 1265
            animwall[g_numAnimWalls].wallnum = i;
1266
            //                animwall[g_numAnimWalls].tag = -1;
1267
            g_numAnimWalls++;
337 terminx 1268
            break;
1269
        case SCREENBREAK6__STATIC:
1270
        case SCREENBREAK7__STATIC:
1271
        case SCREENBREAK8__STATIC:
1229 terminx 1272
            for (j=SCREENBREAK6; j<SCREENBREAK9; j++)
337 terminx 1273
                tloadtile(j, 0);
1143 terminx 1274
            animwall[g_numAnimWalls].wallnum = i;
1275
            animwall[g_numAnimWalls].tag = -1;
1276
            g_numAnimWalls++;
337 terminx 1277
            break;
5 Plagman 1278
 
337 terminx 1279
        case FEMPIC1__STATIC:
1280
        case FEMPIC2__STATIC:
1281
        case FEMPIC3__STATIC:
1282
            wal->extra = wal->picnum;
1143 terminx 1283
            animwall[g_numAnimWalls].tag = -1;
337 terminx 1284
            if (ud.lockout)
1285
            {
1286
                if (wal->picnum == FEMPIC1)
1287
                    wal->picnum = BLANKSCREEN;
1288
                else wal->picnum = SCREENBREAK6;
1289
            }
5 Plagman 1290
 
1143 terminx 1291
            animwall[g_numAnimWalls].wallnum = i;
1292
            animwall[g_numAnimWalls].tag = wal->picnum;
1293
            g_numAnimWalls++;
337 terminx 1294
            break;
5 Plagman 1295
 
337 terminx 1296
        case SCREENBREAK1__STATIC:
1297
        case SCREENBREAK2__STATIC:
1298
        case SCREENBREAK3__STATIC:
1299
        case SCREENBREAK4__STATIC:
1300
        case SCREENBREAK5__STATIC:
3010 helixhorne 1301
            //
337 terminx 1302
        case SCREENBREAK9__STATIC:
1303
        case SCREENBREAK10__STATIC:
1304
        case SCREENBREAK11__STATIC:
1305
        case SCREENBREAK12__STATIC:
1306
        case SCREENBREAK13__STATIC:
1307
        case SCREENBREAK14__STATIC:
1308
        case SCREENBREAK15__STATIC:
1309
        case SCREENBREAK16__STATIC:
1310
        case SCREENBREAK17__STATIC:
1311
        case SCREENBREAK18__STATIC:
1312
        case SCREENBREAK19__STATIC:
1143 terminx 1313
            animwall[g_numAnimWalls].wallnum = i;
1314
            animwall[g_numAnimWalls].tag = wal->picnum;
1315
            g_numAnimWalls++;
337 terminx 1316
            break;
5 Plagman 1317
        }
1318
    }
1319
 
1320
    //Invalidate textures in sector behind mirror
1229 terminx 1321
    for (i=0; i<g_mirrorCount; i++)
5 Plagman 1322
    {
1143 terminx 1323
        startwall = sector[g_mirrorSector[i]].wallptr;
1324
        endwall = startwall + sector[g_mirrorSector[i]].wallnum;
1229 terminx 1325
        for (j=startwall; j<endwall; j++)
5 Plagman 1326
        {
1327
            wall[j].picnum = MIRROR;
1328
            wall[j].overpicnum = MIRROR;
1143 terminx 1329
            if (wall[g_mirrorWall[i]].pal == 4)
1097 terminx 1330
                wall[j].pal = 4;
5 Plagman 1331
        }
1332
    }
1333
}
1334
 
1536 helixhorne 1335
 
3950 helixhorne 1336
void G_NewGame(int32_t vn, int32_t ln, int32_t sk)
5 Plagman 1337
{
1143 terminx 1338
    DukePlayer_t *p = g_player[0].ps;
1205 terminx 1339
    int32_t i;
5 Plagman 1340
 
2996 helixhorne 1341
    G_HandleAsync();
423 terminx 1342
 
4986 terminx 1343
    if (g_skillSoundVoice >= 0 && ud.config.SoundToggle)
423 terminx 1344
    {
4449 helixhorne 1345
        while (FX_SoundActive(g_skillSoundVoice))
2996 helixhorne 1346
            G_HandleAsync();
335 terminx 1347
    }
428 terminx 1348
 
4449 helixhorne 1349
    g_skillSoundVoice = -1;
5 Plagman 1350
 
1351
    ready2send = 0;
1352
 
1587 terminx 1353
    if (ud.m_recstat != 2 && ud.last_level >= 0 && (g_netServer || ud.multimode > 1) && (ud.coop&GAMETYPE_SCORESHEET))
1143 terminx 1354
        G_BonusScreen(1);
5 Plagman 1355
 
3566 helixhorne 1356
    if (ln == 0 && vn == 3 && (!g_netServer && ud.multimode < 2) && ud.lockout == 0
1357
            && (G_GetLogoFlags() & LOGO_NOE4CUTSCENE)==0)
5 Plagman 1358
    {
4948 hendricks2 1359
        S_PlayMusic(MapInfo[MUS_BRIEFING].musicfn);
5 Plagman 1360
 
1361
        flushperms();
1362
        setview(0,0,xdim-1,ydim-1);
1363
        clearview(0L);
1364
        nextpage();
1365
 
4987 terminx 1366
        i = G_PlayAnim("vol41a.anm");
5 Plagman 1367
        clearview(0L);
1368
        nextpage();
4243 hendricks2 1369
        if (i)
1370
            goto end_vol4a;
5 Plagman 1371
 
4987 terminx 1372
        i = G_PlayAnim("vol42a.anm");
4243 hendricks2 1373
        clearview(0L);
1374
        nextpage();
1375
        if (i)
1376
            goto end_vol4a;
1377
 
4987 terminx 1378
        G_PlayAnim("vol43a.anm");
5 Plagman 1379
        clearview(0L);
1380
        nextpage();
1381
 
4243 hendricks2 1382
end_vol4a:
5 Plagman 1383
        FX_StopAllSounds();
1384
    }
1385
 
1346 terminx 1386
    g_showShareware = GAMETICSPERSEC*34;
5 Plagman 1387
 
3950 helixhorne 1388
    ud.level_number = ln;
1389
    ud.volume_number = vn;
1390
    ud.player_skill = sk;
1391
    ud.secretlevel = 0;
5 Plagman 1392
    ud.from_bonus = 0;
1393
 
1394
    ud.last_level = -1;
1143 terminx 1395
    g_lastSaveSlot = -1;
4706 terminx 1396
 
4852 hendricks2 1397
#ifdef EDUKE32_TOUCH_DEVICES
4706 terminx 1398
    p->zoom = 360;
1399
#else
3950 helixhorne 1400
    p->zoom = 768;
4706 terminx 1401
#endif
3950 helixhorne 1402
    p->gm = 0;
4738 hendricks2 1403
    M_CloseMenu(0);
5 Plagman 1404
 
3410 helixhorne 1405
#if !defined LUNATIC
423 terminx 1406
    //AddLog("Newgame");
1179 terminx 1407
    Gv_ResetVars();
5 Plagman 1408
 
1143 terminx 1409
    Gv_InitWeaponPointers();
5 Plagman 1410
 
2230 helixhorne 1411
    // PK: Gv_ResetVars() might trip up the system (pointer) gamevars,
1412
    // e.g. if some earlier-version CON code had been loaded before
1413
    Gv_RefreshPointers();
3459 helixhorne 1414
#endif
1415
    Gv_ResetSystemDefaults();
2230 helixhorne 1416
 
1229 terminx 1417
    for (i=0; i<(MAXVOLUMES*MAXLEVELS); i++)
1143 terminx 1418
        if (MapInfo[i].savedstate)
862 terminx 1419
        {
4923 terminx 1420
            Baligned_free(MapInfo[i].savedstate);
1143 terminx 1421
            MapInfo[i].savedstate = NULL;
862 terminx 1422
        }
1423
 
423 terminx 1424
    if (ud.m_coop != 1)
1425
    {
1229 terminx 1426
        for (i=0; i<MAX_WEAPONS; i++)
5 Plagman 1427
        {
3328 helixhorne 1428
            if (PWEAPON(0, i, WorksLike)==PISTOL_WEAPON)
5 Plagman 1429
            {
423 terminx 1430
                p->curr_weapon = i;
1625 terminx 1431
                p->gotweapon |= (1<<i);
1444 terminx 1432
                p->ammo_amount[i] = min(p->max_ammo_amount[i], 48);
5 Plagman 1433
            }
3328 helixhorne 1434
            else if (PWEAPON(0, i, WorksLike)==KNEE_WEAPON)
1625 terminx 1435
                p->gotweapon |= (1<<i);
3328 helixhorne 1436
            else if (PWEAPON(0, i, WorksLike)==HANDREMOTE_WEAPON)
1625 terminx 1437
                p->gotweapon |= (1<<i);
5 Plagman 1438
        }
423 terminx 1439
        p->last_weapon = -1;
5 Plagman 1440
    }
428 terminx 1441
 
3950 helixhorne 1442
    display_mirror = 0;
5 Plagman 1443
 
3951 helixhorne 1444
#ifdef LUNATIC
1445
    // NOTE: Lunatic state creation is relatively early. No map has yet been loaded.
1446
    // XXX: What about the cases where G_EnterLevel() is called without a preceding G_NewGame()?
1447
    El_CreateGameState();
1448
    G_PostCreateGameState();
1449
#endif
4745 terminx 1450
    VM_OnEvent(EVENT_NEWGAME, g_player[myconnectindex].ps->i, myconnectindex);
5 Plagman 1451
}
1452
 
395 terminx 1453
static void resetpspritevars(char g)
5 Plagman 1454
{
2897 helixhorne 1455
    int16_t i, j; //circ;
1456
 
1457 terminx 1457
    uint8_t aimmode[MAXPLAYERS],autoaim[MAXPLAYERS],weaponswitch[MAXPLAYERS];
1458
    DukeStatus_t tsbar[MAXPLAYERS];
5 Plagman 1459
 
2342 helixhorne 1460
    if (g_player[0].ps->cursectnum >= 0)  // < 0 may happen if we start a map in void space (e.g. testing it)
1461
    {
1462
        A_InsertSprite(g_player[0].ps->cursectnum,g_player[0].ps->pos.x,g_player[0].ps->pos.y,g_player[0].ps->pos.z,
1463
                       APLAYER,0,0,0,g_player[0].ps->ang,0,0,0,10);
1464
    }
5 Plagman 1465
 
1457 terminx 1466
    if (ud.recstat != 2)
2379 helixhorne 1467
        for (TRAVERSE_CONNECT(i))
5 Plagman 1468
        {
2379 helixhorne 1469
            aimmode[i] = g_player[i].ps->aim_mode;
1470
            autoaim[i] = g_player[i].ps->auto_aim;
1471
            weaponswitch[i] = g_player[i].ps->weaponswitch;
1472
            if ((g_netServer || ud.multimode > 1) && (GametypeFlags[ud.coop]&GAMETYPE_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
1473
            {
1474
                for (j=0; j<MAX_WEAPONS; j++)
1475
                    tsbar[i].ammo_amount[j] = g_player[i].ps->ammo_amount[j];
5 Plagman 1476
 
2379 helixhorne 1477
                tsbar[i].gotweapon = g_player[i].ps->gotweapon;
1478
                Bmemcpy(tsbar[i].inv_amount, g_player[i].ps->inv_amount, sizeof(tsbar[i].inv_amount));
1479
                tsbar[i].curr_weapon = g_player[i].ps->curr_weapon;
1480
                tsbar[i].inven_icon = g_player[i].ps->inven_icon;
1481
            }
5 Plagman 1482
        }
1483
 
1143 terminx 1484
    P_ResetStatus(0);
5 Plagman 1485
 
2379 helixhorne 1486
    for (TRAVERSE_CONNECT(i))
1487
        if (i) Bmemcpy(g_player[i].ps,g_player[0].ps,sizeof(DukePlayer_t));
5 Plagman 1488
 
514 terminx 1489
    if (ud.recstat != 2)
2379 helixhorne 1490
        for (TRAVERSE_CONNECT(i))
5 Plagman 1491
        {
2379 helixhorne 1492
            g_player[i].ps->aim_mode = aimmode[i];
1493
            g_player[i].ps->auto_aim = autoaim[i];
1494
            g_player[i].ps->weaponswitch = weaponswitch[i];
1495
            if ((g_netServer || ud.multimode > 1) && (GametypeFlags[ud.coop]&GAMETYPE_PRESERVEINVENTORYDEATH) && ud.last_level >= 0)
1496
            {
1497
                for (j=0; j<MAX_WEAPONS; j++)
1498
                    g_player[i].ps->ammo_amount[j] = tsbar[i].ammo_amount[j];
1625 terminx 1499
 
2379 helixhorne 1500
                g_player[i].ps->gotweapon = tsbar[i].gotweapon;
1501
                g_player[i].ps->curr_weapon = tsbar[i].curr_weapon;
1502
                g_player[i].ps->inven_icon = tsbar[i].inven_icon;
1503
                Bmemcpy(g_player[i].ps->inv_amount, tsbar[i].inv_amount, sizeof(tsbar[i].inv_amount));
1504
            }
5 Plagman 1505
        }
1506
 
1143 terminx 1507
    g_numPlayerSprites = 0;
2079 helixhorne 1508
//    circ = 2048/ud.multimode;
5 Plagman 1509
 
1143 terminx 1510
    g_whichPalForPlayer = 9;
1552 terminx 1511
    j = 0;
1143 terminx 1512
    i = headspritestat[STAT_PLAYER];
331 terminx 1513
    while (i >= 0)
5 Plagman 1514
    {
2897 helixhorne 1515
        const int32_t nexti = nextspritestat[i];
1516
        spritetype *const s = &sprite[i];
5 Plagman 1517
 
1143 terminx 1518
        if (g_numPlayerSprites == MAXPLAYERS)
1519
            G_GameExit("\nToo many player sprites (max 16.)");
5 Plagman 1520
 
4703 terminx 1521
        g_playerSpawnPoints[g_numPlayerSprites].pos.x = s->x;
1522
        g_playerSpawnPoints[g_numPlayerSprites].pos.y = s->y;
1523
        g_playerSpawnPoints[g_numPlayerSprites].pos.z = s->z;
1524
        g_playerSpawnPoints[g_numPlayerSprites].ang = s->ang;
1525
        g_playerSpawnPoints[g_numPlayerSprites].sect = s->sectnum;
5 Plagman 1526
 
1143 terminx 1527
        g_numPlayerSprites++;
1552 terminx 1528
 
1529
        if (j < MAXPLAYERS)
5 Plagman 1530
        {
1531
            s->owner = i;
1532
            s->shade = 0;
1533
            s->xrepeat = 42;
1534
            s->yrepeat = 36;
2879 helixhorne 1535
            if (!g_fakeMultiMode)
1536
                s->cstat = j < numplayers ? 1+256 : 32768;
1537
            else
2897 helixhorne 1538
                s->cstat = j < ud.multimode ? 1+256 : 32768;
5 Plagman 1539
            s->xoffset = 0;
1540
            s->clipdist = 64;
1541
 
1552 terminx 1542
//            if (j < playerswhenstarted)
5 Plagman 1543
            {
1552 terminx 1544
                if ((g&MODE_EOL) != MODE_EOL || g_player[j].ps->last_extra == 0)
1545
                {
1546
                    g_player[j].ps->last_extra = g_player[j].ps->max_player_health;
1547
                    s->extra = g_player[j].ps->max_player_health;
1548
                    g_player[j].ps->runspeed = g_playerFriction;
1549
                }
1550
                else s->extra = g_player[j].ps->last_extra;
5 Plagman 1551
 
1552 terminx 1552
                s->yvel = j;
5 Plagman 1553
 
1587 terminx 1554
                if (!g_player[j].pcolor && (g_netServer || ud.multimode > 1) && !(GametypeFlags[ud.coop] & GAMETYPE_TDM))
53 terminx 1555
                {
1552 terminx 1556
                    if (s->pal == 0)
1557
                    {
1558
                        int32_t k = 0;
56 terminx 1559
 
1552 terminx 1560
                        for (; k<ud.multimode; k++)
335 terminx 1561
                        {
1552 terminx 1562
                            if (g_whichPalForPlayer == g_player[k].ps->palookup)
1563
                            {
1564
                                g_whichPalForPlayer++;
1565
                                if (g_whichPalForPlayer >= 17)
1566
                                    g_whichPalForPlayer = 9;
1567
                                k=0;
1568
                            }
56 terminx 1569
                        }
1552 terminx 1570
                        g_player[j].pcolor = s->pal = g_player[j].ps->palookup = g_whichPalForPlayer++;
1571
                        if (g_whichPalForPlayer >= 17)
1572
                            g_whichPalForPlayer = 9;
56 terminx 1573
                    }
1552 terminx 1574
                    else g_player[j].pcolor = g_player[j].ps->palookup = s->pal;
53 terminx 1575
                }
1552 terminx 1576
                else
1577
                {
1578
                    int32_t k = g_player[j].pcolor;
5 Plagman 1579
 
1552 terminx 1580
                    if (GametypeFlags[ud.coop] & GAMETYPE_TDM)
1581
                    {
1582
                        k = G_GetTeamPalette(g_player[j].pteam);
1583
                        g_player[j].ps->team = g_player[j].pteam;
1584
                    }
1585
                    s->pal = g_player[j].ps->palookup = k;
268 terminx 1586
                }
1587
 
1552 terminx 1588
                g_player[j].ps->i = i;
1589
                g_player[j].ps->frag_ps = j;
1625 terminx 1590
                actor[i].owner = i;
5 Plagman 1591
 
3100 hendricks2 1592
                g_player[j].ps->autostep = (20L<<8);
1593
                g_player[j].ps->autostep_sbw = (4L<<8);
1594
 
4703 terminx 1595
                actor[i].bpos.x = g_player[j].ps->bobpos.x = g_player[j].ps->opos.x = g_player[j].ps->pos.x =        s->x;
1596
                actor[i].bpos.y = g_player[j].ps->bobpos.y = g_player[j].ps->opos.y = g_player[j].ps->pos.y =        s->y;
3314 helixhorne 1597
                actor[i].bpos.z = g_player[j].ps->opos.z = g_player[j].ps->pos.z =        s->z;
1552 terminx 1598
                g_player[j].ps->oang  = g_player[j].ps->ang  =        s->ang;
5 Plagman 1599
 
1552 terminx 1600
                updatesector(s->x,s->y,&g_player[j].ps->cursectnum);
1601
            }
5 Plagman 1602
 
1552 terminx 1603
            j++;
5 Plagman 1604
        }
2452 helixhorne 1605
        else A_DeleteSprite(i);
2897 helixhorne 1606
 
5 Plagman 1607
        i = nexti;
1608
    }
1609
}
1610
 
400 terminx 1611
static inline void clearfrags(void)
5 Plagman 1612
{
2201 helixhorne 1613
    int32_t i;
5 Plagman 1614
 
2201 helixhorne 1615
    for (i=0; i<ud.multimode; i++)
564 terminx 1616
    {
2201 helixhorne 1617
        playerdata_t *p = &g_player[i];
1618
 
1619
        p->ps->frag = p->ps->fraggedself = 0;
2878 helixhorne 1620
        Bmemset(p->frags, 0, sizeof(p->frags));
564 terminx 1621
    }
5 Plagman 1622
}
1623
 
3929 helixhorne 1624
void G_ResetTimers(uint8_t keepgtics)
5 Plagman 1625
{
4703 terminx 1626
    totalclock = cloudtotalclock = ototalclock = lockclock = 0;
5 Plagman 1627
    ready2send = 1;
1143 terminx 1628
    g_levelTextTime = 85;
4831 helixhorne 1629
 
3929 helixhorne 1630
    if (!keepgtics)
1631
        g_moveThingsCount = 0;
4831 helixhorne 1632
 
4655 terminx 1633
    if (g_curViewscreen >= 0)
1634
        actor[g_curViewscreen].t_data[0] = 0;
5 Plagman 1635
}
1636
 
1802 terminx 1637
void G_ClearFIFO(void)
5 Plagman 1638
{
1802 terminx 1639
    int32_t i = MAXPLAYERS-1;
1640
 
1641
    g_emuJumpTics = 0;
563 terminx 1642
 
1802 terminx 1643
    Bmemset(&avg, 0, sizeof(input_t));
5 Plagman 1644
 
933 terminx 1645
    clearbufbyte(&loc,sizeof(input_t),0L);
1646
    clearbufbyte(&inputfifo,sizeof(input_t)*MOVEFIFOSIZ*MAXPLAYERS,0L);
1143 terminx 1647
 
1802 terminx 1648
    for (; i >= 0; i--)
564 terminx 1649
    {
1650
        if (g_player[i].sync != NULL)
1802 terminx 1651
            Bmemset(g_player[i].sync, 0, sizeof(input_t));
1652
        g_player[i].vote = g_player[i].gotvote = 0;
564 terminx 1653
    }
5 Plagman 1654
}
1655
 
1802 terminx 1656
int32_t G_FindLevelByFile(const char *fn)
398 terminx 1657
{
4703 terminx 1658
    for (int volume = 0; volume < MAXVOLUMES; volume++)
398 terminx 1659
    {
4703 terminx 1660
        const int voloff = volume * MAXLEVELS;
1661
        for (int level = 0; level < MAXLEVELS; level++)
398 terminx 1662
        {
4703 terminx 1663
            if (MapInfo[voloff + level].filename == NULL)
1664
                continue;
1665
 
1666
            if (!Bstrcasecmp(fn, MapInfo[voloff + level].filename))
1667
                return voloff + level;
398 terminx 1668
        }
1669
    }
4703 terminx 1670
 
1671
    return MAXLEVELS * MAXVOLUMES;
398 terminx 1672
}
1673
 
4978 hendricks2 1674
static void G_FadeLoad(int32_t r, int32_t g, int32_t b, int32_t start, int32_t end, int32_t step, int32_t ticwait, int32_t tc)
1625 terminx 1675
{
1969 helixhorne 1676
    int32_t m = (step < 0) ? -1 : 1;
1677
 
1678
    int32_t nexttic = totalclock;
1679
 
1680
    for (; m*start <= m*end; start += step)
1625 terminx 1681
    {
1969 helixhorne 1682
        while (totalclock < nexttic)
1683
            sampletimer();
1684
        nexttic += ticwait;
1685
 
1686
        if (KB_KeyPressed(sc_Space))
1625 terminx 1687
        {
1969 helixhorne 1688
            KB_ClearKeyDown(sc_Space);
1689
            return;
1690
        }
1625 terminx 1691
 
1969 helixhorne 1692
        G_FadePalette(r,g,b,start|128);
1693
        flushperms();
4978 hendricks2 1694
        G_DoLoadScreen(" ", tc);
1625 terminx 1695
    }
1696
}
1697
 
4884 hendricks2 1698
static int32_t G_TryMapHack(const char *mhkfile)
1699
{
1700
    int32_t retval = loadmaphack(mhkfile);
1754 helixhorne 1701
 
4884 hendricks2 1702
    if (!retval)
1703
        initprintf("Loaded map hack file \"%s\"\n", mhkfile);
1704
 
1705
    return retval;
1706
}
1707
 
1754 helixhorne 1708
static void G_LoadMapHack(char *outbuf, const char *filename)
1709
{
1710
    if (filename != NULL)
1711
        Bstrcpy(outbuf, filename);
1802 terminx 1712
 
3735 helixhorne 1713
    append_ext_UNSAFE(outbuf, ".mhk");
1802 terminx 1714
 
4884 hendricks2 1715
    if (G_TryMapHack(outbuf))
1716
    {
4933 helixhorne 1717
        usermaphack_t *mapinfo = (usermaphack_t*)bsearch(
1718
            &g_loadedMapHack, usermaphacks, num_usermaphacks, sizeof(usermaphack_t),
1719
            compare_usermaphacks);
1720
 
4884 hendricks2 1721
        if (mapinfo)
1722
            G_TryMapHack(mapinfo->mhkfile);
1723
    }
1754 helixhorne 1724
}
1725
 
2202 helixhorne 1726
// levnamebuf should have at least size BMAX_PATH
1727
void G_SetupFilenameBasedMusic(char *levnamebuf, const char *boardfilename, int32_t level_number)
1728
{
4703 terminx 1729
    char *p, *exts[] = {
1730
#ifdef HAVE_FLAC
1731
                 "flac",
1732
#endif
1733
#ifdef HAVE_VORBIS
1734
                 "ogg",
1735
#endif
1736
                 "mid"
1737
             };
2202 helixhorne 1738
 
4703 terminx 1739
    Bstrncpy(levnamebuf, boardfilename, BMAX_PATH);
2202 helixhorne 1740
 
4703 terminx 1741
    Bcorrectfilename(levnamebuf, 0);
2202 helixhorne 1742
 
4703 terminx 1743
    if (NULL == (p = Bstrrchr(levnamebuf, '.')))
2202 helixhorne 1744
    {
1745
        p = levnamebuf + Bstrlen(levnamebuf);
1746
        p[0] = '.';
1747
    }
1748
 
4706 terminx 1749
    for (unsigned int i = 0; i < ARRAY_SIZE(exts); i++)
2202 helixhorne 1750
    {
4703 terminx 1751
        int32_t fil;
2202 helixhorne 1752
 
4703 terminx 1753
        Bmemcpy(p+1, exts[i], Bstrlen(exts[i]) + 1);
1754
 
1755
        if ((fil = kopen4loadfrommod(levnamebuf, 0)) != -1)
1756
        {
1757
            kclose(fil);
4948 hendricks2 1758
            realloc_copy(&MapInfo[level_number].musicfn, levnamebuf);
4703 terminx 1759
            return;
1760
        }
2202 helixhorne 1761
    }
1762
 
4948 hendricks2 1763
    realloc_copy(&MapInfo[level_number].musicfn, "dethtoll.mid");
2202 helixhorne 1764
}
1765
 
4703 terminx 1766
static inline int G_HaveUserMap(void)
4214 helixhorne 1767
{
1768
    return (boardfilename[0] != 0 && ud.m_level_number == 7 && ud.m_volume_number == 0);
1769
}
1770
 
1205 terminx 1771
int32_t G_EnterLevel(int32_t g)
5 Plagman 1772
{
2695 helixhorne 1773
    int32_t i, mii;
5 Plagman 1774
    char levname[BMAX_PATH];
1775
 
1143 terminx 1776
//    flushpackets();
1777
//    waitforeverybody();
564 terminx 1778
    vote_map = vote_episode = voting = -1;
1779
 
5 Plagman 1780
    ud.respawn_monsters = ud.m_respawn_monsters;
1781
    ud.respawn_items    = ud.m_respawn_items;
1782
    ud.respawn_inventory    = ud.m_respawn_inventory;
1783
    ud.monsters_off = ud.m_monsters_off;
1784
    ud.coop = ud.m_coop;
1785
    ud.marker = ud.m_marker;
1786
    ud.ffire = ud.m_ffire;
147 terminx 1787
    ud.noexits = ud.m_noexits;
5 Plagman 1788
 
3950 helixhorne 1789
    if ((g&MODE_DEMO) != MODE_DEMO)
1790
        ud.recstat = ud.m_recstat;
333 terminx 1791
    if ((g&MODE_DEMO) == 0 && ud.recstat == 2)
5 Plagman 1792
        ud.recstat = 0;
1793
 
1802 terminx 1794
    if (g_networkMode != NET_DEDICATED_SERVER)
1795
    {
1796
        FX_StopAllSounds();
1797
        S_ClearSoundLocks();
1798
        FX_SetReverb(0);
1799
        setgamemode(ud.config.ScreenMode,ud.config.ScreenWidth,ud.config.ScreenHeight,ud.config.ScreenBPP);
1800
    }          
5 Plagman 1801
 
4214 helixhorne 1802
    if (G_HaveUserMap())
392 terminx 1803
    {
1444 terminx 1804
        int32_t volume, level;
398 terminx 1805
 
412 terminx 1806
        Bcorrectfilename(boardfilename,0);
1807
 
1802 terminx 1808
        volume = level = G_FindLevelByFile(boardfilename);
398 terminx 1809
 
1473 terminx 1810
        if (level != MAXLEVELS*MAXVOLUMES)
392 terminx 1811
        {
1444 terminx 1812
            level &= MAXLEVELS-1;
1813
            volume = (volume - level) / MAXLEVELS;
1814
 
392 terminx 1815
            ud.level_number = ud.m_level_number = level;
1816
            ud.volume_number = ud.m_volume_number = volume;
1817
            boardfilename[0] = 0;
1818
        }
1819
    }
1820
 
2695 helixhorne 1821
    mii = (ud.volume_number*MAXLEVELS)+ud.level_number;
1822
 
1823
    if (MapInfo[mii].name == NULL || MapInfo[mii].filename == NULL)
382 terminx 1824
    {
4214 helixhorne 1825
        if (G_HaveUserMap())
385 terminx 1826
        {
2695 helixhorne 1827
            if (MapInfo[mii].filename == NULL)
4491 helixhorne 1828
                MapInfo[mii].filename = (char *)Xcalloc(BMAX_PATH, sizeof(uint8_t));
2695 helixhorne 1829
            if (MapInfo[mii].name == NULL)
4491 helixhorne 1830
                MapInfo[mii].name = Xstrdup("User Map");
385 terminx 1831
        }
1832
        else
1833
        {
2695 helixhorne 1834
            OSD_Printf(OSDTEXT_RED "Map E%dL%d not defined!\n", ud.volume_number+1, ud.level_number+1);
385 terminx 1835
            return 1;
1836
        }
382 terminx 1837
    }
1838
 
1595 helixhorne 1839
    i = ud.screen_size;
1840
    ud.screen_size = 0;
5 Plagman 1841
 
1754 helixhorne 1842
    G_DoLoadScreen("Loading map . . .", -1);
1595 helixhorne 1843
    G_UpdateScreenArea();
1754 helixhorne 1844
 
1595 helixhorne 1845
    ud.screen_size = i;
1536 helixhorne 1846
 
4214 helixhorne 1847
    if (G_HaveUserMap())
382 terminx 1848
    {
1849
        Bstrcpy(levname, boardfilename);
4516 hendricks2 1850
        if (g_gameNamePtr)
1851
            Bsprintf(apptitle,"%s - %s - " APPNAME,levname,g_gameNamePtr);
1852
        else
1853
            Bsprintf(apptitle,"%s - " APPNAME,levname);
382 terminx 1854
    }
4516 hendricks2 1855
    else
1856
    {
1857
        if (g_gameNamePtr)
1858
            Bsprintf(apptitle,"%s - %s - " APPNAME,MapInfo[mii].name,g_gameNamePtr);
1859
        else
1860
            Bsprintf(apptitle,"%s - " APPNAME,MapInfo[mii].name);
1861
    }
382 terminx 1862
 
886 terminx 1863
    Bstrcpy(tempbuf,apptitle);
385 terminx 1864
    wm_setapptitle(tempbuf);
1754 helixhorne 1865
 
4214 helixhorne 1866
    /***** Load the map *****/
335 terminx 1867
    {
4214 helixhorne 1868
        DukePlayer_t *ps = g_player[0].ps;
1869
 
1870
        if (!VOLUMEONE && G_HaveUserMap())
5 Plagman 1871
        {
4214 helixhorne 1872
            if (loadboard(boardfilename, 0, &ps->pos, &ps->ang, &ps->cursectnum) < 0)
5 Plagman 1873
            {
2694 helixhorne 1874
                OSD_Printf(OSD_ERROR "Map \"%s\" not found or invalid map version!\n",boardfilename);
5 Plagman 1875
                return 1;
335 terminx 1876
            }
423 terminx 1877
 
2202 helixhorne 1878
            G_LoadMapHack(levname, boardfilename);
1879
            G_SetupFilenameBasedMusic(levname, boardfilename, ud.m_level_number);
5 Plagman 1880
        }
4214 helixhorne 1881
        else if (loadboard(MapInfo[mii].filename, VOLUMEONE, &ps->pos, &ps->ang, &ps->cursectnum) < 0)
5 Plagman 1882
        {
2694 helixhorne 1883
            OSD_Printf(OSD_ERROR "Map \"%s\" not found or invalid map version!\n",
2695 helixhorne 1884
                       MapInfo[mii].filename);
5 Plagman 1885
            return 1;
335 terminx 1886
        }
1887
        else
1888
        {
2695 helixhorne 1889
            G_LoadMapHack(levname, MapInfo[mii].filename);
5 Plagman 1890
        }
335 terminx 1891
    }
2694 helixhorne 1892
 
1595 helixhorne 1893
    g_precacheCount = 0;
1969 helixhorne 1894
    Bmemset(gotpic, 0, sizeof(gotpic));
1895
    Bmemset(precachehightile, 0, sizeof(precachehightile));
1595 helixhorne 1896
 
1625 terminx 1897
    //clearbufbyte(Actor,sizeof(Actor),0l); // JBF 20040531: yes? no?
5 Plagman 1898
 
1899
    prelevel(g);
1900
 
3010 helixhorne 1901
    G_AlignWarpElevators();
5 Plagman 1902
    resetpspritevars(g);
1903
 
4968 hendricks2 1904
    ud.playerbest = CONFIG_GetMapBestTime(MapInfo[mii].filename);
1905
 
4978 hendricks2 1906
    G_FadeLoad(0,0,0, 63,0, -7, 4, -1);
1595 helixhorne 1907
    G_CacheMapData();
4978 hendricks2 1908
    G_FadeLoad(0,0,0, 0,63, 7, 4, -2);
5 Plagman 1909
 
331 terminx 1910
    if (ud.recstat != 2)
5 Plagman 1911
    {
2695 helixhorne 1912
        g_musicIndex = mii;
2738 terminx 1913
        if (MapInfo[g_musicIndex].musicfn != NULL)
4948 hendricks2 1914
            S_PlayMusic(MapInfo[g_musicIndex].musicfn);
5 Plagman 1915
    }
1916
 
1673 terminx 1917
    if (g & (MODE_GAME|MODE_EOL))
1440 terminx 1918
    {
2379 helixhorne 1919
        for (TRAVERSE_CONNECT(i))
4776 hendricks2 1920
        {
2379 helixhorne 1921
            g_player[i].ps->gm = MODE_GAME;
4776 hendricks2 1922
            M_CloseMenu(i);
1923
        }
1440 terminx 1924
    }
1673 terminx 1925
    else if (g & MODE_RESTART)
5 Plagman 1926
    {
331 terminx 1927
        if (ud.recstat == 2)
564 terminx 1928
            g_player[myconnectindex].ps->gm = MODE_DEMO;
1929
        else g_player[myconnectindex].ps->gm = MODE_GAME;
5 Plagman 1930
    }
1931
 
333 terminx 1932
    if ((ud.recstat == 1) && (g&MODE_RESTART) != MODE_RESTART)
1143 terminx 1933
        G_OpenDemoWrite();
5 Plagman 1934
 
4852 hendricks2 1935
#ifndef EDUKE32_TOUCH_DEVICES
1673 terminx 1936
    if (VOLUMEONE && ud.level_number == 0 && ud.recstat != 2)
1802 terminx 1937
        P_DoQuote(QUOTE_F1HELP,g_player[myconnectindex].ps);
4612 terminx 1938
#endif
5 Plagman 1939
 
2379 helixhorne 1940
    for (TRAVERSE_CONNECT(i))
1941
        switch (DYNAMICTILEMAP(sector[sprite[g_player[i].ps->i].sectnum].floorpicnum))
1942
        {
1943
        case HURTRAIL__STATIC:
1944
        case FLOORSLIME__STATIC:
1945
        case FLOORPLASMA__STATIC:
1946
            P_ResetWeapons(i);
1947
            P_ResetInventory(i);
1948
            g_player[i].ps->gotweapon &= ~(1<<PISTOL_WEAPON);
1949
            g_player[i].ps->ammo_amount[PISTOL_WEAPON] = 0;
1950
            g_player[i].ps->curr_weapon = KNEE_WEAPON;
1951
            g_player[i].ps->kickback_pic = 0;
1952
            break;
1953
        }
5 Plagman 1954
 
1955
    //PREMAP.C - replace near the my's at the end of the file
1956
 
3270 terminx 1957
    Net_NotifyNewGame();
1143 terminx 1958
    Net_ResetPrediction();
5 Plagman 1959
 
564 terminx 1960
    //g_player[myconnectindex].ps->palette = palette;
1143 terminx 1961
    //G_FadePalette(0,0,0,0);
1772 plagman 1962
    P_SetGamePalette(g_player[myconnectindex].ps, BASEPAL, 0);    // JBF 20040308
5 Plagman 1963
 
1595 helixhorne 1964
    P_UpdateScreenPal(g_player[myconnectindex].ps);
1965
    flushperms();
5 Plagman 1966
 
1967
    everyothertime = 0;
1143 terminx 1968
    g_globalRandom = 0;
5 Plagman 1969
 
1970
    ud.last_level = ud.level_number+1;
1971
 
1802 terminx 1972
    G_ClearFIFO();
5 Plagman 1973
 
1229 terminx 1974
    for (i=g_numInterpolations-1; i>=0; i--) bakipos[i] = *curipos[i];
5 Plagman 1975
 
2306 helixhorne 1976
    g_restorePalette = -1;
5 Plagman 1977
 
1595 helixhorne 1978
    G_FadePalette(0,0,0,0);
1979
    G_UpdateScreenArea();
1980
    clearview(0L);
1981
    G_DrawBackground();
1982
    G_DrawRooms(myconnectindex,65536);
5 Plagman 1983
 
564 terminx 1984
    g_player[myconnectindex].ps->over_shoulder_on = 0;
5 Plagman 1985
 
1986
    clearfrags();
1987
 
3929 helixhorne 1988
    G_ResetTimers(0);  // Here we go
5 Plagman 1989
 
1143 terminx 1990
    //Bsprintf(g_szBuf,"G_EnterLevel L=%d V=%d",ud.level_number, ud.volume_number);
5 Plagman 1991
    //AddLog(g_szBuf);
1992
    // variables are set by pointer...
317 terminx 1993
 
2695 helixhorne 1994
    Bmemcpy(currentboardfilename, boardfilename, BMAX_PATH);
4745 terminx 1995
    VM_OnEvent(EVENT_ENTERLEVEL, -1, -1);
2695 helixhorne 1996
    OSD_Printf(OSDTEXT_YELLOW "E%dL%d: %s\n", ud.volume_number+1, ud.level_number+1,
1997
               MapInfo[mii].name);
1857 terminx 1998
 
1999
    Net_WaitForServer();
5 Plagman 2000
    return 0;
2001
}
862 terminx 2002
 
1205 terminx 2003
void G_FreeMapState(int32_t mapnum)
862 terminx 2004
{
3791 helixhorne 2005
    map_t *mapinfo = &MapInfo[mapnum];
3788 helixhorne 2006
#if !defined LUNATIC
1205 terminx 2007
    int32_t j;
3791 helixhorne 2008
#endif
2009
    if (mapinfo->savedstate == NULL)
2010
        return;
2011
 
2012
#if !defined LUNATIC
1229 terminx 2013
    for (j=0; j<g_gameVarCount; j++)
862 terminx 2014
    {
1143 terminx 2015
        if (aGameVars[j].dwFlags & GAMEVAR_NORESET) continue;
3791 helixhorne 2016
        if (aGameVars[j].dwFlags & (GAMEVAR_PERPLAYER|GAMEVAR_PERACTOR))
862 terminx 2017
        {
3791 helixhorne 2018
            if (mapinfo->savedstate->vars[j])
4923 terminx 2019
                Baligned_free(mapinfo->savedstate->vars[j]);
862 terminx 2020
        }
2021
    }
4118 helixhorne 2022
#else
2023
    Bfree(mapinfo->savedstate->savecode);
3415 helixhorne 2024
#endif
4923 terminx 2025
    Baligned_free(mapinfo->savedstate);
3791 helixhorne 2026
    mapinfo->savedstate = NULL;
955 hnt_ts 2027
}