Subversion Repositories eduke32

Rev

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