summaryrefslogtreecommitdiff
path: root/src/strife/p_spec.c
blob: af070d55f48d90d5118a846875d552e7481d11d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
//	Implements special effects:
//	Texture animation, height or lighting changes
//	 according to adjacent sectors, respective
//	 utility functions, etc.
//	Line Tag handling. Line and Sector triggers.
//
//-----------------------------------------------------------------------------


#include <stdlib.h>

#include "doomdef.h"
#include "doomstat.h"

#include "deh_main.h"
#include "i_system.h"
#include "z_zone.h"
#include "m_argv.h"
#include "m_misc.h"
#include "m_random.h"
#include "w_wad.h"

#include "r_local.h"
#include "p_local.h"

#include "g_game.h"

#include "s_sound.h"

// State.
#include "r_state.h"

// Data.
#include "sounds.h"

// [STRIFE]
#include "hu_stuff.h"
#include "p_dialog.h"


//
// Animating textures and planes
// There is another anim_t used in wi_stuff, unrelated.
//
typedef struct
{
    boolean	istexture;
    int		picnum;
    int		basepic;
    int		numpics;
    int		speed;
    
} anim_t;

//
//      source animation definition
//
typedef struct
{
    int 	istexture;	// if false, it is a flat
    char	endname[9];
    char	startname[9];
    int		speed;
} animdef_t;


// haleyjd 08/30/10: [STRIFE] MAXANIMS raised from 32 to 40
#define MAXANIMS                40

//
// P_InitPicAnims
//

// Floor/ceiling animation sequences,
//  defined by first and last frame,
//  i.e. the flat (64x64 tile) name to
//  be used.
// The full animation sequence is given
//  using all the flats between the start
//  and end entry, in the order found in
//  the WAD file.
//
// haleyjd 08/29/10: [STRIFE] Changed animdefs.
//
animdef_t               animdefs[] =
{
    { false, "F_SCANR8", "F_SCANR5",  4},
    { false, "F_WATR03", "F_WATR01",  8},
    { false, "F_PWATR3", "F_PWATR1", 11},
    { false, "F_SCANR4", "F_SCANR1",  4},
    { true,  "SCAN08",   "SCAN05",    4},
    { true,  "SWTRMG03", "SWTRMG01",  4},
    { true,  "SCAN04",   "SCAN01",    4},
    { true,  "COMP04",   "COMP01",    4},
    { true,  "COMP08",   "COMP05",    6},
    { true,  "COMP12",   "COMP09",   11},
    { true,  "COMP16",   "COMP13",   12},
    { true,  "COMP20",   "COMP17",   12},
    { true,  "COMP24",   "COMP21",   12},
    { true,  "COMP28",   "COMP25",   12},
    { true,  "COMP32",   "COMP29",   12},
    { true,  "COMP37",   "COMP33",   12},
    { true,  "COMP41",   "COMP38",   12},
    { true,  "COMP49",   "COMP42",   10},
    { true,  "BRKGRY16", "BRKGRY13", 10},
    { true,  "BRNSCN04", "BRNSCN01", 10},
    { true,  "CONCRT12", "CONCRT09", 11},
    { true,  "CONCRT25", "CONCRT22", 11},
    { true,  "WALPMP02", "WALPMP01", 16},
    { true,  "WALTEK17", "WALTEK16",  8},
    { true,  "FORCE04",  "FORCE01",   4},
    { true,  "FORCE08",  "FORCE05",   4},
    { true,  "FAN02",    "FAN01",     4},
    { false, "F_VWATR3", "P_VWATR1",  4},
    { false, "F_HWATR3", "F_HWATR1",  4},
    { false, "F_TELE2",  "F_TELE1",   4},
    { false, "F_FAN2",   "F_FAN1",    4},
    { false, "F_CONVY2", "F_CONVY1",  4},
    { false, "F_RDALN4", "F_RDALN1",  4},
    { -1,    "",         "",          0},
};

anim_t  anims[MAXANIMS];
anim_t* lastanim;

//
//      Animating line specials
//
// haleyjd 08/29/10: [STRIFE] MAXLINEANIMS raised from 64 to 96
#define MAXLINEANIMS            96

extern  short   numlinespecials;
extern  line_t* linespeciallist[MAXLINEANIMS];



void P_InitPicAnims (void)
{
    int		i;

    
    //	Init animation
    lastanim = anims;
    for (i=0 ; animdefs[i].istexture != -1 ; i++)
    {
        char *startname, *endname;

        startname = DEH_String(animdefs[i].startname);
        endname = DEH_String(animdefs[i].endname);

        if (animdefs[i].istexture)
        {
            // different episode ?
            if (R_CheckTextureNumForName(startname) == -1)
                continue;	

            lastanim->picnum = R_TextureNumForName(endname);
            lastanim->basepic = R_TextureNumForName(startname);
        }
        else
        {
            if (W_CheckNumForName(startname) == -1)
                continue;

            lastanim->picnum = R_FlatNumForName(endname);
            lastanim->basepic = R_FlatNumForName(startname);
        }

        lastanim->istexture = animdefs[i].istexture;
        lastanim->numpics = lastanim->picnum - lastanim->basepic + 1;

        if (lastanim->numpics < 2)
            I_Error ("P_InitPicAnims: bad cycle from %s to %s",
            startname, endname);

        lastanim->speed = animdefs[i].speed;
        lastanim++;
    }

}

// villsa [STRIFE] terrain type definitions
typedef struct
{
    char*   flat;
    int     type;
    int     num;
} terraintype_t;

terraintype_t   terraintypes[] =
{
    {   "F_WATR03", FLOOR_WATER,    -1  },
    {   "F_WATR02", FLOOR_WATER,    -1  },
    {   "F_WATR01", FLOOR_WATER,    -1  },
    {   "F_VWATR3", FLOOR_WATER,    -1  },
    {   "F_VWATR2", FLOOR_WATER,    -1  },
    {   "P_VWATR1", FLOOR_WATER,    -1  },
    {   "F_HWATR3", FLOOR_WATER,    -1  },
    {   "F_HWATR2", FLOOR_WATER,    -1  },
    {   "F_HWATR1", FLOOR_WATER,    -1  },
    {   "F_PWATR3", FLOOR_SLIME,    -1  },
    {   "F_PWATR2", FLOOR_SLIME,    -1  },
    {   "F_PWATR1", FLOOR_SLIME,    -1  },
    {   "END",      FLOOR_END,      -1  },
};

//
// P_GetTerrainType
// villsa [STRIFE] new function
//

terraintype_e P_GetTerrainType(mobj_t* mobj)
{
    int i = 0;
    subsector_t* ss = mobj->subsector;

    if(mobj->z <= ss->sector->floorheight &&
        terraintypes[0].type != FLOOR_END)
    {
        while(ss->sector->floorpic != terraintypes[i].num)
        {
            if(terraintypes[i+1].type == FLOOR_END)
                return FLOOR_SOLID;

            i++;
        }

        return terraintypes[i].type;
    }

    return FLOOR_SOLID;
}

