doom / src / t_func.c

   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
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
// Emacs style mode select -*- C++ -*-
//---------------------------------------------------------------------------
//
// $Id: t_func.c 823 2011-03-17 18:53:01Z smite-meister $
//
// Copyright (C) 2000 Simon Howard
// Copyright (C) 2001-2011 by DooM Legacy Team.
//
// 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
//
// $Log: t_func.c,v $
// Revision 1.40  2005/11/07 22:54:39  iori_
// Kind of redundant unless we want a 1.43 release sometime.
//
// PlayerPitch - enabled setting the player's pitch
// ObjAngle - Enabled/Fixed setting the player's angle (was broken). May not work for MP..
// SectorEffect - kind of limited, but useful I guess. Incomplete (secret, dmg sectors)
//
// Revision 1.39  2005/05/21 08:41:23  iori_
// May 19, 2005 - PlayerArmor FS function;  1.43 can be compiled again.
//
// Revision 1.38  2004/09/17 23:04:48  darkwolf95
// playerkeysb (see comment), waittic and clocktic
//
// Revision 1.37  2004/08/26 10:53:51  iori_
// warpmap fs function
//
// Revision 1.36  2004/07/27 08:19:37  exl
// New fmod, fs functions, bugfix or 2, patrol nodes
//
// Revision 1.35  2004/03/06 17:25:04  darkwolf95
// SetObjPosition to work around spawning and removing objects
//
// Revision 1.34  2003/12/07 14:59:40  darkwolf95
// changed objstate to set state and return result of function, user can use other clues to find out what the state of an object is
//
// Revision 1.33  2003/11/21 16:15:27  darkwolf95
// small fix to resurrect
//
// Revision 1.32  2003/11/16 02:01:33  darkwolf95
// objheal(): so there's no pain state or noise; resurrect(): completely bring 'em back
//
// Revision 1.31  2003/11/15 22:04:42  darkwolf95
// Added objstate(), which is modifed from Exl's code and playerselwep() to complement playerweapon().
//
// Revision 1.30  2003/10/15 14:57:04  darkwolf95
// added ability to set with objangle()
//
// Revision 1.29  2003/10/08 15:13:02  darkwolf95
// Small fix - spawnmissile now returns mobj
//
// Revision 1.28  2003/08/23 14:07:26  darkwolf95
// added gameskill() and fixed setcamera pitch
//
// Revision 1.27  2003/07/23 17:26:36  darkwolf95
// SetLineTexture function for Fraggle Script
//
// Revision 1.26  2003/07/21 11:33:57  hurdler
// go RC1
//
// Revision 1.25  2003/07/13 13:16:15  hurdler
// go RC1
//
// Revision 1.24  2003/05/30 22:44:07  hurdler
// add checkcvar function to FS
//
// Revision 1.23  2003/05/26 18:02:29  darkwolf95
// added playeraddfrag, skincolor, testlocation and radiusattack functions
//
// Revision 1.22  2003/04/21 19:55:26  darkwolf95
// Added playdemo, spawnmissle, mapthings, objtype, mapthingnumexist, and playerweapon.
//
// Revision 1.21  2002/09/07 16:46:47  hurdler
// Fix respawning things bug using FS
//
// Revision 1.20  2002/07/28 17:11:33  hurdler
// Change version number to reflect the big changes since v.30
//
// Revision 1.19  2002/06/30 21:37:48  hurdler
// Ready for 1.32 beta 5 release
//
// Revision 1.18  2002/06/15 13:39:26  ssntails
// Little fix. =)
//
// Revision 1.17  2002/06/14 02:20:06  ssntails
// New FS function (SoM Request)
//
// Revision 1.16  2002/05/19 19:44:44  hurdler
// (woops ;))
//
// Revision 1.14  2002/01/05 16:39:19  hurdler
// Little fix
//
// Revision 1.13  2002/01/05 00:58:10  hurdler
// fix compiling problem when not using hwrender
//
// Revision 1.12  2001/12/31 14:44:50  hurdler
// Last fix for beta 4
//
// Revision 1.11  2001/12/31 13:47:46  hurdler
// Add setcorona FS command and prepare the code for beta 4
//
// Revision 1.10  2001/12/28 16:57:30  hurdler
// Add setcorona command to FS
//
// Revision 1.9  2001/12/26 17:24:46  hurdler
// Update Linux version
//
// Revision 1.8  2001/08/14 00:36:26  hurdler
// Small update
//
// Revision 1.7  2001/08/06 23:57:10  stroggonmeth
// Removed portal code, improved 3D floors in hardware mode.
//
// Revision 1.6  2001/04/30 17:19:24  stroggonmeth
// HW fix and misc. changes
//
// Revision 1.5  2001/03/21 18:24:56  stroggonmeth
// Misc changes and fixes. Code cleanup
//
// Revision 1.4  2001/03/13 22:14:20  stroggonmeth
// Long time no commit. 3D floors, FraggleScript, portals, ect.
//
// Revision 1.3  2000/11/09 17:56:20  stroggonmeth
// Hopefully fixed a few bugs and did a few optimizations.
//
// Revision 1.2  2000/11/04 16:23:44  bpereira
// no message
//
// Revision 1.1  2000/11/02 17:57:28  stroggonmeth
// FraggleScript files...
//
//
//--------------------------------------------------------------------------
//
// Functions
//
// functions are stored as variables(see variable.c), the
// value being a pointer to a 'handler' function for the
// function. Arguments are stored in an argc/argv-style list
//
// this module contains all the handler functions for the
// basic FraggleScript Functions.
//
// By Simon Howard
//
//---------------------------------------------------------------------------

/* includes ************************/

#include <stdio.h>
#include <string.h>
#include "command.h"
#include "doomstat.h"
#include "doomtype.h"
#include "d_main.h"
#include "g_game.h"
#include "hu_stuff.h"
#include "info.h"
#include "m_random.h"
#include "p_mobj.h"
#include "p_tick.h"
#include "p_spec.h"
//#include "p_hubs.h"
#include "p_inter.h"
#include "r_data.h"
#include "r_main.h"
#include "r_segs.h"
#include "r_defs.h"
#include "s_sound.h"
#include "w_wad.h"
#include "z_zone.h"
#include "p_local.h"
#include "p_setup.h"
#include "d_think.h"
#include "i_video.h"

#include "t_parse.h"
#include "t_spec.h"
#include "t_script.h"
#include "t_oper.h"
#include "t_vari.h"
#include "t_func.h"
#include "t_array.h"


fs_value_t  evaluate_expression(int start, int stop);
int find_operator(int start, int stop, char *value);


// array functions in t_array.c
void SF_NewArray(void);          // impls: array newarray(...)
void SF_NewEmptyArray(void);     // impls: array newemptyarray(...)
void SF_ArrayCopyInto(void);     // impls: void copyinto(array, array)
void SF_ArrayElementAt(void);    // impls: 'a elementat(array, int)
void SF_ArraySetElementAt(void); // impls: void setelementat(array, int, 'a)
void SF_ArrayLength(void);       // impls: int length(array)


// return a Z_Malloc string of the args( i1.. ) concatenated
char *  Z_cat_args( int i1 )
{
    int strsize = 0;
    int i;
   
    for (i = i1; i < t_argc; i++)
        strsize += strlen(stringvalue(t_argv[i]));

    char * tempstr = Z_Malloc(strsize + 1, PU_IN_USE, 0);
    tempstr[0] = '\0';

    for (i = i1; i < t_argc; i++)
//        sprintf(tempstr, "%s%s", tempstr, stringvalue(t_argv[i]));
        strcat(tempstr, stringvalue(t_argv[i])); // append

    return tempstr;
}

// functions. SF_ means Script Function not, well.. heh, me

        /////////// actually running a function /////////////

/*******************
  FUNCTIONS
 *******************/

// Actual handler functions for the script functions

// arguments are evaluated and passed to the
// handler functions using 't_argc' and 't_argv'
// in a similar way to the way C does with command line options.

// values can be returned from the functions using
// the variable 't_return'

void SF_Print()
{
    int i;

    if (!t_argc)
        return;

    for (i = 0; i < t_argc; i++)
    {
        CONS_Printf("%s", stringvalue(t_argv[i]));
    }
}

// return a random number from 0 to 255
void SF_Rnd()
{
    t_return.type = FSVT_int;
    t_return.value.i = rand() % 256;
}

// return a random number from 0 to 255
void SF_PRnd()
{
    t_return.type = FSVT_int;
    t_return.value.i = P_Random();
}

// Find the next outermost
// loop we are currently in and return the section_t for it.
fs_section_t * in_looping_section()
{
    // deepest level loop we're in that has been found so far
    fs_section_t * loopfnd = NULL;
    int n;

    // check thru all the hashchains
    for (n = 0; n < SECTIONSLOTS; n++)
    {
        fs_section_t *current = fs_current_script->sections[n];

        // check all the sections in this hashchain
        while (current)
        {
            if (current->type == FSST_loop) // a loop?
	    {
                // check to see if it's a loop that we're inside
                if (fs_src_cp >= current->start && fs_src_cp <= current->end)
                {
                    // a deeper nesting level than already found ?
                    if (!loopfnd || (current->start > loopfnd->start))
                        loopfnd = current; // save it
                }
	    }
            current = current->next;
        }
    }

    return loopfnd;        // return the closest one found
}

// "continue;" in FraggleScript is a function
void SF_Continue()
{
    fs_section_t *section;

    if (!(section = in_looping_section())) // no loop found
    {
        script_error("continue() not in loop\n");
        return;
    }

    fs_src_cp = section->end;       // jump to the closing brace
}

void SF_Break()
{
    fs_section_t *section;

    if (!(section = in_looping_section()))
    {
        script_error("break() not in loop\n");
        return;
    }

    fs_src_cp = section->end + 1;   // jump out of the loop
}

void SF_Goto()
{
    if (t_argc < 1)
    {
        script_error("incorrect arguments to goto\n");
        return;
    }

    // check argument is a labelptr

    if (t_argv[0].type != FSVT_label)
    {
        script_error("goto argument not a label\n");
        return;
    }

    // go there then if everythings fine
    fs_src_cp = t_argv[0].value.labelptr;
}

void SF_Return()
{
    fs_killscript = true;  // kill the script
}

void SF_Include()
{
    char tempstr[9];

    if (t_argc < 1)
    {
        script_error("incorrect arguments to include()");
        return;
    }

    memset(tempstr, 0, 9);

    if (t_argv[0].type == FSVT_string)
        strncpy(tempstr, t_argv[0].value.s, 8);
    else
        snprintf(tempstr, 8, "%s", stringvalue(t_argv[0]));

    parse_include(tempstr);
}

void SF_Input()
{
#if 1
   // [WDJ] was disabled in 143beta_macosx
   // Doing gets() will probably freeze program until user cancels the input.
   // If it does errmsg "not available in doom", then why do it, FIXME maybe ??
        static char inputstr[128];
	// [WDJ] NEVER use gets(), it can overrun the buffer, use fgets()
        fgets(inputstr, 128, stdin);

        t_return.type = FSVT_string;
        t_return.value.s = inputstr;
#endif
    CONS_Printf("input() function not available in doom\a\n");
}

void SF_Beep()
{
    CONS_Printf("\3");
}

void SF_Clock()
{
    t_return.type = FSVT_int;
    t_return.value.i = (gametic * 100) / 35;
}

void SF_ClockTic()
{
    t_return.type = FSVT_int;
    t_return.value.i = gametic;
}

    /**************** doom stuff ****************/

void SF_ExitLevel()
{
    G_ExitLevel();
}

void SF_Warp()  //08/25/04 iori: warp(<skill>, <"map">, [reset 0|1]);
{
    int reset = 1;

    if(t_argc < 2)
    {
        script_error("Too few arguments to function.\n");
        return;
    }

    if (t_argv[0].value.i < 1 || t_argv[0].value.i > 5)
    {
        script_error("Skill must be between 1 and 5.\n");
        return;
    }

    if(t_argc > 2)
    {
        reset = t_argv[2].value.i;

        if(reset != 0 && reset != 1)
        {
	    script_error("Reset must be either 0 or 1.\n");
	    return;
	}
    }
    G_InitNew(t_argv[0].value.i - 1, t_argv[1].value.s, reset);
}
   