//
// P_InitTerrainTypes
// villsa [STRIFE] new function
// Initialize terrain types
//

void P_InitTerrainTypes(void)
{
    int pic = 0;
    int i = 0;

    if(terraintypes[0].type != FLOOR_END)
    {
        while(terraintypes[i].type != FLOOR_END)
        {
            terraintypes[i].num = R_FlatNumForName(terraintypes[i].flat);
            i++;
        }
    }
}



//
// UTILITIES
//



//
// getSide()
// Will return a side_t*
//  given the number of the current sector,
//  the line number, and the side (0/1) that you want.
//
side_t*
getSide
( int		currentSector,
  int		line,
  int		side )
{
    return &sides[ (sectors[currentSector].lines[line])->sidenum[side] ];
}


//
// getSector()
// Will return a sector_t*
//  given the number of the current sector,
//  the line number and the side (0/1) that you want.
//
sector_t*
getSector
( int		currentSector,
  int		line,
  int		side )
{
    return sides[ (sectors[currentSector].lines[line])->sidenum[side] ].sector;
}


//
// twoSided()
// Given the sector number and the line number,
//  it will tell you whether the line is two-sided or not.
//
int
twoSided
( int	sector,
  int	line )
{
    return (sectors[sector].lines[line])->flags & ML_TWOSIDED;
}




//
// getNextSector()
// Return sector_t * of sector next to current.
// NULL if not two-sided line
//
sector_t*
getNextSector
( line_t*	line,
  sector_t*	sec )
{
    if (!(line->flags & ML_TWOSIDED))
	return NULL;
		
    if (line->frontsector == sec)
	return line->backsector;
	
    return line->frontsector;
}



//
// P_FindLowestFloorSurrounding()
// FIND LOWEST FLOOR HEIGHT IN SURROUNDING SECTORS
//
fixed_t	P_FindLowestFloorSurrounding(sector_t* sec)
{
    int			i;
    line_t*		check;
    sector_t*		other;
    fixed_t		floor = sec->floorheight;
	
    for (i=0 ;i < sec->linecount ; i++)
    {
	check = sec->lines[i];
	other = getNextSector(check,sec);

	if (!other)
	    continue;
	
	if (other->floorheight < floor)
	    floor = other->floorheight;
    }
    return floor;
}



//
// P_FindHighestFloorSurrounding()
// FIND HIGHEST FLOOR HEIGHT IN SURROUNDING SECTORS
//
fixed_t	P_FindHighestFloorSurrounding(sector_t *sec)
{
    int			i;
    line_t*		check;
    sector_t*		other;
    fixed_t		floor = -500*FRACUNIT;
	
    for (i=0 ;i < sec->linecount ; i++)
    {
	check = sec->lines[i];
	other = getNextSector(check,sec);
	
	if (!other)
	    continue;
	
	if (other->floorheight > floor)
	    floor = other->floorheight;
    }
    return floor;
}



//
// P_FindNextHighestFloor
// FIND NEXT HIGHEST FLOOR IN SURROUNDING SECTORS
// Note: this should be doable w/o a fixed array.

// Thanks to entryway for the Vanilla overflow emulation.

// 20 adjoining sectors max!
#define MAX_ADJOINING_SECTORS     20

fixed_t
P_FindNextHighestFloor
( sector_t* sec,
  int       currentheight )
{
    int         i;
    int         h;
    int         min;
    line_t*     check;
    sector_t*   other;
    fixed_t     height = currentheight;
    fixed_t     heightlist[MAX_ADJOINING_SECTORS + 2];

    for (i=0, h=0; i < sec->linecount; i++)
    {
        check = sec->lines[i];
        other = getNextSector(check,sec);

        if (!other)
            continue;
        
        if (other->floorheight > height)
        {
            // Emulation of memory (stack) overflow
            if (h == MAX_ADJOINING_SECTORS + 1)
            {
                height = other->floorheight;
            }
            else if (h == MAX_ADJOINING_SECTORS + 2)
            {
                // Fatal overflow: game crashes at 22 textures
                I_Error("Sector with more than 22 adjoining sectors. "
                        "Vanilla will crash here");
            }

            heightlist[h++] = other->floorheight;
        }
    }
    
    // Find lowest height in list
    if (!h)
    {
        return currentheight;
    }
        
    min = heightlist[0];
    
    // Range checking? 
    for (i = 1; i < h; i++)
    {
        if (heightlist[i] < min)
        {
            min = heightlist[i];
        }
    }

    return min;
}

//
// FIND LOWEST CEILING IN THE SURROUNDING SECTORS
//
fixed_t
P_FindLowestCeilingSurrounding(sector_t* sec)
{
    int			i;
    line_t*		check;
    sector_t*		other;
    fixed_t		height = INT_MAX;
	
    for (i=0 ;i < sec->linecount ; i++)
    {
	check = sec->lines[i];
	other = getNextSector(check,sec);

	if (!other)
	    continue;

	if (other->ceilingheight < height)
	    height = other->ceilingheight;
    }
    return height;
}


//
// FIND HIGHEST CEILING IN THE SURROUNDING SECTORS
//
fixed_t	P_FindHighestCeilingSurrounding(sector_t* sec)
{
    int		i;
    line_t*	check;
    sector_t*	other;
    fixed_t	height = 0;
	
    for (i=0 ;i < sec->linecount ; i++)
    {
	check = sec->lines[i];
	other = getNextSector(check,sec);

	if (!other)
	    continue;

	if (other->ceilingheight > height)
	    height = other->ceilingheight;
    }
    return height;
}



//
// RETURN NEXT SECTOR # THAT LINE TAG REFERS TO
//
int
P_FindSectorFromLineTag
( line_t*	line,
  int		start )
{
    int	i;
	
    for (i=start+1;i<numsectors;i++)
	if (sectors[i].tag == line->tag)
	    return i;
    
    return -1;
}




//
// Find minimum light from an adjacent sector
//
int
P_FindMinSurroundingLight
( sector_t*	sector,
  int		max )
{
    int		i;
    int		min;
    line_t*	line;
    sector_t*	check;
	
    min = max;
    for (i=0 ; i < sector->linecount ; i++)
    {
	line = sector->lines[i];
	check = getNextSector(line,sector);

	if (!check)
	    continue;

	if (check->lightlevel < min)
	    min = check->lightlevel;
    }
    return min;
}



//
// EVENTS
// Events are operations triggered by using, crossing,
// or shooting special lines, or by timed thinkers.
//

// [STRIFE]
static char crosslinestr[90];

//
// P_CrossSpecialLine - TRIGGER
// Called every time a thing origin is about
//  to cross a line with a non 0 special.
//
void
P_CrossSpecialLine
( int           linenum,
  int           side,
  mobj_t*       thing )
{
    line_t*     line;
    side_t*     sidedef; // [STRIFE]
    int         flag;    // [STRIFE]
    int         ok;

    line = &lines[linenum];

    // haleyjd 09/21/10: corpses and missiles cannot activate any cross-over
    // line types, *except* 182 (which is for the sake of missiles).
    if((thing->flags & (MF_MISSILE|MF_CORPSE)) && line->special != 182)
        return;

    //	Triggers that other things can activate
    if (!thing->player)
    {
        // Things that should NOT trigger specials...
        // villsa [STRIFE] unused
        //   haleyjd: removed dead switch. Strife only excludes missiles and
        //   corpses, which is handled above.
 
        ok = 0;

        // [STRIFE] Added several line types. Removed none.
        switch(line->special)
        {
        case 97:        // TELEPORT RETRIGGER
        case 185:       // haleyjd: STRIFE-TODO: Identify type
        case 195:       // haleyjd: STRIFE-TODO: Identify type
        case 231:       // haleyjd: STRIFE-TODO: Identify type
        case 125:       // TELEPORT MONSTERONLY TRIGGER
        case 126:       // TELEPORT MONSTERONLY RETRIGGER
        case 182:       // haleyjd: [STRIFE] Break glass - it's a W1 type too!
        case 10:        // PLAT DOWN-WAIT-UP-STAY TRIGGER
        case 39:        // TELEPORT TRIGGER
        case 88:        // PLAT DOWN-WAIT-UP-STAY RETRIGGER
        case 4:         // RAISE DOOR
            ok = 1;
            break;
        }
        if (!ok)
            return;
    }

    
    // Note: could use some const's here.
    switch (line->special)
    {
    //
    // TRIGGERS.
    // All from here to RETRIGGERS.
    //
    case 230:
        // haleyjd 09/21/10: [STRIFE] W1 Open Door if Quest
        sidedef = &sides[line->sidenum[0]];
        flag = (sidedef->rowoffset >> FRACBITS) - 1;

        if(!(thing->player->questflags & (1 << flag)))
            break;
        // fall-through:
    case 2:
        // Open Door - [STRIFE] Verified unmodified.
        EV_DoDoor(line,open);
        line->special = 0;
        break;

    case 227:
        // haleyjd 09/21/10: [STRIFE] W1 Close Door if Quest
        sidedef = &sides[line->sidenum[0]];
        flag = (sidedef->rowoffset >> FRACBITS) - 1;

        if(!(thing->player->questflags & (1 << flag)))
            break;
        // fall-through:
    case 3:
        // Close Door - [STRIFE] Verified unmodified.
        EV_DoDoor(line,close);
        line->special = 0;
        break;

    case 4:
        // Raise Door - [STRIFE] Verified unmodified.
        EV_DoDoor(line,normal);
        line->special = 0;
        break;

    case 5:
        // Raise Floor - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloor);
        line->special = 0;
        break;

    case 6:
        // Fast Ceiling Crush & Raise - [STRIFE] Verified unmodified.
        EV_DoCeiling(line,fastCrushAndRaise);
        line->special = 0;
        break;

    case 8:
        // Build Stairs - [STRIFE] Verified unmodified.
        EV_BuildStairs(line,build8);
        line->special = 0;
        break;

    case 10:
        // PlatDownWaitUp - [STRIFE] Verified unmodified.
        EV_DoPlat(line,downWaitUpStay,0);
        line->special = 0;
        break;

    case 12:
        // Light Turn On - brightest near - [STRIFE] Verified unmodified.
        EV_LightTurnOn(line,0);
        line->special = 0;
        break;

    case 13:
        // Light Turn On 255 - [STRIFE] Verified unmodified.
        EV_LightTurnOn(line,255);
        line->special = 0;
        break;

    case 16:
        // Close Door 30 - [STRIFE] Verified unmodified.
        EV_DoDoor(line,close30ThenOpen);
        line->special = 0;
        break;

    case 17:
        // Start Light Strobing - [STRIFE] Verified unmodified.
        EV_StartLightStrobing(line);
        line->special = 0;
        break;

    case 19:
        // Lower Floor - [STRIFE] Verified unmodified.
        EV_DoFloor(line,lowerFloor);
        line->special = 0;
        break;

    case 22:
        // villsa [STRIFE] Verified unmodified.
        // Raise floor to nearest height and change texture
        EV_DoPlat(line,raiseToNearestAndChange,0);
        line->special = 0;
        break;

    case 25:
        // Ceiling Crush and Raise - [STRIFE] Verified unmodified.
        EV_DoCeiling(line,crushAndRaise);
        line->special = 0;
        break;

    case 30:
        // Raise floor to shortest texture height - [STRIFE] Verified unmodified.
        //  on either side of lines.
        EV_DoFloor(line,raiseToTexture);
        line->special = 0;
        break;

    case 35:
        // Lights Very Dark - [STRIFE] Verified unmodified.
        EV_LightTurnOn(line,35);
        line->special = 0;
        break;

    case 36:
        // Lower Floor (TURBO) - [STRIFE] Verified unmodified.
        EV_DoFloor(line,turboLower);
        line->special = 0;
        break;

    case 37:
        // LowerAndChange - [STRIFE] Verified unmodified.
        EV_DoFloor(line,lowerAndChange);
        line->special = 0;
        break;

    case 193:
        // haleyjd 09/21/10: [STRIFE] W1 Floor Lower to Lowest if Quest
        sidedef = &sides[line->sidenum[0]];
        flag = (sidedef->rowoffset >> FRACBITS) - 1; // note is fixed_t

        // must have the questflag indicated in the line's y offset
        if(!(thing->player->questflags & (1 << flag)))
            break;
        // fall-through:
    case 38: 
        // Lower Floor To Lowest - [STRIFE] Verified unmodified.
        EV_DoFloor( line, lowerFloorToLowest );
        line->special = 0;
        break;

    case 39:
        // TELEPORT! - [STRIFE] Verified unmodified (except for 0 flags param)
        EV_Teleport( line, side, thing, TF_NORMAL );
        line->special = 0;
        break;

        /*case 40:
        // RaiseCeilingLowerFloor
        EV_DoCeiling( line, raiseToHighest );
        EV_DoFloor( line, lowerFloorToLowest );
        line->special = 0;
        break;*/

    case 44:
        // Ceiling Crush - [STRIFE] Verified unmodified.
        EV_DoCeiling( line, lowerAndCrush );
        line->special = 0;
        break;

    case 52:
        // EXIT! - haleyjd 09/21/10: [STRIFE] Exit to level tag/100
        G_ExitLevel (line->tag / 100);
        break;

    case 53:
        // Perpetual Platform Raise - [STRIFE] Verified unmodified.
        EV_DoPlat(line,perpetualRaise,0);
        line->special = 0;
        break;

    case 54:
        // Platform Stop - [STRIFE] Verified unmodified.
        EV_StopPlat(line);
        line->special = 0;
        break;

    case 56:
        // Raise Floor Crush - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloorCrush);
        line->special = 0;
        break;

    case 57:
        // Ceiling Crush Stop - [STRIFE] Verified unmodified.
        EV_CeilingCrushStop(line);
        line->special = 0;
        break;

    case 58:
        // [STRIFE] raiseFloor24 was modified into raiseFloor64
        // Raise Floor 64 
        EV_DoFloor(line,raiseFloor64);
        line->special = 0;
        break;

    case 59:
        // Raise Floor 24 And Change - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloor24AndChange);
        line->special = 0;
        break;

    case 104:
        // Turn lights off in sector(tag) - [STRIFE] Verified unmodified.
        EV_TurnTagLightsOff(line);
        line->special = 0;
        break;

    case 108:
        // Blazing Door Raise (faster than TURBO!) - [STRIFE] Verified unmodified.
        EV_DoDoor (line,blazeRaise);
        line->special = 0;
        break;

    case 109:
        // Blazing Door Open (faster than TURBO!) - [STRIFE] Verified unmodified.
        EV_DoDoor (line,blazeOpen);
        line->special = 0;
        break;

    case 100:
        // Build Stairs Turbo 16 - [STRIFE] Verified unmodified.
        EV_BuildStairs(line,turbo16);
        line->special = 0;
        break;

    case 197:
        // haleyjd 09/21/10: [STRIFE] Blazing Door Close if Has Sigil B
        if(thing->player->sigiltype <= 0)
            break;
        // fall-through:
    case 110:
        // Blazing Door Close (faster than TURBO!) - [STRIFE] Verified unmodified.
        EV_DoDoor (line,blazeClose);
        line->special = 0;
        break;

    case 119:
        // Raise floor to nearest surr. floor - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloorToNearest);
        line->special = 0;
        break;

    case 121:
        // villsa [STRIFE] Verified unmodified.
        // Blazing PlatDownWaitUpStay
        EV_DoPlat(line,blazeDWUS,0);
        line->special = 0;
        break;

    case 124:
        // haleyjd 09/21/10: [STRIFE] W1 Start Finale
        // Altered from G_SecretExitLevel.
        G_StartFinale();
        break;

    case 125:
        // TELEPORT MonsterONLY - [STRIFE] Verified unmodified
        //    (except for 0 flags parameter)
        if (!thing->player)
        {
            EV_Teleport( line, side, thing, TF_NORMAL );
            line->special = 0;
        }
        break;

    case 130:
        // Raise Floor Turbo - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloorTurbo);
        line->special = 0;
        break;

    case 141:
        // Silent Ceiling Crush & Raise - [STRIFE] Verified unmodified.
        EV_DoCeiling(line,silentCrushAndRaise);
        line->special = 0;
        break;

    case 174:
        // villsa [STRIFE] Split Open
        EV_DoDoor(line, splitOpen);
        line->special = 0;
        break;

    case 183:
        // villsa [STRIFE] Split Raise Nearest
        EV_DoDoor(line, splitRaiseNearest);
        line->special = 0;
        break;

    case 178: 
        // haleyjd 09/24/10: [STRIFE] W1 Build Stairs Down 16
        EV_BuildStairs(line, buildDown16);
        line->special = 0;
        break;

    case 179: 
        // haleyjd 09/25/10: [STRIFE] W1 Ceiling Lower to Floor
        EV_DoCeiling(line, lowerToFloor);
        line->special = 0;
        break;

    case 182:
        // haleyjd 09/21/10: [STRIFE] Break Glass
        // 182 is a unique linetype in that it is both a G1 and a W1 linetype,
        // but only missiles may activate it as a W1 type.
        if(thing->flags & MF_MISSILE)
            P_ChangeSwitchTexture(line, 1); // why 1? it will be cleared anyway.
        break;

    case 187:
        // haleyjd 09/21/10: [STRIFE] W1 Clear Force Fields if Quest
        sidedef = &sides[line->sidenum[0]];
        flag = (sidedef->rowoffset >> FRACBITS) - 1; // note is fixed_t

        // must have the questflag indicated in the line's y offset
        if(!(thing->player->questflags & (1 << flag)))
            break;

        // Do it!
        EV_ClearForceFields(line);
        line->special = 0;
        break;

    case 188:
        // haleyjd 09/21/10: [STRIFE] W1 Open Door if Quest 16 (Gate Mechanism 
        // Destroyed)
        if(!(thing->player->questflags & QF_QUEST16))
            break;
        EV_DoDoor(line, open);
        line->special = 0;
        break;

    case 196:
        // haleyjd 09/26/10: [STRIFE] W1 Floor Lower to Lowest if Sigil Type > 0
        if(thing->player->sigiltype > 0)
        {
            EV_DoFloor(line, lowerFloorToLowest);
            line->special = 0;
        }
        break;

    case 200:
        // haleyjd 09/21/10: [STRIFE] W1 Open Door if Sigil Owned
        if(!(thing->player->weaponowned[wp_sigil]))
            break;
        EV_DoDoor(line, open);
        line->special = 0;
        break;

    case 201:
        // haleyjd 09/21/10: [STRIFE] W1 Voiced Objective (First Side Only)
        if(side == 1)
            break;
        // fall-through:
    case 202:
        // haleyjd 09/21/10: [STRIFE] W1 Voiced Objective (Tag = VOC/LOG #)
        // must be consoleplayer
        if(thing->player != &players[consoleplayer])
            break;

        // must have comm unit
        if(!(thing->player->powers[pw_communicator]))
            break;

        // load voice
        DEH_snprintf(crosslinestr, sizeof(crosslinestr), "voc%i", line->tag);
        I_StartVoice(crosslinestr);

        // load objective
        DEH_snprintf(crosslinestr, sizeof(crosslinestr), "log%i", line->tag);
        GiveObjective(crosslinestr, 0);

        // Put up a message
        thing->player->message = DEH_String("Incoming Message...");
        line->special = 0;
        break;

    case 210:
        // haleyjd 09/21/10: [STRIFE] W1 Voiced Objective if Flamethrower????
        // I don't think this is actually used anywhere o_O
        // must be player 1...
        if(thing->player != &players[0])
            break;

        // must have comm unit
        if(!(thing->player->powers[pw_communicator]))
            break;

        // must have... the flamethrower?!
        if(!(thing->player->weaponowned[wp_flame]))
            break;

        // load voice
        DEH_snprintf(crosslinestr, sizeof(crosslinestr), "voc%i", line->tag);
        I_StartVoice(crosslinestr);

        // load objective
        DEH_snprintf(crosslinestr, sizeof(crosslinestr), "log%i", line->tag);
        GiveObjective(crosslinestr, 0);

        // Put up a message
        thing->player->message = DEH_String("Incoming Message from BlackBird...");
        line->special = 0;
        break;

    case 212:
        // haleyjd 09/25/10: [STRIFE] W1 Floor Lower to Lowest if Have Flamethrower
        if(thing->player->weaponowned[wp_flame])
        {
            EV_DoFloor(line, lowerFloorToLowest);
            line->special = 0;
        }
        break;

    case 215:
        // haleyjd 09/21/10: [STRIFE] W1 Voiced Objective if Quest (Tag/100, Tag%100)
        // must be player 1...
        if(thing->player != &players[0])
            break;

        // must have comm unit
        if(!(thing->player->powers[pw_communicator]))
            break;

        if(line->tag != 0)
        {
            // test for questflag
            if(!(thing->player->questflags & (1 << (line->tag % 100 - 1))))
                break;
        }

        // start voice
        DEH_snprintf(crosslinestr, sizeof(crosslinestr), "voc%i", line->tag/100);
        I_StartVoice(crosslinestr);

        // give objective
        DEH_snprintf(crosslinestr, sizeof(crosslinestr), "log%i", line->tag/100);
        GiveObjective(crosslinestr, 0);

        // Put up a message
        thing->player->message = DEH_String("Incoming Message from BlackBird...");
        line->special = 0;
        break;

    case 204:
        // haleyjd 09/21/10: [STRIFE] W1 Change Music (unused!)
        if(thing->player != &players[0])
            break;
        S_ChangeMusic(line->tag, 1);
        line->special = 0;
        break;

    case 228:
        // haleyjd 09/21/10: [STRIFE] W1 Entity Voice?
        if(!(thing->player->questflags & QF_QUEST24)) // Not killed Macil???
            break; // STRIFE-TODO: verify...

        if(!(thing->player->questflags & QF_QUEST28)) // ????? STRIFE-TODO
            I_StartVoice(DEH_String("voc128"));
        else
            I_StartVoice(DEH_String("voc130"));
        
        line->special = 0;
        break;

        //
        // RETRIGGERS.  All from here till end.
        //
    case 72:
        // Ceiling Crush - [STRIFE] Verified unmodified.
        EV_DoCeiling( line, lowerAndCrush );
        break;

    case 73:
        // Ceiling Crush and Raise - [STRIFE] Verified unmodified.
        EV_DoCeiling(line,crushAndRaise);
        break;

    case 74:
        // Ceiling Crush Stop - [STRIFE] Verified unmodified.
        EV_CeilingCrushStop(line);
        break;

    case 75:
        // Close Door - [STRIFE] Verified unmodified.
        EV_DoDoor(line,close);
        break;

    case 76:
        // Close Door 30 - [STRIFE] Verified unmodified.
        EV_DoDoor(line,close30ThenOpen);
        break;

    case 77:
        // Fast Ceiling Crush & Raise - [STRIFE] Verified unmodified.
        EV_DoCeiling(line,fastCrushAndRaise);
        break;

    case 79:
        // Lights Very Dark - [STRIFE] Verified unmodified.
        EV_LightTurnOn(line,35);
        break;

    case 80:
        // Light Turn On - brightest near - [STRIFE] Verified unmodified.
        EV_LightTurnOn(line,0);
        break;

    case 81:
        // Light Turn On 255 - [STRIFE] Verified unmodified.
        EV_LightTurnOn(line,255);
        break;

    case 82:
        // Lower Floor To Lowest - [STRIFE] Verified unmodified.
        EV_DoFloor( line, lowerFloorToLowest );
        break;

    case 83:
        // Lower Floor - [STRIFE] Verified unmodified.
        EV_DoFloor(line,lowerFloor);
        break;

    case 84:
        // LowerAndChange - [STRIFE] Verified unmodified.
        EV_DoFloor(line,lowerAndChange);
        break;

    case 86:
        // Open Door - [STRIFE] Verified unmodified.
        EV_DoDoor(line,open);
        break;

    case 87:
        // Perpetual Platform Raise - [STRIFE] Verified unmodified.
        EV_DoPlat(line,perpetualRaise,0);
        break;

    case 88:
        // PlatDownWaitUp - [STRIFE] Verified unmodified.
        EV_DoPlat(line,downWaitUpStay,0);
        break;

    case 89:
        // Platform Stop - [STRIFE] Verified unmodified.
        EV_StopPlat(line);
        break;

    case 216:
        // haleyjd 09/21/10: [STRIFE] WR Raise Door if Quest
        sidedef = &sides[line->sidenum[0]];
        flag = (sidedef->rowoffset >> FRACBITS) - 1; // note is fixed_t.

        if(!(thing->player->questflags & (1 << flag)))
            break;
        // fall-through:
    case 90: 
        // Raise Door - [STRIFE] Verified unmodified.
        EV_DoDoor(line,normal);
        break;

    case 91:
        // Raise Floor - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloor);
        break;

    case 92:
        // [STRIFE] raiseFloor24 changed to raiseFloor64
        // Raise Floor 64
        EV_DoFloor(line,raiseFloor64);
        break;

    case 93:
        // Raise Floor 24 And Change - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloor24AndChange);
        break;

    case 94:
        // Raise Floor Crush - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloorCrush);
        break;

    case 95:
        // villsa [STRIFE] Verified unmodified.
        // Raise floor to nearest height
        // and change texture.
        EV_DoPlat(line,raiseToNearestAndChange,0);
        break;

    case 96:
        // Raise floor to shortest texture height - [STRIFE] Verified unmodified.
        // on either side of lines.
        EV_DoFloor(line,raiseToTexture);
        break;

    case 97:
        // TELEPORT! - [STRIFE] Verified unmodified (except for 0 flags param)
        EV_Teleport( line, side, thing, TF_NORMAL );
        break;

    case 98:
        // Lower Floor (TURBO) - [STRIFE] Verified unmodified.
        EV_DoFloor(line,turboLower);
        break;

    case 105:
        // Blazing Door Raise (faster than TURBO!) - [STRIFE] Verified unmodified.
        EV_DoDoor (line,blazeRaise);
        break;

    case 106:
        // Blazing Door Open (faster than TURBO!) - [STRIFE] Verified unmodified.
        EV_DoDoor (line,blazeOpen);
        break;

    case 107:
        // Blazing Door Close (faster than TURBO!) - [STRIFE] Verified unmodified.
        EV_DoDoor (line,blazeClose);
        break;

    case 120:
        // villsa [STRIFE] Verified unmodified.
        // Blazing PlatDownWaitUpStay.
        EV_DoPlat(line,blazeDWUS,0);
        break;

    case 126:
        // TELEPORT MonsterONLY. - [STRIFE] Verified unmodified (except for 0 flags param)
        if (!thing->player)
            EV_Teleport( line, side, thing, TF_NORMAL );
        break;

    case 128:
        // Raise To Nearest Floor - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloorToNearest);
        break;

    case 129:
        // Raise Floor Turbo - [STRIFE] Verified unmodified.
        EV_DoFloor(line,raiseFloorTurbo);
        break;

    case 186:
        // haleyjd [STRIFE] Exit Level to Spot, First Side Only
        if(side == 1)
            break;
        // fall-through:
    case 145:
        // haleyjd [STRIFE] Exit Level to Spot
        thing->momx = thing->momy = thing->momz = 0;
        {
            int map  = line->tag / 100;
            int spot = line->tag % 100;

            if(thing->player->weaponowned[wp_sigil])
            {
                if(map == 3)
                    map = 30;
                else if(map == 7)
                    map = 10;
            }

            DEH_snprintf(crosslinestr, sizeof(crosslinestr), 
                         "Entering%s", 
                         DEH_String(mapnames[map - 1]) + 8);
            thing->player->message = crosslinestr;

            if(netgame && deathmatch)
            {
                if(levelTimer && levelTimeCount != 0)
                {
                    DEH_snprintf(crosslinestr, sizeof(crosslinestr), 
                                 "%d min left", 
                                 (levelTimeCount/TICRATE)/60);
                    break;
                }

                // raise switch from floor
                EV_DoFloor(line, raiseFloor64);
            }
            else
            {
                // normal single-player exit

                // BUG: Here is the opening for a flaming player to cross past
                // the exit line and hit a deathmatch switch ;) It's not so much
                // that this is incorrect, as that they forgot to add such a 
                // check to the other kind of exit lines too ;)
                if(thing->player->health <= 0)
                    break;

                G_RiftExitLevel(map, spot, thing->angle);
            }
        }
        break;

    case 175:
        // haleyjd 09/21/10: [STRIFE] WR Raise Alarm if < 16 Above Floor
        if(thing->z < thing->floorz + 16 * FRACUNIT)
            P_NoiseAlert(thing->player->mo, thing->player->mo);
        break;

    case 198:
        // haleyjd 09/21/10: [STRIFE] WR Raise Alarm if No Guard Uniform
        if(P_PlayerHasItem(thing->player, MT_QUEST_GUARD_UNIFORM))
            break;
        // fall-through:
    case 150:
        // haleyjd 09/21/10: [STRIFE] WR Raise Alarm
        P_NoiseAlert(thing->player->mo, thing->player->mo);
        break;

    case 208:
        // haleyjd 09/21/10: [STRIFE] WR Raise Alarm if Have Flamethrower
        // O_o - this is definitely unused. Was an entire flamethrower quest
        // cut out of the game before release?
        if(thing->player->weaponowned[wp_flame])
            P_NoiseAlert(thing->player->mo, thing->player->mo);
        break;

    case 206:
        // haleyjd 09/21/10: [STRIFE] WR Raise Alarm if Have Chalice
        // This *is* used, inside the Tavern in Tarnhill. Oddly there is also
        // one just randomly placed outside the entrance to the Power Station.
        if(P_PlayerHasItem(thing->player, MT_INV_CHALICE))
            P_NoiseAlert(thing->player->mo, thing->player->mo);
        break;

    case 184:
        // villsa [STRIFE] plat up wait down stay
        if(EV_DoPlat(line, upWaitDownStay, 0))
            P_ChangeSwitchTexture(line, 1); // In P_CrossSpecialLine? Copypasta error?
        break;

    case 185:
        // haleyjd 09/21/10: [STRIFE] Silent Teleport (used for Converter)
        EV_Teleport(line, side, thing, TF_FULLSILENCE);
        break;

    case 195:
        // haleyjd 09/21/10: [STRIFE] Silent Teleport and Change Zombie
        EV_Teleport(line, side, thing, TF_FULLSILENCE);
        P_SetMobjState(thing, S_AGRD_00); // 419
        break;

    case 203:
        // haleyjd 09/21/10: [STRIFE] WR Change Music
        if(thing->player != &players[0])
            break;
        S_ChangeMusic(line->tag, 1);
        break;

    case 231:
        // haleyjd 09/21/10: [STRIFE] WR Teleport (Silent at Source)
        EV_Teleport(line, side, thing, TF_SRCSILENCE);
        break;
        
        // haleyjd 09/21/10: Moved one-time-use lines up above with the others.
    }
}