// centremsg
void SF_Tip()
{
    if (fs_current_script->trigger->player != displayplayer_ptr)
        return;

    char * tempstr = Z_cat_args(0);
    HU_SetTip(tempstr, 53);
    Z_Free(tempstr);
}

// SoM: Timed tip!
void SF_TimedTip()
{
    int tiptime;

    if (t_argc < 2)
    {
        script_error("Missing parameters.\n");
        return;
    }

    tiptime = (intvalue(t_argv[0]) * 35) / 100;

    if (fs_current_script->trigger->player != displayplayer_ptr)
        return;

    char * tempstr = Z_cat_args(1);
    //CONS_Printf("%s\n", tempstr);
    HU_SetTip(tempstr, tiptime);
    Z_Free(tempstr);
}

// tip to a particular player
void SF_PlayerTip()
{
    int plnum;

    if (!t_argc)
    {
        script_error("player not specified\n");
        return;
    }

    plnum = intvalue(t_argv[0]);

    if (consoleplayer != plnum)
        return;

    char * tempstr = Z_cat_args(1);
    //CONS_Printf("%s\n", tempstr);
    HU_SetTip(tempstr, 53);
    Z_Free(tempstr);
}

        // message player
void SF_Message()
{
    if (fs_current_script->trigger->player != displayplayer_ptr)
        return;

    char * tempstr = Z_cat_args(0);
    CONS_Printf("%s\n", tempstr);
    Z_Free(tempstr);
}


//DarkWolf95:July 28, 2003:Added unimplemented function
void SF_GameSkill()
{
    t_return.type = FSVT_int;
    t_return.value.i = gameskill + 1;  //make 1-5, rather than 0-4
}

// Returns what type of game is going on - Deathmatch, CoOp, or Single Player.
// Feature Requested by SoM! SSNTails 06-13-2002
void SF_GameMode()
{
    t_return.type = FSVT_int;

    if (cv_deathmatch.value)    // Deathmatch!
        t_return.value.i = 2;
    else if (netgame || multiplayer)    // Cooperative
        t_return.value.i = 1;
    else        // Single Player
        t_return.value.i = 0;

    return;
}

// message to a particular player
void SF_PlayerMsg()
{
    int plnum;

    if (!t_argc)
    {
        script_error("player not specified\n");
        return;
    }

    plnum = intvalue(t_argv[0]);

    if (displayplayer != plnum)
        return;

    char * tempstr = Z_cat_args(1);
    CONS_Printf("%s\n", tempstr);
    Z_Free(tempstr);
}

void SF_PlayerInGame()
{
    if (!t_argc)
    {
        script_error("player not specified\n");
        return;
    }

    t_return.type = FSVT_int;
    t_return.value.i = playeringame[intvalue(t_argv[0])];
}

void SF_PlayerName()
{
    int plnum;

    if (!t_argc)
    {
        player_t *pl;
        pl = fs_current_script->trigger->player;
        if (pl)
            plnum = pl - players;
        else
        {
            script_error("script not started by player\n");
            return;
        }
    }
    else
        plnum = intvalue(t_argv[0]);

    t_return.type = FSVT_string;
    t_return.value.s = player_names[plnum];
}

void SF_PlayerAddFrag()
{
    int playernum1;
    int playernum2;

    if (t_argc < 1)
    {
        script_error("not enough arguements");
        return;
    }

    if (t_argc == 1)
    {
        playernum1 = intvalue(t_argv[0]);

        players[playernum1].addfrags++;

        t_return.type = FSVT_int;
        t_return.value.f = players[playernum1].addfrags;
    }

    else
    {
        playernum1 = intvalue(t_argv[0]);
        playernum2 = intvalue(t_argv[1]);

        players[playernum1].frags[playernum2]++;

        t_return.type = FSVT_int;
        t_return.value.f = players[playernum1].frags[playernum2];
    }
}

        // object being controlled by player
void SF_PlayerObj()
{
    int plnum;

    if (!t_argc)
    {
        player_t *pl;
        pl = fs_current_script->trigger->player;
        if (pl)
            plnum = pl - players;
        else
        {
            script_error("script not started by player\n");
            return;
        }
    }
    else
        plnum = intvalue(t_argv[0]);

    t_return.type = FSVT_mobj;
    t_return.value.mobj = players[plnum].mo;
}

void SF_MobjIsPlayer()
{
    mobj_t *mobj;

    if (t_argc == 0)
    {
        t_return.type = FSVT_int;
        t_return.value.i = fs_current_script->trigger->player ? 1 : 0;
        return;
    }
    mobj = MobjForSvalue(t_argv[0]);
    t_return.type = FSVT_int;
    if (!mobj)
        t_return.value.i = 0;
    else
        t_return.value.i = mobj->player ? 1 : 0;
    return;
}

void SF_SkinColor()
{
    int playernum = 0;	// might be used uninit
    int colour;

    if (!t_argc)
    {
        script_error("too few parameters for skincolor\n");
        return;
    }

    if (t_argc >= 1)
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
    }

    if(t_argc == 2)
    {
		colour = intvalue(t_argv[1]);
		
		if(colour > MAXSKINCOLORS)
		{
			script_error("skin colour %i is out of range\n", colour);
		}

		players[playernum].skincolor = colour;
		players[playernum].mo->flags = (players[playernum].mo->flags & ~MF_TRANSLATION) | ((players[playernum].mo->player->skincolor) << MF_TRANSSHIFT);

		CV_SetValue (&cv_playercolor, colour);
    }
	
    t_return.type = FSVT_int;
    t_return.value.i = players[playernum].skincolor;

    return;
}

void SF_PlayerKeys()
{
    int playernum;
    int keynum;
    int givetake;

    if (t_argc < 2)
    {
        script_error("missing parameters for playerkeys\n");
        return;
    }

    if (t_argc == 2)
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        keynum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (keynum > 5)
        {
            script_error("keynum out of range! %s\n", keynum);
            return;
        }
        t_return.type = FSVT_int;
        t_return.value.i = (players[playernum].cards & (1 << keynum)) ? 1 : 0;
        return;
    }
    else
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        keynum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (keynum > 6)
        {
            script_error("keynum out of range! %s\n", keynum);
            return;
        }
        givetake = intvalue(t_argv[2]);
        t_return.type = FSVT_int;
        if (givetake)
            players[playernum].cards |= (1 << keynum);
        else
            players[playernum].cards &= ~(1 << keynum);
        t_return.value.i = 0;
        return;
    }
}

/*	DarkWolf95:September 17, 2004:playerkeysb

	Returns players[i].cards as a whole, since FS supports binary operators.
	Also allows you to set upper two bits of cards (64 & 128).  Thus the user
	can have two new boolean values to work with.  CTF, Runes, Tag...
	
	playerkeys(playernum, [newbyte]) */

void SF_PlayerKeysByte()
{
	int playernum;
	int keybyte;
	
	if (!t_argc)
	{
		script_error("not enough arguments for playerkeysb\n");
		return;
	}
	
	playernum = intvalue(t_argv[0]);
    if (!playeringame[playernum])
	{
            script_error("player %i not in game\n", playernum);
            return;
	}

	if(t_argc == 2)
	{
		keybyte = intvalue(t_argv[1]);
		if(keybyte > 255)	//don't overflow
			keybyte = 0;

		players[playernum].cards = keybyte;
	}
	
	t_return.type = FSVT_int;
	t_return.value.i = players[playernum].cards;
}

// iori 05/17/2005: playerarmor
void SF_PlayerArmor()
{
	int armor;
	int playernum;

	if (!t_argc)
	{
		script_error("insufficient parameters for playerarmor\n");
		return;
	}

	playernum = t_argv[0].value.i;
	armor = t_argv[1].value.i;

	players[playernum].armorpoints = armor;

	if (players[playernum].armorpoints > 100)
	{
		players[playernum].armortype = 2;
	}
	else
		players[playernum].armortype = 1;

	t_return.type = FSVT_int;
    t_return.value.i = armor;
}

void SF_PlayerAmmo()
{
    int playernum;
    int ammonum;
    int newammo;

    if (t_argc < 2)
    {
        script_error("missing parameters for playerammo\n");
        return;
    }

    if (t_argc == 2)
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        ammonum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (ammonum >= NUMAMMO)
        {
            script_error("ammonum out of range! %s\n", ammonum);
            return;
        }
        t_return.type = FSVT_int;
        t_return.value.i = players[playernum].ammo[ammonum];
        return;
    }
    else
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        ammonum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (ammonum > NUMAMMO)
        {
            script_error("ammonum out of range! %s\n", ammonum);
            return;
        }
        newammo = intvalue(t_argv[2]);
        newammo = newammo > players[playernum].maxammo[ammonum] ? players[playernum].maxammo[ammonum] : newammo;
        t_return.type = FSVT_int;
        t_return.value.i = players[playernum].ammo[ammonum] = newammo;
        return;
    }
}

void SF_MaxPlayerAmmo()
{
    int playernum;
    int ammonum;
    int newmax;

    if (t_argc < 2)
    {
        script_error("missing parameters for maxplayerammo\n");
        return;
    }

    if (t_argc == 2)
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        ammonum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (ammonum >= NUMAMMO || ammonum < 0)
        {
            script_error("maxammonum out of range! %i\n", ammonum);
            return;
        }
        t_return.type = FSVT_int;
        t_return.value.i = players[playernum].maxammo[ammonum];
        return;
    }
    else
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        ammonum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (ammonum > NUMAMMO)
        {
            script_error("ammonum out of range! %s\n", ammonum);
            return;
        }
        newmax = intvalue(t_argv[2]);
        t_return.type = FSVT_int;
        t_return.value.i = players[playernum].maxammo[ammonum] = newmax;
        return;
    }
}

//playerweapon(playernum, weaponnum, [give])

void SF_PlayerWeapon()
{
    int playernum;
    int weaponnum;
    int newweapon;

    if (t_argc < 2)
    {
        script_error("missing parameters for playerweapon\n");
        return;
    }

    if (t_argc == 2)
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        weaponnum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (weaponnum >= NUMWEAPONS)
        {
            script_error("weaponnum out of range! %s\n", weaponnum);
            return;
        }
        t_return.type = FSVT_int;
        t_return.value.i = players[playernum].weaponowned[weaponnum];
        return;
    }
    else
    {
        if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
        else
            playernum = intvalue(t_argv[0]);

        weaponnum = intvalue(t_argv[1]);
        if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (weaponnum > NUMWEAPONS)
        {
            script_error("weaponnum out of range! %s\n", weaponnum);
            return;
        }

        newweapon = intvalue(t_argv[2]);

        if (newweapon != 0)
            newweapon = 1;

        t_return.type = FSVT_int;
        t_return.value.i = players[playernum].weaponowned[weaponnum] = newweapon;
        return;
    }
}

void SF_PlayerSelectedWeapon()
{
	int playernum;
    int weaponnum;

	if (!t_argc)
	{
		script_error("no enough arguments for playerselwep\n");
		return;
	}

	if (t_argv[0].type == FSVT_mobj)
        {
            if (!t_argv[0].value.mobj->player)
            {
                script_error("mobj not a player!\n");
                return;
            }
            playernum = t_argv[0].value.mobj->player - players;
        }
	else
		playernum = intvalue(t_argv[0]);

	
	if (t_argc == 1)
    {   
		t_return.type = FSVT_int;
        t_return.value.i = players[playernum].readyweapon;
    }

	else if(t_argc == 2)
	{
		weaponnum = intvalue(t_argv[1]);
		
		if (!playeringame[playernum])
        {
            script_error("player %i not in game\n", playernum);
            return;
        }
        if (weaponnum >= NUMWEAPONS)
        {
            script_error("weapon not available: %s\n", weaponnum);
            return;
        }

		players[playernum].pendingweapon = weaponnum;

		t_return.type = FSVT_int;
		t_return.value.i = players[playernum].readyweapon;
	}
}