//
// P_ShootSpecialLine - IMPACT SPECIALS
// Called when a thing shoots a special line.
//
void
P_ShootSpecialLine
( mobj_t*       thing,
  line_t*       line )
{
    int         ok;

    //	Impacts that other things can activate.
    if (!thing->player)
    {
        ok = 0;
        switch(line->special)
        {
        case 46:  // OPEN DOOR IMPACT
        case 182: // villsa [STRIFE] for windows
            ok = 1;
            break;
        }
        if (!ok)
            return;
    }

    switch(line->special)
    {
    case 24:
        // RAISE FLOOR - [STRIFE] Verified unmodified
        EV_DoFloor(line,raiseFloor);
        P_ChangeSwitchTexture(line,0);
        break;

    case 46:
        // OPEN DOOR - [STRIFE] Verified unmodified.
        EV_DoDoor(line,open);
        P_ChangeSwitchTexture(line,1);
        break;

    case 47:
        // villsa [STRIFE] Verified unmodified.
        // RAISE FLOOR NEAR AND CHANGE
        EV_DoPlat(line,raiseToNearestAndChange,0);
        P_ChangeSwitchTexture(line,0);
        break;

    case 180:
        // haleyjd 09/22/10: [STRIFE] G1 Raise Floor 512 & Change
        EV_DoFloor(line, raiseFloor512AndChange);
        P_ChangeSwitchTexture(line, 0);
        break;

    case 182:
        // villsa [STRIFE] G1 Break Glass
        //   haleyjd: note that 182 is also a W1 type in P_CrossSpecialLine, but
        //   can only be activated in that manner by an MF_MISSILE object.
        P_ChangeSwitchTexture(line, 0);
        break;
    }
}



//
// P_PlayerInSpecialSector
// Called every tic frame
//  that the player origin is in a special sector
//
// [STRIFE] Modified for new sector types and changes to old ones.
//
void P_PlayerInSpecialSector (player_t* player)
{
    sector_t*	sector;

    sector = player->mo->subsector->sector;

    // Falling, not all the way down yet?
    if (player->mo->z != sector->floorheight)
        return;	

    // Has hitten ground.
    switch (sector->special)
    {
    case 5:
        // HELLSLIME DAMAGE
        // [STRIFE] +2 to nukagecount
        if(!player->powers[pw_ironfeet])
            player->nukagecount += 2;
        break;
    
    case 16:
        // [STRIFE] +4 to nukagecount
        if(!player->powers[pw_ironfeet])
            player->nukagecount += 4;
        break;

    case 4:
    case 7:
        // [STRIFE] Immediate 5 damage every 31 tics
        if(!player->powers[pw_ironfeet])
            if(!(leveltime & 0x1f))
                P_DamageMobj(player->mo, NULL, NULL, 5);
        break;

    case 9:
        // SECRET SECTOR
        //player->secretcount++; [STRIFE] Don't have a secret count.
        sector->special = 0;
        if(player - players == consoleplayer)
            S_StartSound(NULL, sfx_yeah);
        break;

    case 11:
        // EXIT SUPER DAMAGE! (for E1M8 finale)
        player->cheats &= ~CF_GODMODE;

        if (!(leveltime&0x1f))
            P_DamageMobj (player->mo, NULL, NULL, 20);

        if (player->health <= 10)
            G_ExitLevel(0);
        break;

    case 15:
        // haleyjd 08/30/10: [STRIFE] "Instant" Death sector
        P_DamageMobj(player->mo, NULL, NULL, 999);
        break;


    case 18:
        // haleyjd 08/30/10: [STRIFE] Water current
        {
            int tagval = sector->tag - 100;
            fixed_t force;
            angle_t angle;

            if(player->cheats & CF_NOCLIP)
                return;

            force = (tagval % 10) << 12;
            angle = (tagval / 10) << 29;

            P_Thrust(player, angle, force);
        }
        break;

    default:
        I_Error ("P_PlayerInSpecialSector: "
                 "unknown special %i",
                 sector->special);
        break;
    };
}