// Exl: Toxicfluff's pitchview function.
// Returns a player's view pitch in a range useful for the FS trig functions
// iori: added ability to modify player's pitch
void SF_PlayerPitch()
{
	int playernum;
	
	if (!t_argc)
	{
		script_error("not enough arguments for playerpitch\n");
		return;
		}
	
	playernum = intvalue(t_argv[0]);
    if (!playeringame[playernum])
	{
            script_error("player %i not in game\n", playernum);
            return;
	}

	if(t_argc == 2)
	{
		localaiming = FixedToAngle(fixedvalue(t_argv[1]));
	}

	t_return.type = FSVT_fixed;
	t_return.value.f = AngleToFixed(players[playernum].aiming);
}


// Set player properties
// This could (or rather, should) be expanded to support all players
//
void SF_PlayerProperty()
{

	if (t_argc != 2)
	{
		script_error("PlayerProperty: insufficient arguments\n");
		return;
	}

	switch(intvalue(t_argv[0]))
	{
		// Speed
		case 0:
			extramovefactor = intvalue(t_argv[1]);
			return;

		case 1:
			JUMPGRAVITY = intvalue(t_argv[1]) * FRACUNIT / NEWTICRATERATIO;
			return;

		case 2:
			if (intvalue(t_argv[1]))
				consoleplayer_ptr->locked = true;
			else
				consoleplayer_ptr->locked = false;
			return;

		default:
			script_error("PlayerProperty: invalid property specified\n");
	}

}


extern void SF_StartScript();   // in t_script.c
extern void SF_ScriptRunning();
extern void SF_Wait();
extern void SF_WaitTic();
extern void SF_TagWait();
extern void SF_ScriptWait();

/*********** Mobj code ***************/

void SF_Player()
{
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_int;

    if (mo)
    {
        t_return.value.i = (int) (mo->player - players);
    }
    else
    {
        t_return.value.i = -1;
    }
}

        // spawn an object: type, x, y, [angle]
void SF_Spawn()
{
    int x, y, z, objtype;
    angle_t angle = 0;

    if (t_argc < 3)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    objtype = intvalue(t_argv[0]);
    x = intvalue(t_argv[1]) << FRACBITS;
    y = intvalue(t_argv[2]) << FRACBITS;
    if (t_argc >= 5)
        z = intvalue(t_argv[4]) << FRACBITS;
    else
    {
        // SoM: Check thing flags for spawn-on-ceiling types...
        z = R_PointInSubsector(x, y)->sector->floorheight;
    }

    if (t_argc >= 4)
        angle = intvalue(t_argv[3]) * (ANG45 / 45);

    // invalid object to spawn
    if (objtype < 0 || objtype >= NUMMOBJTYPES)
    {
        script_error("unknown object type: %i\n", objtype);
        return;
    }

    t_return.type = FSVT_mobj;
    t_return.value.mobj = P_SpawnMobj(x, y, z, objtype);
    t_return.value.mobj->angle = angle;

    {   //Hurdler: fix the crashing bug of respawning monster
        mapthing_t *mthing;
        mthing = Z_Malloc(sizeof(mapthing_t), PU_LEVEL, NULL);
        mthing->x = x >> FRACBITS;
        mthing->y = y >> FRACBITS;
        mthing->z = z >> FRACBITS;
        mthing->angle = angle >> FRACBITS;
        mthing->type = mobjinfo[objtype].doomednum;     //objtype;
        mthing->options = MTF_FS_SPAWNED;
        mthing->mobj = t_return.value.mobj;
        t_return.value.mobj->spawnpoint = mthing;
    }
}

void SF_SpawnExplosion()
{
    int type;
    fixed_t x, y, z;
    mobj_t *spawn;

    if (t_argc < 3)
    {
        script_error("SpawnExplosion: Missing arguments\n");
        return;
    }

    type = intvalue(t_argv[0]);
    if (type < 0 || type >= NUMMOBJTYPES)
    {
        script_error("SpawnExplosion: Invalud type number\n");
        return;
    }

    x = fixedvalue(t_argv[1]);
    y = fixedvalue(t_argv[2]);
    if (t_argc > 3)
        z = fixedvalue(t_argv[3]);
    else
        z = R_PointInSubsector(x, y)->sector->floorheight;

    spawn = P_SpawnMobj(x, y, z, type);
    t_return.type = FSVT_int;
    t_return.value.i = P_SetMobjState(spawn, spawn->info->deathstate);
    if (spawn->info->deathsound)
        S_StartSound(spawn, spawn->info->deathsound);
}

void SF_RadiusAttack()
{
    mobj_t *spot;
    mobj_t *source;
    int damage;

    if (t_argc != 3)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    spot = MobjForSvalue(t_argv[0]);
    source = MobjForSvalue(t_argv[1]);
    damage = intvalue(t_argv[2]);

    if (spot && source)
    {
        P_RadiusAttack(spot, source, damage);
    }
}

void SF_RemoveObj()
{
    mobj_t *mo;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (mo)     // nullptr check
        P_RemoveMobj(mo);
}

void SF_KillObj()
{
    mobj_t *mo;

    if (t_argc)
        mo = MobjForSvalue(t_argv[0]);
    else
        mo = fs_current_script->trigger;   // default to trigger object

    if (mo)     // nullptr check
        P_KillMobj(mo, NULL, fs_current_script->trigger);  // kill it
}

        // mobj x, y, z
void SF_ObjX()
{
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_fixed;
    t_return.value.f = mo ? mo->x : 0;  // null ptr check
}

void SF_ObjY()
{
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_fixed;
    t_return.value.f = mo ? mo->y : 0;  // null ptr check
}

void SF_ObjZ()
{
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_fixed;
    t_return.value.f = mo ? mo->z : 0;  // null ptr check
}

void SF_SetObjPosition()
{
	mobj_t* mobj;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

	mobj = MobjForSvalue(t_argv[0]);

	P_UnsetThingPosition(mobj);

	mobj->x = intvalue(t_argv[1]) << FRACBITS;

	if(t_argc >= 3)
		mobj->y = intvalue(t_argv[2]) << FRACBITS;
	if(t_argc == 4)
		mobj->z = intvalue(t_argv[3]) << FRACBITS;

	P_SetThingPosition(mobj);
}
	

void SF_Resurrect()
{

	mobj_t *mo;

	if(t_argc != 1)
	{
		script_error("invalid number of arguments for resurrect\n");
		return;
	}

	mo = MobjForSvalue(t_argv[0]);

	if(!mo->info->raisestate)  //Don't resurrect things that can't be resurrected
		return;

	P_SetMobjState (mo, mo->info->raisestate);
	if( demoversion<129 )
		mo->height <<= 2;
	else
	{
		mo->height = mo->info->height;
		mo->radius = mo->info->radius;
	}
	
	mo->flags = mo->info->flags;
	mo->health = mo->info->spawnhealth;
	mo->target = NULL;

}

void SF_TestLocation()
{
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    if (!mo)
        return;

    if (P_TestMobjLocation(mo))
    {
        t_return.type = FSVT_int;
        t_return.value.f = 1;
    }

    else
    {
        t_return.type = FSVT_int;
        t_return.value.f = 0;
    }
}

        // mobj angle
void SF_ObjAngle()
{
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

	if(t_argc > 1)
	{
		//iori: now able to change the player's angle, not just mobj's
		if(mo == consoleplayer_ptr->mo)
		{
			localangle = FixedToAngle(fixedvalue(t_argv[1]));
		}
		else
		{
			mo->angle = FixedToAngle(fixedvalue(t_argv[1]));
		}
	}
	t_return.type = FSVT_fixed;
	t_return.value.f = (int)AngleToFixed(mo->angle);       // null ptr check
}


void SF_CheckSight()
{
	mobj_t *obj1;
	mobj_t *obj2;

	if(!t_argc)
	{
		script_error("CheckSight: insufficient arguments to function\n");
		return;
	}

	obj1 = MobjForSvalue(t_argv[0]);
	obj2 = t_argc == 2 ? MobjForSvalue(t_argv[1]) : fs_current_script->trigger;

	t_return.type = FSVT_int;
	t_return.value.i = P_CheckSight(obj1, obj2);
}


// teleport: object, sector_tag
void SF_Teleport()
{
    line_t line;                // dummy line for teleport function
    mobj_t *mo;

    if (t_argc == 0)    // no arguments
    {
        script_error("insufficient arguments to function\n");
        return;
    }
    else if (t_argc == 1)       // 1 argument: sector tag
    {
        mo = fs_current_script->trigger;   // default to trigger
        line.tag = intvalue(t_argv[0]);
    }
    else        // 2 or more
    {   // teleport a given object
        mo = MobjForSvalue(t_argv[0]);
        line.tag = intvalue(t_argv[1]);
    }

    if (mo)
        EV_Teleport(&line, 0, mo);
}

void SF_SilentTeleport()
{
    line_t line;                // dummy line for teleport function
    mobj_t *mo;

    if (t_argc == 0)    // no arguments
    {
        script_error("insufficient arguments to function\n");
        return;
    }
    else if (t_argc == 1)       // 1 argument: sector tag
    {
        mo = fs_current_script->trigger;   // default to trigger
        line.tag = intvalue(t_argv[0]);
    }
    else        // 2 or more
    {   // teleport a given object
        mo = MobjForSvalue(t_argv[0]);
        line.tag = intvalue(t_argv[1]);
    }

    if (mo)
        EV_SilentTeleport(&line, 0, mo);
}

void SF_DamageObj()
{
    mobj_t *mo;
    int damageamount;

    if (t_argc == 0)    // no arguments
    {
        script_error("insufficient arguments to function\n");
        return;
    }
    else if (t_argc == 1)       // 1 argument: damage trigger by amount
    {
        mo = fs_current_script->trigger;   // default to trigger
        damageamount = intvalue(t_argv[0]);
    }
    else        // 2 or more
    {   // teleport a given object
        mo = MobjForSvalue(t_argv[0]);
        damageamount = intvalue(t_argv[1]);
    }

    if (mo)
        P_DamageMobj(mo, NULL, fs_current_script->trigger, damageamount);
}


        // the tag number of the sector the thing is in
void SF_ObjSector()
{
    // use trigger object if not specified
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_int;
    t_return.value.i = mo ? mo->subsector->sector->tag : 0;     // nullptr check
}

        // the health number of an object
void SF_ObjHealth()
{
    // use trigger object if not specified
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_int;
    t_return.value.i = mo ? mo->health : 0;
}

void SF_ObjDead()
{
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_int;
    if (mo && (mo->health <= 0 || mo->flags & MF_CORPSE))
        t_return.value.i = 1;
    else
        t_return.value.i = 0;
}

void SF_ObjFlag()
{
    mobj_t *mo;
    int flagnum;

    if (t_argc == 0)    // no arguments
    {
        script_error("ObjFlag: no arguments for function\n");
        return;
    }
    else if (t_argc == 1)       // use trigger, 1st is flag
    {
        // use trigger:
        mo = fs_current_script->trigger;
        flagnum = intvalue(t_argv[0]);
    }
    else if (t_argc == 2)
    {
        // specified object
        mo = MobjForSvalue(t_argv[0]);
        flagnum = intvalue(t_argv[1]);
    }
    else        // >= 3 : SET flags
    {
        mo = MobjForSvalue(t_argv[0]);
        flagnum = intvalue(t_argv[1]);

        if (mo) // nullptr check
        {
            long newflag;
            // remove old bit
            mo->flags = mo->flags & ~(1 << flagnum);

            // make the new flag
            newflag = (!!intvalue(t_argv[2])) << flagnum;
            mo->flags |= newflag;       // add new flag to mobj flags
        }

        //P_UpdateThinker(&mo->thinker);     // update thinker

    }

    t_return.type = FSVT_int;
    // nullptr check:
    t_return.value.i = mo ? !!(mo->flags & (1 << flagnum)) : 0;
}


// Just copy n paste :>
void SF_ObjFlag2()
{
    mobj_t *mo;
    int flagnum;

    if (t_argc == 0)    // no arguments
    {
        script_error("ObjFlag2: no arguments for function\n");
        return;
    }
    else if (t_argc == 1)       // use trigger, 1st is flag
    {
        // use trigger:
        mo = fs_current_script->trigger;
        flagnum = intvalue(t_argv[0]);
    }
    else if (t_argc == 2)
    {
        // specified object
        mo = MobjForSvalue(t_argv[0]);
        flagnum = intvalue(t_argv[1]);
    }
    else        // >= 3 : SET flags
    {
        mo = MobjForSvalue(t_argv[0]);
        flagnum = intvalue(t_argv[1]);

        if (mo) // nullptr check
        {
            long newflag;
            // remove old bit
            mo->flags2 = mo->flags2 & ~(1 << flagnum);

            // make the new flag
            newflag = (!!intvalue(t_argv[2])) << flagnum;
            mo->flags2 |= newflag;       // add new flag to mobj flags
        }

        //P_UpdateThinker(&mo->thinker);     // update thinker

    }

    t_return.type = FSVT_int;
    // nullptr check:
    t_return.value.i = mo ? !!(mo->flags2 & (1 << flagnum)) : 0;
}


// Extra flags, too
void SF_ObjEFlag()
{
    mobj_t *mo;
    int flagnum;

    if (t_argc == 0)    // no arguments
    {
        script_error("ObjEFlag: no arguments for function\n");
        return;
    }
    else if (t_argc == 1)       // use trigger, 1st is flag
    {
        // use trigger:
        mo = fs_current_script->trigger;
        flagnum = intvalue(t_argv[0]);
    }
    else if (t_argc == 2)
    {
        // specified object
        mo = MobjForSvalue(t_argv[0]);
        flagnum = intvalue(t_argv[1]);
    }
    else        // >= 3 : SET flags
    {
        mo = MobjForSvalue(t_argv[0]);
        flagnum = intvalue(t_argv[1]);

        if (mo) // nullptr check
        {
            long newflag;
            // remove old bit
            mo->eflags = mo->eflags & ~(1 << flagnum);

            // make the new flag
            newflag = (!!intvalue(t_argv[2])) << flagnum;
            mo->eflags |= newflag;       // add new flag to mobj flags
        }

    }

    t_return.type = FSVT_int;
    // nullptr check:
    t_return.value.i = mo ? !!(mo->eflags & (1 << flagnum)) : 0;
}