//
// P_UpdateSpecials
// Animate planes, scroll walls, etc.
//
// [STRIFE] Modifications to support multiple scrolling line types.
//
boolean         levelTimer;
int             levelTimeCount;

void P_UpdateSpecials (void)
{
    anim_t*     anim;
    int         pic;
    int         i;
    line_t*     line;


    //  LEVEL TIMER
    if (levelTimer == true)
    {
        if(levelTimeCount) // [STRIFE] Does not allow to go negative
            levelTimeCount--;
        
        /*
        // [STRIFE] Not done here. Exit lines check this manually instead.
        if (!levelTimeCount)
            G_ExitLevel(0);
        */
    }

    //  ANIMATE FLATS AND TEXTURES GLOBALLY
    for (anim = anims ; anim < lastanim ; anim++)
    {
        for (i=anim->basepic ; i<anim->basepic+anim->numpics ; i++)
        {
            pic = anim->basepic + ( (leveltime/anim->speed + i)%anim->numpics );
            if (anim->istexture)
                texturetranslation[i] = pic;
            else
                flattranslation[i] = pic;
        }
    }

    
    //  ANIMATE LINE SPECIALS
    for (i = 0; i < numlinespecials; i++)
    {
        line = linespeciallist[i];
        switch(line->special)
        {
        case 48:
            // EFFECT FIRSTCOL SCROLL +
            sides[line->sidenum[0]].textureoffset += FRACUNIT;
            break;

        case 142:
            // haleyjd 09/25/10 [STRIFE] Scroll Up Slow
            sides[line->sidenum[0]].rowoffset += FRACUNIT;
            break;

        case 143:
            // haleyjd 09/25/10 [STRIFE] Scroll Down Fast (3 Units/Tic)
            sides[line->sidenum[0]].rowoffset -= 3*FRACUNIT;
            break;

        case 149:
            // haleyjd 09/25/10 [STRIFE] Scroll Down Slow
            sides[line->sidenum[0]].rowoffset -= FRACUNIT;
            break;
        }
    }

    
    //  DO BUTTONS
    for (i = 0; i < MAXBUTTONS; i++)
        if (buttonlist[i].btimer)
        {
            buttonlist[i].btimer--;
            if (!buttonlist[i].btimer)
            {
                switch(buttonlist[i].where)
                {
                case top:
                    sides[buttonlist[i].line->sidenum[0]].toptexture =
                        buttonlist[i].btexture;
                    break;

                case middle:
                    sides[buttonlist[i].line->sidenum[0]].midtexture =
                        buttonlist[i].btexture;
                    break;

                case bottom:
                    sides[buttonlist[i].line->sidenum[0]].bottomtexture =
                        buttonlist[i].btexture;
                    break;
                }
                S_StartSound(&buttonlist[i].soundorg,sfx_swtchn);
                memset(&buttonlist[i],0,sizeof(button_t));
            }
        }
}