// apply momentum to a thing
void SF_PushThing()
{
    mobj_t *mo;
    angle_t angle;
    fixed_t force;

    if (t_argc < 3)     // missing arguments
    {
        script_error("insufficient arguments for function\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);

    if (!mo)
        return;

    angle = FixedToAngle(fixedvalue(t_argv[1]));
    force = fixedvalue(t_argv[2]);

    mo->momx += FixedMul(finecosine[angle >> ANGLETOFINESHIFT], force);
    mo->momy += FixedMul(finesine[angle >> ANGLETOFINESHIFT], force);
}

void SF_ReactionTime()
{
    mobj_t *mo;

    if (t_argc < 1)
    {
        script_error("no arguments for function\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (!mo)
        return;

    if (t_argc > 1)
    {
        mo->reactiontime = (intvalue(t_argv[1]) * 35) / 100;
    }

    t_return.type = FSVT_int;
    t_return.value.i = mo->reactiontime;
}

// Sets a mobj's Target! >:)
void SF_MobjTarget()
{
    mobj_t *mo;
    mobj_t *target;

    if (t_argc < 1)
    {
        script_error("Missing parameters!\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (!mo)
        return;

    if (t_argc >= 2)
    {
        if (t_argv[1].type != FSVT_mobj && intvalue(t_argv[1]) == -1)
        {
            // Set target to NULL
			mo->target = NULL;
            P_SetMobjState(mo, mo->info->spawnstate);
        }
        else
        {
			target = MobjForSvalue(t_argv[1]);

			// Also remember node here
			if (target->type == MT_NODE)
				mo->targetnode = target;

            mo->target = target;
            P_SetMobjState(mo, mo->info->seestate);
        }
    }

    t_return.type = FSVT_mobj;
    t_return.value.mobj = mo->target;
}

void SF_MobjMomx()
{
    mobj_t *mo;

    if (t_argc < 1)
    {
        script_error("missing parameters\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (t_argc > 1)
    {
        if (!mo)
            return;
        mo->momx = fixedvalue(t_argv[1]);
    }

    t_return.type = FSVT_fixed;
    t_return.value.f = mo ? mo->momx : 0;
}

void SF_MobjMomy()
{
    mobj_t *mo;

    if (t_argc < 1)
    {
        script_error("missing parameters\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (t_argc > 1)
    {
        if (!mo)
            return;
        mo->momy = fixedvalue(t_argv[1]);
    }

    t_return.type = FSVT_fixed;
    t_return.value.f = mo ? mo->momy : 0;
}

void SF_MobjMomz()
{
    mobj_t *mo;

    if (t_argc < 1)
    {
        script_error("missing parameters\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (t_argc > 1)
    {
        if (!mo)
            return;
        mo->momz = fixedvalue(t_argv[1]);
    }

    t_return.type = FSVT_fixed;
    t_return.value.f = mo ? mo->momz : 0;
}

void SF_SpawnMissile()
{
    mobj_t *mobj;
    mobj_t *target;
    int objtype;

    if (t_argc != 3)
    {
        script_error("invalid number of arguments");
        return;
    }

    objtype = intvalue(t_argv[2]);

    if (objtype < 0 || objtype >= NUMMOBJTYPES)
    {
        script_error("unknown object type: %i\n", objtype);
        return;
    }
    mobj = MobjForSvalue(t_argv[0]);
    target = MobjForSvalue(t_argv[1]);

	t_return.type = FSVT_mobj;
    t_return.value.mobj = P_SpawnMissile(mobj, target, objtype);
}


// Exl: Modified by Tox to take a pitch parameter
void SF_LineAttack()
{
	mobj_t	*mo;
	angle_t aiming;
	int		damage, angle, slope;
	int		short fixedtodeg = 182.033;
	
	
	mo = MobjForSvalue(t_argv[0]);
	damage = intvalue(t_argv[2]);

	angle = (intvalue(t_argv[1]) * (ANG45 / 45));
	
	if(t_argc == 4)
	{
		aiming = fixedvalue(t_argv[3]) * fixedtodeg;
		slope = AIMINGTOSLOPE(aiming);
	}
	else
		slope = P_AimLineAttack(mo, angle, MISSILERANGE);

	P_LineAttack(mo, angle, MISSILERANGE, slope, damage);
}


//checks to see if a Map Thing Number exists; used to avoid script errors

void SF_MapThingNumExist()
{

    int intval;

    if (t_argc != 1)
    {
        script_error("invalid number of arguments");
        return;
    }

    intval = intvalue(t_argv[0]);

    if (intval < 0 || intval >= nummapthings || !mapthings[intval].mobj)
    {
        t_return.type = FSVT_int;
        t_return.value.i = 0;
    }
    else
    {
        t_return.type = FSVT_int;
        t_return.value.i = 1;
    }
}

void SF_MapThings()
{
    t_return.type = FSVT_int;
    t_return.value.i = nummapthings;
}

void SF_ObjType()
{
    // use trigger object if not specified
    mobj_t *mo = t_argc ? MobjForSvalue(t_argv[0]) : fs_current_script->trigger;

    t_return.type = FSVT_int;
    t_return.value.i = mo->type;
}


// Exl: sets an object's properties (tox)
// setobjproperty(attribute, value, [mobj])
void SF_SetObjProperty()
{
  if (t_argc < 2)
  {
    script_error("insufficient parameters for objproperty");
    return;
  }

  int attrib = intvalue(t_argv[0]); 
  int value  = intvalue(t_argv[1]);
  // FIXME fixed_t vals, syntax does not make sense (operate on MTs instead of mobjs...
  mobj_t *mo = (t_argc >= 3) ? MobjForSvalue(t_argv[2]) : fs_current_script->trigger;

  switch (attrib)
    {
    case 0:
      mo->info->radius = (value*FRACUNIT);
      break;
    case 1:
      mo->info->height = (value*FRACUNIT);
      break;
    case 2:
      mo->info->mass = value;
      break;
    case 3:
      mo->info->spawnhealth = value;
      break;
    case 4:
      mo->info->damage = value;
      break;
    case 5:
      mo->info->speed = value;
      break;
    case 6:
      mo->info->reactiontime = value;
      break;
    case 7:
      mo->info->painchance = value;
      break;

    case 8:
      mo->info->spawnstate = value;
      break;
    case 9:
      mo->info->seestate = value;
      break;
    case 10:
      mo->info->meleestate = value;
      break;
    case 11:
      mo->info->missilestate = value;
      break;
    case 12:
      mo->info->painstate = value;
      break;
    case 13:
      mo->info->deathstate = value;
      break;
    case 14:
      mo->info->xdeathstate = value;
      break;
    case 15:
      mo->info->crashstate = value;
      break;
    case 16:
      mo->info->raisestate = value;
      break;

    case 17:
      mo->info->seesound = value;
      break;
    case 18:
      mo->info->activesound = value;
      break;
    case 19:
      mo->info->attacksound = value;
      break;
    case 20:
      mo->info->painsound = value;
      break;
    case 21:
      mo->info->deathsound = value;
      break;
    default:
      script_error("invalid attribute for objproperty");
      return;
    }
}


// Exl: Returns an object's properties (tox)
void SF_GetObjProperty()
{
	int	attrib = 0, retval = 0;	// might be used uninit
	mobj_t	*mo = NULL;

	if(t_argc < 1)
	{
		script_error("insufficient parameters for getobjproperty");
	}
	if(t_argc == 1)
	{
		mo = fs_current_script->trigger;
		attrib = intvalue(t_argv[0]);
	}
	if(t_argc == 2)
	{
		mo = MobjForSvalue(t_argv[0]);
		attrib = intvalue(t_argv[1]);
	}
	switch (attrib)
	{
		case 0:
			retval = mo->info->radius / FRACUNIT;
			break;
		case 1:
			retval = mo->info->height / FRACUNIT;
			break;
		case 2:
		  retval = mo->info->mass;		  
			break;
		case 3:
		        retval = mo->info->spawnhealth;
			break;
		case 4:
			retval = mo->info->damage;
			break;
		case 5:
		        retval = mo->info->speed;
			break;
		case 6:
			retval = mo->info->reactiontime;
			break;
		case 7:
			retval = mo->info->painchance;
			break;
		case 8:
			retval = mo->info->spawnstate;
			break;
		case 9:
			retval = mo->info->seestate;
			break;
		case 10:
			retval = mo->info->meleestate;
			break;
		case 11:
			retval = mo->info->missilestate;
			break;
		case 12:
			retval = mo->info->painstate;
			break;
		case 13:
			retval = mo->info->deathstate;
			break;
		case 14:
			retval = mo->info->xdeathstate;
			break;
		case 15:
			retval = mo->info->crashstate;
			break;
		case 16:
			retval = mo->info->raisestate;
			break;
		case 17:
			retval = mo->info->seesound;
			break;
		case 18:
			retval = mo->info->activesound;
			break;
		case 19:
			retval = mo->info->attacksound;
			break;
		case 20:
			retval = mo->info->painsound;
			break;
		case 21:
			retval = mo->info->deathsound;
			break;
		default:
			script_error("invalid attribute for getobjproperty");
			return;

	}
	t_return.type = FSVT_int;
	t_return.value.i = retval;
}


void SF_ObjState()  //DarkWolf95:November 15, 2003: Adaptaion of Exl's code
{					//DarkWolf95:December 7, 2003: Change to set only
	int		state, newstate;
	mobj_t	*mo;

	if(t_argc == 1)
	{
		mo = fs_current_script->trigger;
		state = intvalue(t_argv[0]);
	}

	else if(t_argc == 2)
	{
		mo = MobjForSvalue(t_argv[0]);
		state = intvalue(t_argv[1]);
	}

	else
	{
		script_error("objstate: invalid number of arguments\n");
		return;
	}

	switch (state)
	{
		case 8:
			newstate = mo->info->spawnstate;
			break;
		case 9:
			newstate = mo->info->seestate;
			break;
		case 10:
			newstate = mo->info->meleestate;
			break;
		case 11:
			newstate = mo->info->missilestate;
			break;
		case 12:
			newstate = mo->info->painstate;
			break;
		case 13:
			newstate = mo->info->deathstate;
			break;
		case 14:
			newstate = mo->info->xdeathstate;
			break;
		case 15:
			newstate = mo->info->crashstate;
			break;
		case 16:
			newstate = mo->info->raisestate;
			break;
		default:
			script_error("objstate: invalid state");
			return;
	}

		t_return.type = FSVT_int;
		t_return.value.i = P_SetMobjState(mo, newstate);
}



void SF_HealObj()
{

	mobj_t	*mo;
	int heal = 0;


	// Heal trigger to default health
	if (t_argc == 0)
	{
		mo = fs_current_script->trigger;
		mo->health = mo->info->spawnhealth;
	}

	// Heal specified mobj to default health
	else if (t_argc == 1)
	{
		mo = MobjForSvalue(t_argv[0]);
		mo->health = mo->info->spawnhealth;
	}

	// Heal specied mobj to given health
	else if (t_argc == 2)
	{
		mo = MobjForSvalue(t_argv[0]);
		heal = intvalue(t_argv[1]);
		mo->health = heal;
	}

	else
	{
		script_error("HealObj: Invalid number of arguments specified");
		return;
	}

}


// Set next node mobj
void SF_SetNodeNext()
{

	mobj_t*		mo;		// Affected
	mobj_t*		nextmo;


	if (t_argc != 2)
	{
		script_error("setnodenext: wrong number of arguments\n");
		return;
	}

	mo = MobjForSvalue(t_argv[0]);
	nextmo = MobjForSvalue(t_argv[1]);
	
	// Check if both mobjs are NODE
	if (mo->type != MT_NODE || nextmo->type != MT_NODE)
	{
		script_error("setnodenext: mobj is not a node\n");
		return;
	}

	mo->nextnode = nextmo;

}


// Set the time to wait at a node
void SF_SetNodePause()
{

	mobj_t*		mo;


	if (t_argc != 2)
	{
		script_error("setnodepause: wrong number of arguments\n");
		return;
	}

	mo = MobjForSvalue(t_argv[0]);
	if (mo->type != MT_NODE)
	{
		script_error("setnodenext: mobj is not a node\n");
		return;
	}

	mo->nodewait = t_argv[1].value.i;

}


// Run a script when touching a node
void SF_SetNodeScript()
{
    mobj_t*  mo;
    int      sn;

    if (t_argc != 2)
    {
	script_error("setnodescript: wrong number of arguments\n");
	return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (mo->type != MT_NODE)
    {
	script_error("setnodescript: mobj is not a node\n");
	return;
    }

	  
    sn = intvalue(t_argv[1]);

    // Check if the script is defined
    if(!fs_levelscript.children[sn])
    {
	script_error("SetNodeScript: script not defined\n");
	return;
    }

    mo->nodescript = sn + 1;  // +1 because 0 = none
}



/****************** Trig *********************/

void SF_PointToAngle()
{
    angle_t angle;
    int x1, y1, x2, y2;

    if (t_argc < 4)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    x1 = intvalue(t_argv[0]) << FRACBITS;
    y1 = intvalue(t_argv[1]) << FRACBITS;
    x2 = intvalue(t_argv[2]) << FRACBITS;
    y2 = intvalue(t_argv[3]) << FRACBITS;

    angle = R_PointToAngle2(x1, y1, x2, y2);

    t_return.type = FSVT_fixed;
    t_return.value.f = AngleToFixed(angle);
}

void SF_PointToDist()
{
    int dist;
    int x1, x2, y1, y2;

    if (t_argc < 4)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    x1 = intvalue(t_argv[0]) << FRACBITS;
    y1 = intvalue(t_argv[1]) << FRACBITS;
    x2 = intvalue(t_argv[2]) << FRACBITS;
    y2 = intvalue(t_argv[3]) << FRACBITS;

    dist = R_PointToDist2(x1, y1, x2, y2);
    t_return.type = FSVT_fixed;
    t_return.value.f = dist;
}

/************* Camera functions ***************/

camera_t script_camera = { false, 0, 0, 0, 0, NULL };
boolean script_camera_on;

// setcamera(obj, [angle], [viewheight], [aiming])
void SF_SetCamera()
{
    if (t_argc < 1)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    mobj_t *mo = MobjForSvalue(t_argv[0]);
    if (!mo)
        return; // nullptr check

    if (script_camera.mo != mo)
    {
        if (script_camera.mo)
            script_camera.mo->angle = script_camera.startangle;

        script_camera.startangle = mo->angle;
    }

    script_camera.mo = mo;
    script_camera.mo->angle = t_argc < 2 ? mo->angle : FixedToAngle(fixedvalue(t_argv[1]));
    script_camera.mo->z = t_argc < 3 ? (mo->subsector->sector->floorheight + (41 << FRACBITS)) : fixedvalue(t_argv[2]);

    angle_t aiming = t_argc < 4 ? 0 : FixedToAngle(fixedvalue(t_argv[3]));
    script_camera.aiming = G_ClipAimingPitch(aiming);

    script_camera_on = true;
    t_return.type = FSVT_fixed;
    t_return.value.f = script_camera.aiming; // FIXME really?
}

void SF_ClearCamera()
{
    script_camera_on = false;
    if (!script_camera.mo)
    {
        script_error("Clearcamera: called without setcamera.\n");
        return;
    }

    script_camera.mo->angle = script_camera.startangle;
    script_camera.mo = NULL;
}

// movecamera(cameraobj, targetobj, targetheight, movespeed, targetangle, anglespeed)
void SF_MoveCamera()
{
    fixed_t x, y, z;
    fixed_t xdist, ydist, zdist, xydist, movespeed;
    fixed_t xstep, ystep, zstep, targetheight;
    angle_t anglespeed, anglestep = 0, angledist, targetangle, mobjangle, bigangle, smallangle;
    // I have to use floats for the math where angles are divided by fixed
    // values.
    double fangledist, fanglestep, fmovestep;
    int angledir = 0;
    mobj_t *camera;
    mobj_t *target;
    int moved = 0; // camera moved
    int quad1, quad2;

    if (t_argc < 6)
    {
        script_error("movecamera: insufficient arguments to function\n");
        return;
    }

    camera = MobjForSvalue(t_argv[0]);
    target = MobjForSvalue(t_argv[1]);
    targetheight = fixedvalue(t_argv[2]);
    movespeed = fixedvalue(t_argv[3]);
    targetangle = FixedToAngle(fixedvalue(t_argv[4]));
    anglespeed = FixedToAngle(fixedvalue(t_argv[5]));

    // Figure out how big the step will be
    xdist = target->x - camera->x;
    ydist = target->y - camera->y;
    zdist = targetheight - camera->z;

    // Angle checking...
    //    90
    //   Q1|Q0
    //180--+--0
    //   Q2|Q3
    //    270
    quad1 = targetangle / ANG90;
    quad2 = camera->angle / ANG90;
    bigangle = targetangle > camera->angle ? targetangle : camera->angle;
    smallangle = targetangle < camera->angle ? targetangle : camera->angle;
    if ((quad1 > quad2 && quad1 - 1 == quad2) || (quad2 > quad1 && quad2 - 1 == quad1) || quad1 == quad2)
    {
        angledist = bigangle - smallangle;
        angledir = targetangle > camera->angle ? 1 : -1;
    }
    else
    {
        if (quad2 == 3 && quad1 == 0)
        {
            angledist = (bigangle + ANG180) - (smallangle + ANG180);
            angledir = 1;
        }
        else if (quad1 == 3 && quad2 == 0)
        {
            angledist = (bigangle + ANG180) - (smallangle + ANG180);
            angledir = -1;
        }
        else
        {
            angledist = bigangle - smallangle;
            if (angledist > ANG180)
            {
                angledist = (bigangle + ANG180) - (smallangle + ANG180);
                angledir = targetangle > camera->angle ? -1 : 1;
            }
            else
                angledir = targetangle > camera->angle ? 1 : -1;
        }
    }

    //CONS_Printf("angle: cam=%i, target=%i; dir: %i; quads: 1=%i, 2=%i\n", camera->angle / ANGLE_1, targetangle / ANGLE_1, angledir, quad1, quad2);
    // set the step variables based on distance and speed...
    mobjangle = R_PointToAngle2(camera->x, camera->y, target->x, target->y);

    if (movespeed)
    {
        xydist = R_PointToDist2(camera->x, camera->y, target->x, target->y);
        xstep = FixedMul(finecosine[mobjangle >> ANGLETOFINESHIFT], movespeed);
        ystep = FixedMul(finesine[mobjangle >> ANGLETOFINESHIFT], movespeed);
        if (xydist)
            zstep = FixedDiv(zdist, FixedDiv(xydist, movespeed));
        else
            zstep = zdist > 0 ? movespeed : -movespeed;

        if (xydist && !anglespeed)
        {
            fangledist = ((double) angledist / ANGLE_1);
            fmovestep = ((double) FixedDiv(xydist, movespeed) / FRACUNIT);
            if (fmovestep)
                fanglestep = (fangledist / fmovestep);
            else
                fanglestep = 360;

            //CONS_Printf("fstep: %f, fdist: %f, fmspeed: %f, ms: %i\n", fanglestep, fangledist, fmovestep, FixedDiv(xydist, movespeed) >> FRACBITS);

            anglestep = (fanglestep * ANGLE_1);
        }
        else
            anglestep = anglespeed;

        if (abs(xstep) >= (abs(xdist) - 1))
            x = target->x;
        else
        {
            x = camera->x + xstep;
            moved = 1;
        }

        if (abs(ystep) >= (abs(ydist) - 1))
            y = target->y;
        else
        {
            y = camera->y + ystep;
            moved = 1;
        }

        if (abs(zstep) >= abs(zdist) - 1)
            z = targetheight;
        else
        {
            z = camera->z + zstep;
            moved = 1;
        }
    }
    else
    {
        x = camera->x;
        y = camera->y;
        z = camera->z;
    }

    if (anglestep >= angledist)
        camera->angle = targetangle;
    else
    {
        if (angledir == 1)
        {
            moved = 1;
            camera->angle += anglestep;
        }
        else if (angledir == -1)
        {
            moved = 1;
            camera->angle -= anglestep;
        }
    }

    if ((x != camera->x || y != camera->y) && !P_TryMove(camera, x, y, true))
    {
        script_error("Illegal camera move\n");
        return;
    }
    camera->z = z;

    t_return.type = FSVT_int;
    t_return.value.i = moved;
}

/*********** sounds ******************/

        // start sound from thing
void SF_StartSound()
{
    mobj_t *mo;

    if (t_argc < 2)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    if (t_argv[1].type != FSVT_string)
    {
        script_error("sound lump argument not a string!\n");
        return;
    }

    mo = MobjForSvalue(t_argv[0]);
    if (!mo)
        return;

    S_StartSoundName(mo, t_argv[1].value.s);
}

        // start sound from sector
void SF_StartSectorSound()
{
    sector_t *sector;
    int tagnum, secnum;

    if (t_argc < 2)
    {
        script_error("insufficient arguments to function\n");
        return;
    }
    if (t_argv[1].type != FSVT_string)
    {
        script_error("sound lump argument not a string!\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);
    // argv is sector tag

    secnum = P_FindSectorFromTag(tagnum, -1);

    if (secnum < 0)
    {
        script_error("sector not found with tagnum %i\n", tagnum);
        return;
    }

    secnum = -1; // init for search FindSector
    while ((secnum = P_FindSectorFromTag(tagnum, secnum)) >= 0)
    {
        // all sectors with the tagnum
        sector = &sectors[secnum];
        S_StartSoundName((mobj_t *) & sector->soundorg, t_argv[1].value.s);
    }
}

void SF_AmbiantSound()
{
    if (t_argc != 1)
    {
        script_error("insufficient arguments to function\n");
        return;
    }
    if (t_argv[0].type != FSVT_string)
    {
        script_error("sound lump argument not a string!\n");
        return;
    }

    S_StartSoundName(NULL, t_argv[0].value.s);
}

/************* Sector functions ***************/

		//sectoreffect(tagnum, [effect])

void SF_SectorEffect() //mainly copy/paste from p_spec
{
    int secnum = P_FindSectorFromTag(intvalue(t_argv[0]), -1);
    int tagnum = intvalue(t_argv[0]);
    int value = intvalue(t_argv[1]);
    int i;
    //sector_t *sec = &sectors[secnum];
    sector_t *sector;
	
    if (!t_argc)
    {
        script_error("sectoreffect: arguments missing\n");
        return;
    }
    //  Init special SECTORs.

    sector = sectors; // & sectors[0]
    for (i=0 ; i<numsectors ; i++, sector++)
    {
        // for all sectors
        if (sector->tag != tagnum)
           continue;

        if (sector->special&SECRET_MASK) //SoM: 3/8/2000: count secret flags
          totalsecret++;

        switch (value)
        {
	  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 8:
	    // GLOWING LIGHT
	    P_SpawnGlowingLight(sector);
	    break;

	  //case 9:
	    // SECRET SECTOR
	    //if(sector->special<32)
	    //  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, secnum);
	    break;

	  case 17:
	    //LIGHT FLICKERS RANDOMLY
	    P_SpawnFireFlicker(sector);
	    break;
	}
    }


    t_return.type = FSVT_int;
    t_return.value.i = intvalue(t_argv[1]);
}
        // floor height of sector

void SF_FloorHeight()
{
    sector_t *sector;
    int tagnum;
    int secnum;
    int returnval = 1;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);

    // argv is sector tag
    secnum = P_FindSectorFromTag(tagnum, -1);

    if (secnum < 0)
    {
        script_error("sector not found with tagnum %i\n", tagnum);
        return;
    }

    sector = &sectors[secnum];

    if (t_argc > 1)     // > 1: set floorheight
    {
        int i = -1;
        boolean crush = t_argc == 3 ? intvalue(t_argv[2]) : false;

        // set all sectors with tag
        while ((i = P_FindSectorFromTag(tagnum, i)) >= 0)
        {
            //sectors[i].floorheight = intvalue(t_argv[1]) << FRACBITS;
            if (T_MovePlane(&sectors[i], abs(fixedvalue(t_argv[1]) - sectors[i].floorheight), fixedvalue(t_argv[1]), crush, 0, fixedvalue(t_argv[1]) > sectors[i].floorheight ? 1 : -1) == crushed)
                returnval = 0;
        }
    }
    else
        returnval = sectors[secnum].floorheight >> FRACBITS;

    // return floorheight

    t_return.type = FSVT_int;
    t_return.value.i = returnval;
}

void SF_MoveFloor()
{
    int secnum = -1;  // init search FindSector
    sector_t *sec;
    floormove_t * mfloor;
    int tagnum, platspeed = 1, destheight;

    if (t_argc < 2)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);
    destheight = intvalue(t_argv[1]) << FRACBITS;
    platspeed = FLOORSPEED * (t_argc > 2 ? intvalue(t_argv[2]) : 1);

    // move all sectors with tag

    while ((secnum = P_FindSectorFromTag(tagnum, secnum)) >= 0)
    {
        sec = &sectors[secnum];

        // Don't start a second thinker on the same floor
        if (P_SectorActive(floor_special, sec))
            continue;

        mfloor = Z_Malloc(sizeof(floormove_t), PU_LEVSPEC, 0);
        P_AddThinker(&mfloor->thinker);
        sec->floordata = mfloor;
        mfloor->thinker.function.acp1 = (actionf_p1) T_MoveFloor;
        mfloor->type = -1;       // not done by line
        mfloor->crush = false;

        mfloor->direction = destheight < sec->floorheight ? -1 : 1;
        mfloor->sector = sec;
        mfloor->speed = platspeed;
        mfloor->floordestheight = destheight;
    }
}

        // ceiling height of sector
void SF_CeilingHeight()
{
    sector_t *sector;
    int secnum;
    int tagnum;
    int returnval = 1;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);

    // argv is sector tag
    secnum = P_FindSectorFromTag(tagnum, -1);

    if (secnum < 0)
    {
        script_error("sector not found with tagnum %i\n", tagnum);
        return;
    }

    sector = &sectors[secnum];

    if (t_argc > 1)     // > 1: set ceilheight
    {
        int fsecn = -1; // init search FindSector
        boolean crush = (t_argc == 3)? intvalue(t_argv[2]) : false;

        // set all sectors with tag
        while ((fsecn = P_FindSectorFromTag(tagnum, fsecn)) >= 0)
        {
            //sectors[i].ceilingheight = intvalue(t_argv[1]) << FRACBITS;
            if (T_MovePlane(&sectors[fsecn],
			    abs(fixedvalue(t_argv[1]) - sectors[fsecn].ceilingheight),
			    fixedvalue(t_argv[1]), crush, 1,
			    fixedvalue(t_argv[1]) > sectors[fsecn].ceilingheight ? 1 : -1) == crushed)
                returnval = 0;
        }
    }
    else
        returnval = sectors[secnum].ceilingheight >> FRACBITS;

    // return floorheight
    t_return.type = FSVT_int;
    t_return.value.i = returnval;
}

void SF_MoveCeiling()
{
    int secnum = -1; // init search FindSector
    sector_t *sec;
    ceiling_t *ceiling;
    int tagnum, platspeed = 1, destheight;

    if (t_argc < 2)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);
    destheight = intvalue(t_argv[1]) << FRACBITS;
    platspeed = FLOORSPEED * (t_argc > 2 ? intvalue(t_argv[2]) : 1);

    // move all sectors with tag

    while ((secnum = P_FindSectorFromTag(tagnum, secnum)) >= 0)
    {
        sec = &sectors[secnum];

        // Don't start a second thinker on the same floor
        if (P_SectorActive(ceiling_special, sec))
            continue;

        ceiling = Z_Malloc(sizeof(*ceiling), PU_LEVSPEC, 0);
        P_AddThinker(&ceiling->thinker);
        sec->ceilingdata = ceiling;
        ceiling->thinker.function.acp1 = (actionf_p1) T_MoveCeiling;
        ceiling->type = genCeiling;     // not done by line
        ceiling->crush = false;

        ceiling->direction = destheight < sec->ceilingheight ? -1 : 1;
        ceiling->sector = sec;
        ceiling->speed = platspeed;
        // just set top and bottomheight the same
        ceiling->topheight = ceiling->bottomheight = destheight;

        ceiling->tag = sec->tag;
        P_AddActiveCeiling(ceiling);
    }
}

void SF_LightLevel()
{
    sector_t *sector;
    int secnum;
    int tagnum;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);

    // argv is sector tag
    secnum = P_FindSectorFromTag(tagnum, -1);

    if (secnum < 0)
    {
        script_error("sector not found with tagnum %i\n", tagnum);
        return;
    }

    sector = &sectors[secnum];

    if (t_argc > 1)     // > 1: set ceilheight
    {
        int fsecn = -1; // init search FindSector

        // set all sectors with tag
        while ((fsecn = P_FindSectorFromTag(tagnum, fsecn)) >= 0)
        {
	    // all sectors with tagnum
            sectors[fsecn].lightlevel = intvalue(t_argv[1]);
        }
    }

    // return lightlevel
    t_return.type = FSVT_int;
    t_return.value.i = sector->lightlevel;
}

void SF_FadeLight()
{
    int sectag, destlevel, speed = 1;

    if (t_argc < 2)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    sectag = intvalue(t_argv[0]);
    destlevel = intvalue(t_argv[1]);
    speed = t_argc > 2 ? intvalue(t_argv[2]) : 1;

    P_FadeLight(sectag, destlevel, speed);
}

void SF_FloorTexture()
{
    int tagnum, secnum;
    sector_t *sector;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);

    // argv is sector tag
    secnum = P_FindSectorFromTag(tagnum, -1);

    if (secnum < 0)
    {
        script_error("sector not found with tagnum %i\n", tagnum);
        return;
    }

    sector = &sectors[secnum];

    if (t_argc > 1)
    {
        int i = -1;
        int picnum = R_FlatNumForName(t_argv[1].value.s);

        // set all sectors with tag
        while ((i = P_FindSectorFromTag(tagnum, i)) >= 0)
        {
            sectors[i].floorpic = picnum;
        }
    }

    t_return.type = FSVT_string;
    t_return.value.s = P_FlatNameForNum(sectors[secnum].floorpic);
}

void SF_SectorColormap()
{
    int tagnum, secnum;
    sector_t *sector;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);

    // argv is sector tag
    secnum = P_FindSectorFromTag(tagnum, -1);

    if (secnum < 0)
    {
        script_error("sector not found with tagnum %i\n", tagnum);
        return;
    }

    sector = &sectors[secnum];

    if (t_argc > 1)
    {
        int i = -1;
        int mapnum = R_ColormapNumForName(t_argv[1].value.s);

        // set all sectors with tag
        while ((i = P_FindSectorFromTag(tagnum, i)) >= 0)
        {
            if (mapnum == -1)
            {
                sectors[i].midmap = 0;
                sectors[i].model = SM_normal;
                sectors[i].modelsec = 0;
            }
            else
            {
                sectors[i].midmap = mapnum;
                sectors[i].model = SM_colormap;
                sectors[i].modelsec = 0;
            }
        }
    }

    t_return.type = FSVT_string;
    t_return.value.s = R_ColormapNameForNum(sector->midmap);
}

void SF_CeilingTexture()
{
    int tagnum, secnum;
    sector_t *sector;

    if (!t_argc)
    {
        script_error("insufficient arguments to function\n");
        return;
    }

    tagnum = intvalue(t_argv[0]);

    // argv is sector tag
    secnum = P_FindSectorFromTag(tagnum, -1);

    if (secnum < 0)
    {
        script_error("sector not found with tagnum %i\n", tagnum);
        return;
    }

    sector = &sectors[secnum];

    if (t_argc > 1)
    {
        int i = -1;
        int picnum = R_FlatNumForName(t_argv[1].value.s);

        // set all sectors with tag
        while ((i = P_FindSectorFromTag(tagnum, i)) >= 0)
        {
            sectors[i].ceilingpic = picnum;
        }
    }

    t_return.type = FSVT_string;
    t_return.value.s = P_FlatNameForNum(sectors[secnum].ceilingpic);
}

void SF_ChangeHubLevel()
{
/*  int tagnum;

  if(!t_argc)
    {
      script_error("hub level to go to not specified!\n");
      return;
    }
  if(t_argv[0].type != FSVT_string)
    {
      script_error("level argument is not a string!\n");
      return;
    }

  // second argument is tag num for 'seamless' travel
  if(t_argc > 1)
    tagnum = intvalue(t_argv[1]);
  else
    tagnum = -1;

  P_SavePlayerPosition(fs_current_script->trigger->player, tagnum);
  P_ChangeHubLevel(t_argv[0].value.s);*/
}

// for start map: start new game on a particular skill
void SF_StartSkill()
{
    int skill;

    if (t_argc < 1)
    {
        script_error("need skill level to start on\n");
        return;
    }

    // -1: 1-5 is how we normally see skills
    // 0-4 is how doom sees them

    skill = intvalue(t_argv[0]) - 1;

    G_DeferedInitNew(skill, G_BuildMapName(1, 1), false);
}

//////////////////////////////////////////////////////////////////////////
//
// Doors
//

// opendoor(sectag, [speed], [delay])

void SF_OpenDoor()
{
    int speed, wait_time;
    int sectag;

    if (t_argc < 1)
    {
        script_error("need sector tag for door to open\n");
        return;
    }

    // got sector tag
    sectag = intvalue(t_argv[0]);

    // door wait time

    if (t_argc > 1)     // door wait time
        wait_time = (intvalue(t_argv[1]) * 35) / 100;
    else
        wait_time = 0;  // 0= stay open

    // door speed

    if (t_argc > 2)
        speed = intvalue(t_argv[2]);
    else
        speed = 1;      // 1= normal speed

    EV_OpenDoor(sectag, speed, wait_time);
}

void SF_CloseDoor()
{
    int speed;
    int sectag;

    if (t_argc < 1)
    {
        script_error("need sector tag for door to open\n");
        return;
    }

    // got sector tag
    sectag = intvalue(t_argv[0]);

    // door speed

    if (t_argc > 1)
        speed = intvalue(t_argv[1]);
    else
        speed = 1;      // 1= normal speed

    EV_CloseDoor(sectag, speed);
}

// play demo, internal lump, should support external too

void SF_PlayDemo()
{
    if (t_argc != 1)
    {
        script_error("playdemo: invalid number of arguments\n");
        return;
    }
    if (t_argv[0].type != FSVT_string)
    {
        script_error("playdemo: not a lump name");
        return;
    }

    G_DoPlayDemo(t_argv[0].value.s);
}

// run console cmd
void SF_RunCommand()
{
    char * tempstr = Z_cat_args(0);
    COM_BufAddText(tempstr);
    Z_Free(tempstr);
}

// return the (string) value of a cvar
void SF_CheckCVar()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        consvar_t *cvar;

        t_return.type = FSVT_string;
        if ((cvar = CV_FindVar(stringvalue(t_argv[0]))))
        {
            t_return.value.s = cvar->string;
        }
        else
        {
            t_return.value.s = "";
        }
    }
}


//DarkWolf95:July 23, 2003:Return/Set LineTexture Yay!
//linetexture(tag, texture, side, sections)
// sections: 1 = top 2 = mid 4 = bot

void SF_SetLineTexture()
{
  int tagnum, linenum, side, sections;
  line_t *line;

  if(t_argc != 4)
  {
     script_error("insufficient arguments to function\n");
     return;
  }

  tagnum = intvalue(t_argv[0]);

  // argv is sector tag
  linenum = P_FindLineFromTag(tagnum, -1);

  if(linenum < 0)
  {
     script_error("line not found with tagnum %i\n", tagnum);
     return;
  }

  line = &lines[linenum];

  if(t_argc > 1)
  {
      int i = -1;
      short picnum = R_TextureNumForName(t_argv[1].value.s);
        // returns 0=no-texture, or default texture, otherwise valid

      side = intvalue(t_argv[2]); // FIXME [smite] 0,1 only
      sections = intvalue(t_argv[3]);

      // set all sectors with tag
      while ((i = P_FindLineFromTag(tagnum, i)) >= 0)
      {
	  if (lines[i].sidenum[side] == NULL_INDEX)
	  {
	      script_error("nonexistant side\n");
	      return;
	  }
	  else
	  {
	      // textures, 0=no-texture, otherwise valid
	      if(sections & 1)
	         sides[lines[i].sidenum[side]].toptexture = picnum;
	      if(sections & 2)
	         sides[lines[i].sidenum[side]].midtexture = picnum;
	      if(sections & 4)
	         sides[lines[i].sidenum[side]].bottomtexture = picnum;
	  }
      }
  }
}

// any linedef type

void SF_LineTrigger()
{
    line_t junk;

    if (!t_argc)
    {
        script_error("need line trigger type\n");
        return;
    }

    junk.special = intvalue(t_argv[0]);
    junk.tag = t_argc < 2 ? 0 : intvalue(t_argv[1]);

    P_UseSpecialLine(fs_run_trigger, &junk, 0);      // Try using it
    P_ActivateCrossedLine(&junk, 0, fs_run_trigger); // Try crossing it
}

void SF_LineFlag()
{
    line_t *line;
    int linenum;
    int flagnum;

    if (t_argc < 2)
    {
        script_error("LineFlag: missing parameters\n");
        return;
    }

    linenum = intvalue(t_argv[0]);
    if (linenum < 0 || linenum > numlines)
    {
        script_error("LineFlag: Invalid line number.\n");
        return;
    }

    line = lines + linenum;

    flagnum = intvalue(t_argv[1]);
    if (flagnum < 0 || flagnum > 32)
    {
        script_error("LineFlag: Invalid flag number.\n");
        return;
    }

    if (t_argc > 2)
    {
        line->flags &= ~(1 << flagnum);
        if (intvalue(t_argv[2]))
            line->flags |= (1 << flagnum);
    }

    t_return.type = FSVT_int;
    t_return.value.i = line->flags & (1 << flagnum);
}

void SF_ChangeMusic()
{
    if (!t_argc)
    {
        script_error("need new music name\n");
        return;
    }
    if (t_argv[0].type != FSVT_string)
    {
        script_error("incorrect argument to function\n");
        return;
    }

    S_ChangeMusicName(t_argv[0].value.s, 1);
}

// SoM: Max and Min math functions.
void SF_Max()
{
    fixed_t n1, n2;

    if (t_argc != 2)
    {
        script_error("invalid number of arguments\n");
        return;
    }

    n1 = fixedvalue(t_argv[0]);
    n2 = fixedvalue(t_argv[1]);

    t_return.type = FSVT_fixed;
    t_return.value.f = n1 > n2 ? n1 : n2;
}

void SF_Min()
{
    fixed_t n1, n2;

    if (t_argc != 2)
    {
        script_error("invalid number of arguments\n");
        return;
    }

    n1 = fixedvalue(t_argv[0]);
    n2 = fixedvalue(t_argv[1]);

    t_return.type = FSVT_fixed;
    t_return.value.f = n1 < n2 ? n1 : n2;
}

void SF_Abs()
{
    fixed_t n1;

    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
        return;
    }

    n1 = fixedvalue(t_argv[0]);

    t_return.type = FSVT_fixed;
    t_return.value.f = n1 < 0 ? n1 * -1 : n1;
}

//Hurdler: some new math functions
fixed_t double2fixed(double t)
{
    double fl = floor(t);
    return ((int) fl << 16) | (int) ((t - fl) * 65536.0);
}

void SF_Sin()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(sin(FIXED_TO_FLOAT(n1)));
    }
}

void SF_ASin()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(asin(FIXED_TO_FLOAT(n1)));
    }
}

void SF_Cos()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(cos(FIXED_TO_FLOAT(n1)));
    }
}

void SF_ACos()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(acos(FIXED_TO_FLOAT(n1)));
    }
}

void SF_Tan()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(tan(FIXED_TO_FLOAT(n1)));
    }
}

void SF_ATan()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(atan(FIXED_TO_FLOAT(n1)));
    }
}

void SF_Exp()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(exp(FIXED_TO_FLOAT(n1)));
    }
}

void SF_Log()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(log(FIXED_TO_FLOAT(n1)));
    }
}

void SF_Sqrt()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = double2fixed(sqrt(FIXED_TO_FLOAT(n1)));
    }
}

void SF_Floor()
{
    if (t_argc != 1)
    {
        script_error("invalid number of arguments\n");
    }
    else
    {
        fixed_t n1 = fixedvalue(t_argv[0]);
        t_return.type = FSVT_fixed;
        t_return.value.f = n1 & 0xffFF0000;
    }
}

void SF_Pow()
{
    fixed_t n1, n2;

    if (t_argc != 2)
    {
        script_error("invalid number of arguments\n");
        return;
    }

    n1 = fixedvalue(t_argv[0]);
    n2 = fixedvalue(t_argv[1]);

    t_return.type = FSVT_fixed;
    t_return.value.f = double2fixed(pow(FIXED_TO_FLOAT(n1), FIXED_TO_FLOAT(n2)));
}