//
// Donut overrun emulation
//
// Derived from the code from PrBoom+.  Thanks go to Andrey Budko (entryway)
// as usual :-)
//

#define DONUT_FLOORHEIGHT_DEFAULT 0x00000000
#define DONUT_FLOORPIC_DEFAULT 0x16

static void DonutOverrun(fixed_t *s3_floorheight, short *s3_floorpic,
                         line_t *line, sector_t *pillar_sector)
{
    static int first = 1;
    static int tmp_s3_floorheight;
    static int tmp_s3_floorpic;

    extern int numflats;

    if (first)
    {
        int p;

        // This is the first time we have had an overrun.
        first = 0;

        // Default values
        tmp_s3_floorheight = DONUT_FLOORHEIGHT_DEFAULT;
        tmp_s3_floorpic = DONUT_FLOORPIC_DEFAULT;

        //!
        // @category compat
        // @arg <x> <y>
        //
        // Use the specified magic values when emulating behavior caused
        // by memory overruns from improperly constructed donuts.
        // In Vanilla Doom this can differ depending on the operating
        // system.  The default (if this option is not specified) is to
        // emulate the behavior when running under Windows 98.

        p = M_CheckParm("-donut");

        if (p > 0 && p < myargc - 2)
        {
            // Dump of needed memory: (fixed_t)0000:0000 and (short)0000:0008
            //
            // C:\>debug
            // -d 0:0
            //
            // DOS 6.22:
            // 0000:0000    (57 92 19 00) F4 06 70 00-(16 00)
            // DOS 7.1:
            // 0000:0000    (9E 0F C9 00) 65 04 70 00-(16 00)
            // Win98:
            // 0000:0000    (00 00 00 00) 65 04 70 00-(16 00)
            // DOSBox under XP:
            // 0000:0000    (00 00 00 F1) ?? ?? ?? 00-(07 00)

            M_StrToInt(myargv[p + 1], &tmp_s3_floorheight);
            M_StrToInt(myargv[p + 2], &tmp_s3_floorpic);

            if (tmp_s3_floorpic >= numflats)
            {
                fprintf(stderr,
                        "DonutOverrun: The second parameter for \"-donut\" "
                        "switch should be greater than 0 and less than number "
                        "of flats (%d). Using default value (%d) instead. \n",
                        numflats, DONUT_FLOORPIC_DEFAULT);
                tmp_s3_floorpic = DONUT_FLOORPIC_DEFAULT;
            }
        }
    }

    /*
    fprintf(stderr,
            "Linedef: %d; Sector: %d; "
            "New floor height: %d; New floor pic: %d\n",
            line->iLineID, pillar_sector->iSectorID,
            tmp_s3_floorheight >> 16, tmp_s3_floorpic);
     */

    *s3_floorheight = (fixed_t) tmp_s3_floorheight;
    *s3_floorpic = (short) tmp_s3_floorpic;
}