// Type forcing functions -- useful with arrays et al

void SF_MobjValue(void)
{
   if(t_argc != 1)
   {
      script_error("incorrect arguments to function\n");
      return;
   }
   t_return.type = FSVT_mobj;
   t_return.value.mobj = MobjForSvalue(t_argv[0]);
}

void SF_StringValue(void)
{  
   if(t_argc != 1)
   {
      script_error("incorrect arguments to function\n");
      return;
   }
   t_return.type = FSVT_string;
   t_return.value.s = Z_Strdup(stringvalue(t_argv[0]), PU_LEVEL, 0);
}

void SF_IntValue(void)
{
   if(t_argc != 1)
   {
      script_error("incorrect arguments to function\n");
      return;
   }
   t_return.type = FSVT_int;
   t_return.value.i = intvalue(t_argv[0]);
}

void SF_FixedValue(void)
{
   if(t_argc != 1)
   {
      script_error("incorrect arguments to function\n");
      return;
   }
   t_return.type = FSVT_fixed;
   t_return.value.f = fixedvalue(t_argv[0]);
}




//////////////////////////////////////////////////////////////////////////
// FraggleScript HUD graphics
//////////////////////////////////////////////////////////////////////////
int HU_GetFSPic(int lumpnum, int xpos, int ypos);
int HU_DeleteFSPic(int handle);
int HU_ModifyFSPic(int handle, int lumpnum, int xpos, int ypos);
int HU_FSDisplay(int handle, boolean newval);

void SF_NewHUPic()
{
    if (t_argc != 3)
    {
        script_error("newhupic: invalid number of arguments\n");
        return;
    }

    t_return.type = FSVT_int;
    t_return.value.i = HU_GetFSPic(W_GetNumForName(stringvalue(t_argv[0])), intvalue(t_argv[1]), intvalue(t_argv[2]));
    return;
}

void SF_DeleteHUPic()
{
    if (t_argc != 1)
    {
        script_error("deletehupic: Invalid number if arguments\n");
        return;
    }

    if (HU_DeleteFSPic(intvalue(t_argv[0])) == -1)
        script_error("deletehupic: Invalid sfpic handle: %i\n", intvalue(t_argv[0]));
    return;
}

void SF_ModifyHUPic()
{
    if (t_argc != 4)
    {
        script_error("modifyhupic: invalid number of arguments\n");
        return;
    }

    if (HU_ModifyFSPic(intvalue(t_argv[0]), W_GetNumForName(stringvalue(t_argv[1])), intvalue(t_argv[2]), intvalue(t_argv[3])) == -1)
        script_error("modifyhypic: invalid sfpic handle %i\n", intvalue(t_argv[0]));
    return;
}

void SF_SetHUPicDisplay()
{
    if (t_argc != 2)
    {
        script_error("sethupicdisplay: invalud number of arguments\n");
        return;
    }

    if (HU_FSDisplay(intvalue(t_argv[0]), intvalue(t_argv[1]) > 0 ? 1 : 0) == -1)
        script_error("sethupicdisplay: invalid pic handle %i\n", intvalue(t_argv[0]));
}

// Hurdler: I'm enjoying FS capability :)

#ifdef HWRENDER
extern light_t lspr[];

int String2Hex(char *s)
{
#define HEX2INT(x) (x >= '0' && x <= '9' ? x - '0' : x >= 'a' && x <= 'f' ? x - 'a' + 10 : x >= 'A' && x <= 'F' ? x - 'A' + 10 : 0)
    return (HEX2INT(s[0]) << 4) + (HEX2INT(s[1]) << 0) + (HEX2INT(s[2]) << 12) + (HEX2INT(s[3]) << 8) + (HEX2INT(s[4]) << 20) + (HEX2INT(s[5]) << 16) + (HEX2INT(s[6]) << 28) + (HEX2INT(s[7]) << 24);
#undef HEX2INT
}

void SF_SetCorona()
{
    if (rendermode == render_soft)
        return; // do nothing in software mode
    if (t_argc != 3 && t_argc != 7)
    {
        script_error("Incorrect parameters.\n");
        return;
    }
    //this function accept 2 kinds of parameters
    if (t_argc == 3)
    {
        int num = t_argv[0].value.i;    // which corona we want to modify
        int what = t_argv[1].value.i;   // what we want to modify (type, color, offset,...)
        int ival = t_argv[2].value.i;   // the value of what we modify
        double fval = ((double) t_argv[2].value.f / FRACUNIT);

        switch (what)
        {
            case 0:
                lspr[num].type = ival;
                break;
            case 1:
                lspr[num].light_xoffset = fval;
                break;
            case 2:
                lspr[num].light_yoffset = fval;
                break;
            case 3:
                if (t_argv[2].type == FSVT_string)
                    lspr[num].corona_color = String2Hex(t_argv[2].value.s);
                else
                    memcpy(&lspr[num].corona_color, &ival, sizeof(int));
                break;
            case 4:
                lspr[num].corona_radius = fval;
                break;
            case 5:
                if (t_argv[2].type == FSVT_string)
                    lspr[num].dynamic_color = String2Hex(t_argv[2].value.s);
                else
                    memcpy(&lspr[num].dynamic_color, &ival, sizeof(int));
                break;
            case 6:
                lspr[num].dynamic_radius = fval;
                lspr[num].dynamic_sqrradius = sqrt(lspr[num].dynamic_radius);
                break;
            default:
                CONS_Printf("Error in setcorona\n");
                break;
        }
    }
    else
    {
        int num = t_argv[0].value.i;    // which corona we want to modify
        lspr[num].type = t_argv[1].value.i;
        lspr[num].light_xoffset = t_argv[2].value.f;
        lspr[num].light_yoffset = t_argv[3].value.f;
        if (t_argv[4].type == FSVT_string)
            lspr[num].corona_color = String2Hex(t_argv[4].value.s);
        else
            memcpy(&lspr[num].corona_color, &t_argv[4].value.i, sizeof(int));
        lspr[num].corona_radius = t_argv[5].value.f;
        if (t_argv[6].type == FSVT_string)
            lspr[num].dynamic_color = String2Hex(t_argv[6].value.s);
        else
            memcpy(&lspr[num].dynamic_color, &t_argv[6].value.i, sizeof(int));
        lspr[num].dynamic_radius = t_argv[7].value.f;
        lspr[num].dynamic_sqrradius = sqrt(lspr[num].dynamic_radius);
    }
}


void SF_SetFade()
{

	int r = 0;
	int g = 0;
	int b = 0;
	int alpha = 0;


	r = (unsigned long)t_argv[0].value.i;
	g = (unsigned long)t_argv[1].value.i;
	b = (unsigned long)t_argv[2].value.i;
	alpha = t_argv[3].value.i;

	// Calculate the color value
	fadecolor = (256 * b) + (65536 * g) + (16777216 * r);
	fadealpha = alpha;

}

#endif

//////////////////////////////////////////////////////////////////////////
//
// Init Functions
//

//extern int fov; // r_main.c
int fov;

void init_functions()
{
    // add all the functions
    add_game_int("consoleplayer", &consoleplayer);
    add_game_int("displayplayer", &displayplayer);
    add_game_int("fov", &fov);
    add_game_int("zoom", &fov); //SoM: BAKWARDS COMPATABILITY!
    add_game_mobj("trigger", &fs_trigger_obj);

    // important C-emulating stuff
    new_function("break", SF_Break);
    new_function("continue", SF_Continue);
    new_function("return", SF_Return);
    new_function("goto", SF_Goto);
    new_function("include", SF_Include);

    // standard FraggleScript functions
    new_function("print", SF_Print);
    new_function("rnd", SF_Rnd);
    new_function("prnd", SF_PRnd);
    new_function("input", SF_Input);    // Hurdler: TODO: document this function
    new_function("beep", SF_Beep);
    new_function("clock", SF_Clock);
    new_function("clocktic", SF_ClockTic);
    new_function("wait", SF_Wait);
    new_function("waittic", SF_WaitTic);
    new_function("tagwait", SF_TagWait);
    new_function("scriptwait", SF_ScriptWait);
    new_function("startscript", SF_StartScript);
    new_function("scriptrunning", SF_ScriptRunning);

    // doom stuff
    new_function("startskill", SF_StartSkill);
    new_function("exitlevel", SF_ExitLevel);
    new_function("warp", SF_Warp);
    new_function("tip", SF_Tip);
    new_function("timedtip", SF_TimedTip);
    new_function("message", SF_Message);
    new_function("gameskill", SF_GameSkill);
    new_function("gamemode", SF_GameMode);      // SoM Request SSNTails 06-13-2002

    // player stuff
    new_function("playermsg", SF_PlayerMsg);
    new_function("playertip", SF_PlayerTip);
    new_function("playeringame", SF_PlayerInGame);
    new_function("playername", SF_PlayerName);
    new_function("playeraddfrag", SF_PlayerAddFrag);
    new_function("playerobj", SF_PlayerObj);
    new_function("isobjplayer", SF_MobjIsPlayer);
    new_function("isplayerobj", SF_MobjIsPlayer);       // Hurdler: due to backward and eternity compatibility
    new_function("skincolor", SF_SkinColor);
    new_function("playerkeys", SF_PlayerKeys);
    new_function("playerkeysb", SF_PlayerKeysByte);
    new_function("playerarmor", SF_PlayerArmor);
    new_function("playerammo", SF_PlayerAmmo);
    new_function("maxplayerammo", SF_MaxPlayerAmmo);
    new_function("playerweapon", SF_PlayerWeapon);
    new_function("playerselwep", SF_PlayerSelectedWeapon);
    new_function("playerpitch", SF_PlayerPitch);
    new_function("playerproperty", SF_PlayerProperty);

    // mobj stuff
    new_function("spawn", SF_Spawn);
    new_function("spawnexplosion", SF_SpawnExplosion);
    new_function("radiusattack", SF_RadiusAttack);
    new_function("kill", SF_KillObj);
    new_function("removeobj", SF_RemoveObj);
    new_function("objx", SF_ObjX);
    new_function("objy", SF_ObjY);
    new_function("objz", SF_ObjZ);
    new_function("testlocation", SF_TestLocation);
    new_function("teleport", SF_Teleport);
    new_function("silentteleport", SF_SilentTeleport);
    new_function("damageobj", SF_DamageObj);
    new_function("healobj", SF_HealObj);
    new_function("player", SF_Player);
    new_function("objsector", SF_ObjSector);
    new_function("objflag", SF_ObjFlag);
    new_function("objflag2", SF_ObjFlag2);
    new_function("objeflag", SF_ObjEFlag);
    new_function("pushobj", SF_PushThing);
    new_function("pushthing", SF_PushThing);    // Hurdler: due to backward and eternity compatibility
    new_function("objangle", SF_ObjAngle);
    new_function("checksight", SF_CheckSight);
    new_function("objhealth", SF_ObjHealth);
    new_function("objdead", SF_ObjDead);
    new_function("objreactiontime", SF_ReactionTime);
    new_function("reactiontime", SF_ReactionTime);      // Hurdler: due to backward and eternity compatibility
    new_function("objtarget", SF_MobjTarget);
    new_function("objmomx", SF_MobjMomx);
    new_function("objmomy", SF_MobjMomy);
    new_function("objmomz", SF_MobjMomz);
    new_function("spawnmissile", SF_SpawnMissile);
    new_function("mapthings", SF_MapThings);
    new_function("objtype", SF_ObjType);
    new_function("mapthingnumexist", SF_MapThingNumExist);
    new_function("objstate", SF_ObjState);
    new_function("resurrect", SF_Resurrect);
    new_function("lineattack", SF_LineAttack);
    new_function("setobjposition", SF_SetObjPosition);
    new_function("setobjproperty", SF_SetObjProperty);
    new_function("getobjproperty", SF_GetObjProperty);
    new_function("setnodenext", SF_SetNodeNext);
    new_function("setnodewait", SF_SetNodePause);
    new_function("setnodescript", SF_SetNodeScript);

    // sector stuff
    new_function("sectoreffect", SF_SectorEffect);
    new_function("floorheight", SF_FloorHeight);
    new_function("floortext", SF_FloorTexture);
    new_function("floortexture", SF_FloorTexture);      // Hurdler: due to backward and eternity compatibility
    new_function("movefloor", SF_MoveFloor);
    new_function("ceilheight", SF_CeilingHeight);
    new_function("ceilingheight", SF_CeilingHeight);    // Hurdler: due to backward and eternity compatibility
    new_function("moveceil", SF_MoveCeiling);
    new_function("moveceiling", SF_MoveCeiling);        // Hurdler: due to backward and eternity compatibility
    new_function("ceiltext", SF_CeilingTexture);
    new_function("ceilingtexture", SF_CeilingTexture);  // Hurdler: due to backward and eternity compatibility
    new_function("lightlevel", SF_LightLevel);
    new_function("fadelight", SF_FadeLight);
    new_function("colormap", SF_SectorColormap);

    // cameras!
    new_function("setcamera", SF_SetCamera);
    new_function("clearcamera", SF_ClearCamera);
    new_function("movecamera", SF_MoveCamera);

    // trig functions
    new_function("pointtoangle", SF_PointToAngle);
    new_function("pointtodist", SF_PointToDist);

    // sound functions
    new_function("startsound", SF_StartSound);
    new_function("startsectorsound", SF_StartSectorSound);
    new_function("startambiantsound", SF_AmbiantSound);
    new_function("ambientsound", SF_AmbiantSound);      // Hurdler: due to backward and eternity compatibility
    new_function("changemusic", SF_ChangeMusic);

    // hubs!
    new_function("changehublevel", SF_ChangeHubLevel);  // Hurdler: TODO: document this function

    // doors
    new_function("opendoor", SF_OpenDoor);
    new_function("closedoor", SF_CloseDoor);

    new_function("playdemo", SF_PlayDemo);
    new_function("runcommand", SF_RunCommand);
    new_function("checkcvar", SF_CheckCVar);
    new_function("setlinetexture", SF_SetLineTexture);
    new_function("linetrigger", SF_LineTrigger);
    new_function("lineflag", SF_LineFlag);

    new_function("max", SF_Max);
    new_function("min", SF_Min);
    new_function("abs", SF_Abs);

    //Hurdler: new math functions
    new_function("sin", SF_Sin);
    new_function("asin", SF_ASin);
    new_function("cos", SF_Cos);
    new_function("acos", SF_ACos);
    new_function("tan", SF_Tan);
    new_function("atan", SF_ATan);
    new_function("exp", SF_Exp);
    new_function("log", SF_Log);
    new_function("sqrt", SF_Sqrt);
    new_function("floor", SF_Floor);
    new_function("pow", SF_Pow);

    // forced coercion functions
    new_function("mobjvalue", SF_MobjValue);
    new_function("stringvalue", SF_StringValue);
    new_function("intvalue", SF_IntValue);
    new_function("fixedvalue", SF_FixedValue);

    // HU Graphics
    new_function("newhupic", SF_NewHUPic);
    new_function("createpic", SF_NewHUPic);
    new_function("deletehupic", SF_DeleteHUPic);
    new_function("modifyhupic", SF_ModifyHUPic);
    new_function("modifypic", SF_ModifyHUPic);
    new_function("sethupicdisplay", SF_SetHUPicDisplay);
    new_function("setpicvisible", SF_SetHUPicDisplay);

    // Arrays
    new_function("newarray", SF_NewArray);
    new_function("newemptyarray", SF_NewEmptyArray);
    new_function("copyinto", SF_ArrayCopyInto);
    new_function("elementat", SF_ArrayElementAt);
    new_function("setelementat", SF_ArraySetElementAt);
    new_function("length", SF_ArrayLength);

    // Hurdler's stuff :)
#ifdef HWRENDER
    new_function("setcorona", SF_SetCorona);
    new_function("setfade", SF_SetFade);
#endif
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.