//
// Special Stuff that can not be categorized
//
int EV_DoDonut(line_t*	line)
{
    sector_t*		s1;
    sector_t*		s2;
    sector_t*		s3;
    int			secnum;
    int			rtn;
    int			i;
    floormove_t*	floor;
    fixed_t s3_floorheight;
    short s3_floorpic;

    secnum = -1;
    rtn = 0;
    while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
    {
	s1 = &sectors[secnum];

	// ALREADY MOVING?  IF SO, KEEP GOING...
	if (s1->specialdata)
	    continue;

	rtn = 1;
	s2 = getNextSector(s1->lines[0],s1);

        // Vanilla Doom does not check if the linedef is one sided.  The
        // game does not crash, but reads invalid memory and causes the
        // sector floor to move "down" to some unknown height.
        // DOSbox prints a warning about an invalid memory access.
        //
        // I'm not sure exactly what invalid memory is being read.  This
        // isn't something that should be done, anyway.
        // Just print a warning and return.

        if (s2 == NULL)
        {
            fprintf(stderr,
                    "EV_DoDonut: linedef had no second sidedef! "
                    "Unexpected behavior may occur in Vanilla Doom. \n");
	    break;
        }

	for (i = 0; i < s2->linecount; i++)
	{
	    s3 = s2->lines[i]->backsector;

	    if (s3 == s1)
		continue;

            if (s3 == NULL)
            {
                // e6y
                // s3 is NULL, so
                // s3->floorheight is an int at 0000:0000
                // s3->floorpic is a short at 0000:0008
                // Trying to emulate

                fprintf(stderr,
                        "EV_DoDonut: WARNING: emulating buffer overrun due to "
                        "NULL back sector. "
                        "Unexpected behavior may occur in Vanilla Doom.\n");

                DonutOverrun(&s3_floorheight, &s3_floorpic, line, s1);
            }
            else
            {
                s3_floorheight = s3->floorheight;
                s3_floorpic = s3->floorpic;
            }

	    //	Spawn rising slime
	    floor = Z_Malloc (sizeof(*floor), PU_LEVSPEC, 0);
	    P_AddThinker (&floor->thinker);
	    s2->specialdata = floor;
	    floor->thinker.function.acp1 = (actionf_p1) T_MoveFloor;
	    floor->type = donutRaise;
	    floor->crush = false;
	    floor->direction = 1;
	    floor->sector = s2;
	    floor->speed = FLOORSPEED / 2;
	    floor->texture = s3_floorpic;
	    floor->newspecial = 0;
	    floor->floordestheight = s3_floorheight;
	    
	    //	Spawn lowering donut-hole
	    floor = Z_Malloc (sizeof(*floor), PU_LEVSPEC, 0);
	    P_AddThinker (&floor->thinker);
	    s1->specialdata = floor;
	    floor->thinker.function.acp1 = (actionf_p1) T_MoveFloor;
	    floor->type = lowerFloor;
	    floor->crush = false;
	    floor->direction = -1;
	    floor->sector = s1;
	    floor->speed = FLOORSPEED / 2;
	    floor->floordestheight = s3_floorheight;
	    break;
	}
    }
    return rtn;
}



//
// SPECIAL SPAWNING
//

//
// P_SpawnSpecials
// After the map has been loaded, scan for specials
//  that spawn thinkers
//
short		numlinespecials;
line_t*		linespeciallist[MAXLINEANIMS];


// Parses command line parameters.
//
// haleyjd 09/25/10: [STRIFE] Modifications for more scrolling line types and
// for initialization of sliding door resources.
//
void P_SpawnSpecials (void)
{
    sector_t*   sector;
    int         i;
    int         episode;

    episode = 1;
    if (W_CheckNumForName(DEH_String("texture2")) >= 0)
        episode = 2;


    // See if -TIMER was specified.

    if (timelimit > 0)
    {
        levelTimer = true;
        levelTimeCount = timelimit * 60 * TICRATE;
    }
    else
    {
        levelTimer = false;
    }

    //	Init special SECTORs - [STRIFE] Verified unmodified.
    sector = sectors;
    for (i=0 ; i<numsectors ; i++, sector++)
    {
        if (!sector->special)
            continue;

        switch (sector->special)
        {
        case 1:
            // FLICKERING LIGHTS
            P_SpawnLightFlash (sector);
            break;

        case 2:
            // STROBE FAST
            P_SpawnStrobeFlash(sector,FASTDARK,0);
            break;

        case 3:
            // STROBE SLOW
            P_SpawnStrobeFlash(sector,SLOWDARK,0);
            break;

        case 4:
            // STROBE FAST/DEATH SLIME
            P_SpawnStrobeFlash(sector,FASTDARK,0);
            sector->special = 4;
            break;

        case 8:
            // GLOWING LIGHT
            P_SpawnGlowingLight(sector);
            break;
        case 9:
            // SECRET SECTOR
            totalsecret++;
            break;

        case 10:
            // DOOR CLOSE IN 30 SECONDS
            P_SpawnDoorCloseIn30 (sector);
            break;

        case 12:
            // SYNC STROBE SLOW
            P_SpawnStrobeFlash (sector, SLOWDARK, 1);
            break;

        case 13:
            // SYNC STROBE FAST
            P_SpawnStrobeFlash (sector, FASTDARK, 1);
            break;

        case 14:
            // DOOR RAISE IN 5 MINUTES
            P_SpawnDoorRaiseIn5Mins (sector, i);
            break;

        case 17:
            P_SpawnFireFlicker(sector);
            break;
        }
    }


    //	Init line EFFECTs
    numlinespecials = 0;
    for (i = 0;i < numlines; i++)
    {
        switch(lines[i].special)
        {
        case 48:  // EFFECT FIRSTCOL SCROLL+
        case 142: // [STRIFE] TODO: verify scroll types
        case 143:
        case 149:
            linespeciallist[numlinespecials] = &lines[i];
            numlinespecials++;
            break;
        }
    }

    //	Init other misc stuff
    for (i = 0;i < MAXCEILINGS;i++)
        activeceilings[i] = NULL;

    for (i = 0;i < MAXPLATS;i++)
        activeplats[i] = NULL;

    for (i = 0;i < MAXBUTTONS;i++)
        memset(&buttonlist[i],0,sizeof(button_t));

    // villsa [STRIFE]
    P_InitSlidingDoorFrames();
}