summaryrefslogtreecommitdiff
path: root/particles/builtin_initializers.cpp
blob: bdfde7bc949e1025f582b0c02632203429285e69 (plain) (blame)
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
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: particle system code
//
//===========================================================================//

#include "tier0/platform.h"
#include "particles/particles.h"
#include "filesystem.h"
#include "tier2/tier2.h"
#include "tier2/fileutils.h"
#include "tier2/renderutils.h"
#include "tier1/UtlStringMap.h"
#include "tier1/strtools.h"
#include "dmxloader/dmxelement.h"
#include "psheet.h"
#include "bspflags.h"
#include "const.h"
#include "particles_internal.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


void CParticleOperatorInstance::InitScalarAttributeRandomRangeBlock( 
	int attr_num, float fMin, float fMax,
	CParticleCollection *pParticles, int start_block, int n_blocks ) const
{
	size_t attr_stride;
	fltx4 *pAttr = pParticles->GetM128AttributePtrForWrite( attr_num, &attr_stride );
	pAttr += attr_stride * start_block;
	fltx4 val0 = ReplicateX4( fMin );
	fltx4 val_d = ReplicateX4( fMax - fMin );
	int nRandContext = GetSIMDRandContext();
	while( n_blocks-- )
	{
		*( pAttr ) = AddSIMD( val0, MulSIMD( RandSIMD( nRandContext ), val_d ) );
		pAttr += attr_stride;
	}
	ReleaseSIMDRandContext( nRandContext );

}

void CParticleOperatorInstance::InitScalarAttributeRandomRangeExpBlock( 
	int attr_num, float fMin, float fMax, float fExp,
	CParticleCollection *pParticles, int start_block, int n_blocks ) const
{
	size_t attr_stride;
	fltx4 *pAttr = pParticles->GetM128AttributePtrForWrite( attr_num, &attr_stride );
	pAttr += attr_stride * start_block;
	fltx4 val0 = ReplicateX4( fMin );
	fltx4 val_d = ReplicateX4( fMax - fMin );
	//fltx4 val_e = ReplicateX4( fExp );
	int nExp = (int)(4.0f * fExp);
	int nRandContext = GetSIMDRandContext();
	while( n_blocks-- )
	{
		*( pAttr ) = AddSIMD( val0, MulSIMD( Pow_FixedPoint_Exponent_SIMD( RandSIMD( nRandContext ), nExp ), val_d ) );
		pAttr += attr_stride;
	}
	ReleaseSIMDRandContext( nRandContext );
}

void CParticleOperatorInstance::AddScalarAttributeRandomRangeBlock( 
	int nAttributeId, float fMin, float fMax, float fExp,
	CParticleCollection *pParticles, int nStartBlock, int nBlockCount, bool bRandomlyInvert ) const
{
	size_t nAttrStride;
	fltx4 *pAttr = pParticles->GetM128AttributePtrForWrite( nAttributeId, &nAttrStride );
	pAttr += nAttrStride * nStartBlock;
	fltx4 val0 = ReplicateX4( fMin );
	fltx4 val_d = ReplicateX4( fMax - fMin );
	int nRandContext = GetSIMDRandContext();
	if ( !bRandomlyInvert )
	{
		if ( fExp != 1.0f )
		{
			int nExp = (int)(4.0f * fExp);
			while( nBlockCount-- )
			{
				*( pAttr ) = AddSIMD( *pAttr, AddSIMD( val0, MulSIMD( Pow_FixedPoint_Exponent_SIMD( RandSIMD( nRandContext ), nExp ), val_d ) ) );
				pAttr += nAttrStride;
			}
		}
		else
		{
			while( nBlockCount-- )
			{
				*pAttr = AddSIMD( *pAttr, AddSIMD( val0, MulSIMD( RandSIMD( nRandContext ), val_d ) ) );
				pAttr += nAttrStride;
			}
		}
	}
	else
	{
		fltx4 fl4NegOne = ReplicateX4( -1.0f );
		if ( fExp != 1.0f )
		{
			int nExp = (int)(4.0f * fExp);
			while( nBlockCount-- )
			{
				fltx4 fl4RandVal = AddSIMD( val0, MulSIMD( Pow_FixedPoint_Exponent_SIMD( RandSIMD( nRandContext ), nExp ), val_d ) );
				fltx4 fl4Sign = MaskedAssign( CmpGeSIMD( RandSIMD( nRandContext ), Four_PointFives ), Four_Ones, fl4NegOne ); 
				*pAttr = AddSIMD( *pAttr, MulSIMD( fl4RandVal, fl4Sign ) );
				pAttr += nAttrStride;
			}
		}
		else
		{
			while( nBlockCount-- )
			{
				fltx4 fl4RandVal = AddSIMD( val0, MulSIMD( RandSIMD( nRandContext ), val_d ) );
				fltx4 fl4Sign = MaskedAssign( CmpGeSIMD( RandSIMD( nRandContext ), Four_PointFives ), Four_Ones, fl4NegOne ); 
				*pAttr = AddSIMD( *pAttr, MulSIMD( fl4RandVal, fl4Sign ) );
				pAttr += nAttrStride;
			}
		}
	}
	ReleaseSIMDRandContext( nRandContext );
}


class C_INIT_CreateOnModel : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateOnModel );

	int m_nControlPointNumber;
	int m_nForceInModel;
	float m_flHitBoxScale;
	Vector m_vecDirectionBias;


	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | 
			PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK | PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK;

	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateOnModel, "Position on Model Random", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateOnModel ) 
	DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
	DMXELEMENT_UNPACK_FIELD( "force to be inside model", "0", int, m_nForceInModel )
	DMXELEMENT_UNPACK_FIELD( "hitbox scale", "1.0", int, m_flHitBoxScale )
	DMXELEMENT_UNPACK_FIELD( "direction bias", "0 0 0", Vector, m_vecDirectionBias )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateOnModel )

void C_INIT_CreateOnModel::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	pParticles->UpdateHitBoxInfo( m_nControlPointNumber );
	while( nParticleCount )
	{
		Vector vecPnts[100];								// minimize stack usage
		Vector vecUVW[100];
		int nHitBoxIndex[100];
		int nToDo = min( (int)ARRAYSIZE( vecPnts ), nParticleCount );

		Assert( m_nControlPointNumber <= pParticles->GetHighestControlPoint() );

		g_pParticleSystemMgr->Query()->GetRandomPointsOnControllingObjectHitBox( 
			pParticles, m_nControlPointNumber,
			nToDo, m_flHitBoxScale, m_nForceInModel, vecPnts, m_vecDirectionBias, vecUVW, 
			nHitBoxIndex );
		
		for( int i=0; i<nToDo; i++)
		{
			float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
			float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
			float *pHitboxRelXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ, start_p );
			int *pHitboxIndex = pParticles->GetIntAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_INDEX, start_p );
			start_p++;

			Vector randpos = vecPnts[i];
			xyz[0] = randpos.x;
			xyz[4] = randpos.y;
			xyz[8] = randpos.z;
			if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
			{
				pxyz[0] = randpos.x;
				pxyz[4] = randpos.y;
				pxyz[8] = randpos.z;
			}
			if ( pHitboxRelXYZ && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK ) )
			{
				pHitboxRelXYZ[0] = vecUVW[i].x;
				pHitboxRelXYZ[4] = vecUVW[i].y;
				pHitboxRelXYZ[8] = vecUVW[i].z;
			}
			if ( pHitboxIndex && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK ) )
			{
				*pHitboxIndex = nHitBoxIndex[i];
			}

		}
		nParticleCount -= nToDo;
	}
}
		

static inline void RandomPointOnUnitSphere( int nRandContext, FourVectors &out )
{
	// generate 4 random points on the unit sphere. uses Marsaglia (1972) method from
	// http://mathworld.wolfram.com/SpherePointPicking.html

	fltx4 f4x1 = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
	fltx4 f4x2 = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
	fltx4 f4x1SQ = MulSIMD( f4x1, f4x1 );
	fltx4 f4x2SQ = MulSIMD( f4x2, f4x2 );
	fltx4 badMask = CmpGeSIMD( AddSIMD( f4x1SQ, f4x2SQ ), Four_Ones );
	while( IsAnyNegative( badMask ) )
	{
		f4x1 = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), f4x1 );
		f4x2 = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), f4x2 );
		f4x1SQ = MulSIMD( f4x1, f4x1 );
		f4x2SQ = MulSIMD( f4x2, f4x2 );
		badMask = CmpGeSIMD( AddSIMD( f4x1SQ, f4x2SQ ), Four_Ones );
	}
	// now, we have 2 points on the unit circle
	fltx4 f4OuterArea = SqrtEstSIMD( SubSIMD( Four_Ones, SubSIMD( f4x1SQ, f4x2SQ ) ) );
	out.x = MulSIMD( AddSIMD( f4x1, f4x1 ), f4OuterArea );
	out.y = MulSIMD( AddSIMD( f4x2, f4x2 ), f4OuterArea );
	out.z = SubSIMD( Four_Ones, MulSIMD( Four_Twos, AddSIMD( f4x1, f4x2 ) ) );
}

static inline void RandomPointInUnitSphere( int nRandContext, FourVectors &out )
{
	// generate 4 random points inside the unit sphere. uses rejection method.
	out.x = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
	out.y = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
	out.z = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
	fltx4 f4xSQ = MulSIMD( out.x, out.x );
	fltx4 f4ySQ = MulSIMD( out.y, out.y );
	fltx4 f4zSQ = MulSIMD( out.z, out.z );
	fltx4 badMask = CmpGtSIMD( AddSIMD( AddSIMD( f4xSQ, f4ySQ ), f4zSQ ), Four_Ones );
	while( IsAnyNegative( badMask ) )
	{
		out.x = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), out.x );
		out.y = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), out.y );
		out.z = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), out.z );
		f4xSQ = MulSIMD( out.x, out.x );
		f4ySQ = MulSIMD( out.y, out.y );
		f4zSQ = MulSIMD( out.z, out.z );
		badMask = CmpGeSIMD( AddSIMD( AddSIMD( f4xSQ, f4ySQ ), f4zSQ ), Four_Ones );
	}
}



class C_INIT_CreateWithinSphere : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateWithinSphere );

	float m_fRadiusMin;
	float m_fRadiusMax;
	Vector m_vecDistanceBias, m_vecDistanceBiasAbs;
	int m_nControlPointNumber;
	float m_fSpeedMin;
	float m_fSpeedMax;
	float m_fSpeedRandExp;
	bool m_bLocalCoords;
	bool m_bDistanceBiasAbs;
	bool m_bUseHighestEndCP;
	bool m_bDistanceBias;
	float m_flEndCPGrowthTime;
	
	Vector m_LocalCoordinateSystemSpeedMin;
	Vector m_LocalCoordinateSystemSpeedMax;
	int m_nCreateInModel;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		if ( !m_bUseHighestEndCP )
			return 1ULL << m_nControlPointNumber;
		return ~( ( 1ULL << m_nControlPointNumber ) - 1 );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
										int start_block, int n_blocks, int nAttributeWriteMask,
										void *pContext ) const;

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
		m_bDistanceBias = ( m_vecDistanceBias.x != 1.0f ) || ( m_vecDistanceBias.y != 1.0f ) || ( m_vecDistanceBias.z != 1.0f );
		m_bDistanceBiasAbs = ( m_vecDistanceBiasAbs.x != 0.0f ) || ( m_vecDistanceBiasAbs.y != 0.0f ) || ( m_vecDistanceBiasAbs.z != 0.0f );
	}

	void Render( CParticleCollection *pParticles ) const;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateWithinSphere, "Position Within Sphere Random", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateWithinSphere ) 
	DMXELEMENT_UNPACK_FIELD( "distance_min", "0", float, m_fRadiusMin )
	DMXELEMENT_UNPACK_FIELD( "distance_max", "0", float, m_fRadiusMax )
	DMXELEMENT_UNPACK_FIELD( "distance_bias", "1 1 1", Vector, m_vecDistanceBias )
	DMXELEMENT_UNPACK_FIELD( "distance_bias_absolute_value", "0 0 0", Vector, m_vecDistanceBiasAbs )
	DMXELEMENT_UNPACK_FIELD( "bias in local system", "0", bool, m_bLocalCoords )
	DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
	DMXELEMENT_UNPACK_FIELD( "speed_min", "0", float, m_fSpeedMin )
	DMXELEMENT_UNPACK_FIELD( "speed_max", "0", float, m_fSpeedMax )
	DMXELEMENT_UNPACK_FIELD( "speed_random_exponent", "1", float, m_fSpeedRandExp )
	DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_min", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMin )
	DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_max", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMax )
	DMXELEMENT_UNPACK_FIELD( "create in model", "0", int, m_nCreateInModel )
	DMXELEMENT_UNPACK_FIELD( "randomly distribute to highest supplied Control Point", "0", bool, m_bUseHighestEndCP )
	DMXELEMENT_UNPACK_FIELD( "randomly distribution growth time", "0", float, m_flEndCPGrowthTime )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateWithinSphere )


ConVar r_sse_s( "r_sse_s", "1", 0, "sse ins for particle sphere create" );

void C_INIT_CreateWithinSphere::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
		int nCurrentControlPoint = m_nControlPointNumber;
		if ( m_bUseHighestEndCP )
		{
			//hack for growth time instead of using strength as currenly initializers don't support it.
			float flStrength = 1.0;
			if ( m_flEndCPGrowthTime != 0.0f )
			{
				flStrength = min ( pParticles->m_flCurTime, m_flEndCPGrowthTime ) / m_flEndCPGrowthTime ;
			}
			int nHighestControlPoint = floor ( pParticles->GetHighestControlPoint() * flStrength );
			nCurrentControlPoint = pParticles->RandomInt( m_nControlPointNumber, nHighestControlPoint );
		}
		Vector randpos, randDir;
		for( int nTryCtr = 0 ; nTryCtr < 10; nTryCtr++ )
		{
			float flLength = pParticles->RandomVectorInUnitSphere( &randpos );
			
			// Absolute value and biasing for creating hemispheres and ovoids.
			if ( m_bDistanceBiasAbs	)
			{
				if ( m_vecDistanceBiasAbs.x	!= 0.0f )
				{
					randpos.x = fabs(randpos.x);
				}
				if ( m_vecDistanceBiasAbs.y	!= 0.0f )
				{
					randpos.y = fabs(randpos.y);
				}
				if ( m_vecDistanceBiasAbs.z	!= 0.0f )
				{
					randpos.z = fabs(randpos.z);
				}
			}
			randpos *= m_vecDistanceBias;
			randpos.NormalizeInPlace();
			
			
			randDir = randpos;
			randpos *= Lerp( flLength, m_fRadiusMin, m_fRadiusMax );
			
			if ( !m_bDistanceBias || !m_bLocalCoords )
			{
				Vector vecControlPoint;
				pParticles->GetControlPointAtTime( nCurrentControlPoint, *ct, &vecControlPoint );
				randpos += vecControlPoint;
			}
			else
			{
				matrix3x4_t mat;
				pParticles->GetControlPointTransformAtTime( nCurrentControlPoint, *ct, &mat );
				Vector vecTransformLocal = vec3_origin;
				VectorTransform( randpos, mat, vecTransformLocal );
				randpos = vecTransformLocal;
			}
			
			// now, force to be in model if we can
			if (
				( m_nCreateInModel == 0 ) || 
				(g_pParticleSystemMgr->Query()->MovePointInsideControllingObject( 
					pParticles, pParticles->m_ControlPoints[nCurrentControlPoint].m_pObject, &randpos ) ) )
				break;
		}
		
		xyz[0] = randpos.x;
		xyz[4] = randpos.y;
		xyz[8] = randpos.z;

		// FIXME: Remove this into a speed setting initializer
		if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
		{
			Vector poffset(0,0,0);
			if ( m_fSpeedMax > 0.0 )
			{
				float rand_speed = pParticles->RandomFloatExp( m_fSpeedMin, m_fSpeedMax, m_fSpeedRandExp );
				poffset.x -= rand_speed * randDir.x;
				poffset.y -= rand_speed * randDir.y;
				poffset.z -= rand_speed * randDir.z;
			}
			poffset -=
				pParticles->RandomFloat( m_LocalCoordinateSystemSpeedMin.x, m_LocalCoordinateSystemSpeedMax.x )*
				pParticles->m_ControlPoints[ nCurrentControlPoint ].m_ForwardVector;
			poffset -=
				pParticles->RandomFloat( m_LocalCoordinateSystemSpeedMin.y, m_LocalCoordinateSystemSpeedMax.y )*
				pParticles->m_ControlPoints[ nCurrentControlPoint ].m_RightVector;
			poffset -=
				pParticles->RandomFloat( m_LocalCoordinateSystemSpeedMin.z, m_LocalCoordinateSystemSpeedMax.z )*
				pParticles->m_ControlPoints[ nCurrentControlPoint ].m_UpVector;

			poffset *= pParticles->m_flPreviousDt;
			randpos += poffset;
			pxyz[0] = randpos.x;
			pxyz[4] = randpos.y;
			pxyz[8] = randpos.z;
		}
	}
}

void C_INIT_CreateWithinSphere::InitNewParticlesBlock( CParticleCollection *pParticles, 
													   int start_block, int n_blocks, int nAttributeWriteMask,
													   void *pContext ) const
{
	// sse-favorable settings
	bool bMustUseScalar = m_bUseHighestEndCP || m_nCreateInModel;
	if ( m_bDistanceBias && m_bLocalCoords )
		bMustUseScalar = true;

	if ( ( !bMustUseScalar ) && 
		 // (( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) == 0 ) &&
		 r_sse_s.GetInt() )
	{
		C4VAttributeWriteIterator pXYZ( PARTICLE_ATTRIBUTE_XYZ, pParticles );
		pXYZ += start_block;
		C4VAttributeWriteIterator pPrevXYZ( PARTICLE_ATTRIBUTE_PREV_XYZ, pParticles );
		pPrevXYZ += start_block;
		CM128AttributeIterator pCT( PARTICLE_ATTRIBUTE_CREATION_TIME, pParticles );
		pCT += start_block;
		
		// now, calculate the terms we need for interpolating control points
		FourVectors v4PrevControlPointPosition;
		v4PrevControlPointPosition.DuplicateVector( pParticles->m_ControlPoints[m_nControlPointNumber].m_PrevPosition );
		FourVectors v4ControlPointDelta;
		v4ControlPointDelta.DuplicateVector( pParticles->m_ControlPoints[m_nControlPointNumber].m_Position );
		v4ControlPointDelta -= v4PrevControlPointPosition;

		float flOODT = ( pParticles->m_flDt > 0.0 ) ? ( 1.0 / pParticles->m_flDt ) : 0.0;
		fltx4 fl4OODt = ReplicateX4( flOODT );
		fltx4 fl4PrevTime = ReplicateX4( pParticles->m_flCurTime - pParticles->m_flDt );
		int nContext = GetSIMDRandContext();

		FourVectors v4DistanceBias;
		v4DistanceBias.DuplicateVector( m_vecDistanceBias );
		FourVectors v4ConditionalAbsMask;
		for( int nComp = 0 ; nComp < 3; nComp++ )
		{
			v4ConditionalAbsMask[nComp] = ( m_vecDistanceBiasAbs[nComp] > 0 ) ?
				LoadAlignedSIMD( ( const float *) g_SIMD_clear_signmask ) :
				LoadAlignedSIMD( ( const float *) g_SIMD_AllOnesMask );
		}
		fltx4 fl4RadiusMin = ReplicateX4( m_fRadiusMin );
		fltx4 fl4RadiusSpread = ReplicateX4( m_fRadiusMax - m_fRadiusMin );
		int nPowSSEMask = 4.0 * m_fSpeedRandExp;

		bool bDoRandSpeed =
			( m_fSpeedMax > 0. ) || 
			( m_LocalCoordinateSystemSpeedMax.x != 0 ) ||
			( m_LocalCoordinateSystemSpeedMax.y != 0 ) ||
			( m_LocalCoordinateSystemSpeedMax.z != 0 ) ||
			( m_LocalCoordinateSystemSpeedMin.x != 0 ) ||
			( m_LocalCoordinateSystemSpeedMin.y != 0 ) ||
			( m_LocalCoordinateSystemSpeedMin.z != 0 );


		fltx4 fl4SpeedMin = ReplicateX4( m_fSpeedMin );
		fltx4 fl4SpeedRange = ReplicateX4( m_fSpeedMax - m_fSpeedMin );

		fltx4 fl4LocalSpeedMinX = ReplicateX4( m_LocalCoordinateSystemSpeedMin.x );
		fltx4 fl4LocalSpeedXSpread = ReplicateX4( m_LocalCoordinateSystemSpeedMax.x - 
												  m_LocalCoordinateSystemSpeedMin.x );
		fltx4 fl4LocalSpeedMinY = ReplicateX4( m_LocalCoordinateSystemSpeedMin.y );
		fltx4 fl4LocalSpeedYSpread = ReplicateX4( m_LocalCoordinateSystemSpeedMax.y - 
												  m_LocalCoordinateSystemSpeedMin.y );
		fltx4 fl4LocalSpeedMinZ = ReplicateX4( m_LocalCoordinateSystemSpeedMin.z );
		fltx4 fl4LocalSpeedZSpread = ReplicateX4( m_LocalCoordinateSystemSpeedMax.z - 
												  m_LocalCoordinateSystemSpeedMin.z );

		FourVectors v4CPForward;
		v4CPForward.DuplicateVector( pParticles->m_ControlPoints[m_nControlPointNumber].m_ForwardVector );
		FourVectors v4CPUp;
		v4CPUp.DuplicateVector( pParticles->m_ControlPoints[m_nControlPointNumber].m_UpVector );
		FourVectors v4CPRight;
		v4CPRight.DuplicateVector( pParticles->m_ControlPoints[m_nControlPointNumber].m_RightVector );

		fltx4 fl4PreviousDt = ReplicateX4( pParticles->m_flPreviousDt );

		while( n_blocks-- )
		{
			FourVectors v4RandPos;
			RandomPointInUnitSphere( nContext, v4RandPos );

			fltx4 fl4Length = v4RandPos.length();

			// conditional absolute value
			v4RandPos.x = AndSIMD( v4RandPos.x, v4ConditionalAbsMask.x );
			v4RandPos.y = AndSIMD( v4RandPos.y, v4ConditionalAbsMask.y );
			v4RandPos.z = AndSIMD( v4RandPos.z, v4ConditionalAbsMask.z );

			v4RandPos *= v4DistanceBias;
			v4RandPos.VectorNormalizeFast();
			
			FourVectors v4randDir = v4RandPos;
			
			// lerp radius
			v4RandPos *= AddSIMD( fl4RadiusMin, MulSIMD( fl4Length, fl4RadiusSpread ) );
			v4RandPos += v4PrevControlPointPosition;

			FourVectors cpnt = v4ControlPointDelta;
			cpnt *= MulSIMD( SubSIMD( *pCT, fl4PrevTime ), fl4OODt );
			v4RandPos += cpnt;

			*(pXYZ) = v4RandPos;

			if ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK )
			{
				if ( bDoRandSpeed )
				{
					fltx4 fl4Rand_speed = Pow_FixedPoint_Exponent_SIMD( RandSIMD( nContext ), nPowSSEMask );
					fl4Rand_speed = AddSIMD( fl4SpeedMin, MulSIMD( fl4SpeedRange, fl4Rand_speed ) );
					v4randDir *= fl4Rand_speed;

					// local speed
					FourVectors v4LocalOffset = v4CPForward;
					v4LocalOffset *= AddSIMD( fl4LocalSpeedMinX, 
											  MulSIMD( fl4LocalSpeedXSpread, RandSIMD( nContext ) ) );
					v4randDir += v4LocalOffset;

					v4LocalOffset = v4CPRight;
					v4LocalOffset *= AddSIMD( fl4LocalSpeedMinY, 
											  MulSIMD( fl4LocalSpeedYSpread, RandSIMD( nContext ) ) );
					v4randDir += v4LocalOffset;


					v4LocalOffset = v4CPUp;
					v4LocalOffset *= AddSIMD( fl4LocalSpeedMinZ, 
											  MulSIMD( fl4LocalSpeedZSpread, RandSIMD( nContext ) ) );
					v4randDir += v4LocalOffset;
					v4randDir *= fl4PreviousDt;
					v4RandPos -= v4randDir;
				}
				*(pPrevXYZ) = v4RandPos;

			}



			++pXYZ;
			++pPrevXYZ;
			++pCT;
		}
		ReleaseSIMDRandContext( nContext );

	}
	else
		CParticleOperatorInstance::InitNewParticlesBlock( pParticles, start_block, n_blocks, nAttributeWriteMask, pContext );

}


//-----------------------------------------------------------------------------
// Render visualization
//-----------------------------------------------------------------------------
void C_INIT_CreateWithinSphere::Render( CParticleCollection *pParticles ) const
{					   
	Vector vecOrigin;
	pParticles->GetControlPointAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &vecOrigin );
	RenderWireframeSphere( vecOrigin, m_fRadiusMin, 16, 8, Color( 192, 192, 0, 255 ), false );
	RenderWireframeSphere( vecOrigin, m_fRadiusMax, 16, 8, Color( 128, 128, 0, 255 ), false );
}




class C_INIT_CreateWithinBox : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateWithinBox );

	Vector m_vecMin;
	Vector m_vecMax;
	int m_nControlPointNumber;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

	void Render( CParticleCollection *pParticles ) const;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateWithinBox, "Position Within Box Random", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateWithinBox ) 
	DMXELEMENT_UNPACK_FIELD( "min", "0 0 0", Vector, m_vecMin )
	DMXELEMENT_UNPACK_FIELD( "max", "0 0 0", Vector, m_vecMax )
	DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateWithinBox )


void C_INIT_CreateWithinBox::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	int nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );

		Vector randpos;
		pParticles->RandomVector( m_vecMin, m_vecMax, &randpos );

		Vector vecControlPoint;
		pParticles->GetControlPointAtTime( nControlPointNumber, *ct, &vecControlPoint );
		randpos += vecControlPoint;

		xyz[0] = randpos.x;
		xyz[4] = randpos.y;
		xyz[8] = randpos.z;
		if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
		{
			pxyz[0] = randpos.x;
			pxyz[4] = randpos.y;
			pxyz[8] = randpos.z;
		}
	}
}

//-----------------------------------------------------------------------------
// Render visualization
//-----------------------------------------------------------------------------
void C_INIT_CreateWithinBox::Render( CParticleCollection *pParticles ) const
{					   
	Vector vecOrigin;
	pParticles->GetControlPointAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &vecOrigin );
	RenderWireframeBox( vecOrigin, vec3_angle, m_vecMin, m_vecMax, Color( 192, 192, 0, 255 ), false );
}



//-----------------------------------------------------------------------------
// Position Offset Initializer
// offsets initial position of particles within a random vector range,
// while still respecting spherical/conical spacial and velocity initialization
//-----------------------------------------------------------------------------
class C_INIT_PositionOffset : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_PositionOffset );

	Vector m_OffsetMin;
	Vector m_OffsetMax;
	int m_nControlPointNumber;
	bool m_bLocalCoords;
	bool m_bProportional;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_RADIUS_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
	}

	bool InitMultipleOverride ( void ) { return true; }

	void Render( CParticleCollection *pParticles ) const;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_PositionOffset, "Position Modify Offset Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_PositionOffset ) 
	DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
	DMXELEMENT_UNPACK_FIELD( "offset min", "0 0 0", Vector, m_OffsetMin )
	DMXELEMENT_UNPACK_FIELD( "offset max", "0 0 0", Vector, m_OffsetMax )
	DMXELEMENT_UNPACK_FIELD( "offset in local space 0/1", "0", bool, m_bLocalCoords )
	DMXELEMENT_UNPACK_FIELD( "offset proportional to radius 0/1", "0", bool, m_bProportional )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_PositionOffset )


void C_INIT_PositionOffset::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		const float *radius = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_RADIUS, start_p );
		
		Vector randpos;
		
		if ( m_bProportional )
		{
			pParticles->RandomVector( (m_OffsetMin * *radius), (m_OffsetMax * *radius), &randpos );
		}
		else
		{
			pParticles->RandomVector( m_OffsetMin, m_OffsetMax, &randpos );
		}

		if ( m_bLocalCoords )
		{
			matrix3x4_t mat;
			pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *ct, &mat );
			Vector vecTransformLocal = vec3_origin;
			VectorRotate( randpos, mat, vecTransformLocal );
			randpos = vecTransformLocal;
		}

		xyz[0] += randpos.x;
		xyz[4] += randpos.y;
		xyz[8] += randpos.z;
		pxyz[0] += randpos.x;
		pxyz[4] += randpos.y;
		pxyz[8] += randpos.z;
	}
}


//-----------------------------------------------------------------------------
// Render visualization
//-----------------------------------------------------------------------------
void C_INIT_PositionOffset::Render( CParticleCollection *pParticles ) const
{					   
	Vector vecOrigin (0,0,0);
	Vector vecMinExtent = m_OffsetMin;
	Vector vecMaxExtent = m_OffsetMax;
	if ( m_bLocalCoords )
	{
		matrix3x4_t mat;
		pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &mat );
		VectorRotate( m_OffsetMin, mat, vecMinExtent );
		VectorRotate( m_OffsetMax, mat, vecMaxExtent ); 
	}
	else
	{
		pParticles->GetControlPointAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &vecOrigin );
	}
	RenderWireframeBox( vecOrigin, vec3_angle, vecMinExtent , vecMaxExtent , Color( 192, 192, 0, 255 ), false );
}


//-----------------------------------------------------------------------------
//
// Velocity-based Operators
//
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Random velocity initializer
//-----------------------------------------------------------------------------
class C_INIT_VelocityRandom : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_VelocityRandom );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		if ( m_bHasLocalSpeed )
			return 1ULL << m_nControlPointNumber;
		return 0;
	}

	virtual bool InitMultipleOverride() { return true; }

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
		m_bHasLocalSpeed = ( m_LocalCoordinateSystemSpeedMin != vec3_origin ) || ( m_LocalCoordinateSystemSpeedMax != vec3_origin );  
		if ( m_fSpeedMax < m_fSpeedMin )
		{
			V_swap( m_fSpeedMin, m_fSpeedMax );
		}
	}

private:
	int m_nControlPointNumber;
	float m_fSpeedMin;
	float m_fSpeedMax;
	Vector m_LocalCoordinateSystemSpeedMin;
	Vector m_LocalCoordinateSystemSpeedMax;
	bool m_bHasLocalSpeed;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_VelocityRandom, "Velocity Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_VelocityRandom ) 
	DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
	DMXELEMENT_UNPACK_FIELD( "random_speed_min", "0", float, m_fSpeedMin )
	DMXELEMENT_UNPACK_FIELD( "random_speed_max", "0", float, m_fSpeedMax )
	DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_min", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMin )
	DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_max", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMax )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_VelocityRandom )


void C_INIT_VelocityRandom::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	for( ; nParticleCount--; start_p++ )
	{
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
			
		Vector vecVelocity( 0.0f, 0.0f, 0.0f );
		if ( m_bHasLocalSpeed )
		{
			Vector vecRandomSpeed, vecForward, vecUp, vecRight;
			pParticles->RandomVector( m_LocalCoordinateSystemSpeedMin, m_LocalCoordinateSystemSpeedMax, &vecRandomSpeed );
			pParticles->GetControlPointOrientationAtTime( m_nControlPointNumber, *ct, &vecForward, &vecRight, &vecUp );
			VectorMA( vecVelocity, vecRandomSpeed.x, vecForward, vecVelocity );
			VectorMA( vecVelocity, -vecRandomSpeed.y, vecRight, vecVelocity );
			VectorMA( vecVelocity, vecRandomSpeed.z, vecUp, vecVelocity );
		}

		if ( m_fSpeedMax > 0.0f )
		{
			Vector vecRandomSpeed;
			pParticles->RandomVector( m_fSpeedMin, m_fSpeedMax, &vecRandomSpeed );
			vecVelocity += vecRandomSpeed;
		}

		vecVelocity *= pParticles->m_flPreviousDt;
		pxyz[0] -= vecVelocity.x;
		pxyz[4] -= vecVelocity.y;
		pxyz[8] -= vecVelocity.z;
	}
}


//-----------------------------------------------------------------------------
// Initial Velocity Noise Operator
//-----------------------------------------------------------------------------
class C_INIT_InitialVelocityNoise : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_InitialVelocityNoise );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK;
	}
	
	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;	

	void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const;

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
	}

	virtual bool InitMultipleOverride() { return true; }

	Vector	m_vecAbsVal, m_vecAbsValInv, m_vecOffsetLoc;
	float	m_flOffset;
	Vector	m_vecOutputMin;
	Vector	m_vecOutputMax;
	float	m_flNoiseScale, m_flNoiseScaleLoc;
	int		nRemainingBlocks, m_nControlPointNumber;
	bool	m_bLocalSpace;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_InitialVelocityNoise, "Velocity Noise", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_InitialVelocityNoise )
	DMXELEMENT_UNPACK_FIELD( "Control Point Number","0",int,m_nControlPointNumber)
	DMXELEMENT_UNPACK_FIELD( "Time Noise Coordinate Scale","1",float,m_flNoiseScale)
	DMXELEMENT_UNPACK_FIELD( "Spatial Noise Coordinate Scale","0.01",float,m_flNoiseScaleLoc)
	DMXELEMENT_UNPACK_FIELD( "Time Coordinate Offset","0", float, m_flOffset )
	DMXELEMENT_UNPACK_FIELD( "Spatial Coordinate Offset","0 0 0", Vector, m_vecOffsetLoc )
	DMXELEMENT_UNPACK_FIELD( "Absolute Value","0 0 0", Vector, m_vecAbsVal )
	DMXELEMENT_UNPACK_FIELD( "Invert Abs Value","0 0 0", Vector, m_vecAbsValInv )
	DMXELEMENT_UNPACK_FIELD( "output minimum","0 0 0", Vector, m_vecOutputMin )
	DMXELEMENT_UNPACK_FIELD( "output maximum","1 1 1", Vector, m_vecOutputMax )
	DMXELEMENT_UNPACK_FIELD( "Apply Velocity in Local Space (0/1)","0", bool, m_bLocalSpace )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_InitialVelocityNoise );


void C_INIT_InitialVelocityNoise::InitNewParticlesBlock( CParticleCollection *pParticles, 
								   int start_block, int n_blocks, int nAttributeWriteMask,
								   void *pContext ) const
{
	float		flAbsScaleX, flAbsScaleY, flAbsScaleZ;
	fltx4 		fl4AbsValX, fl4AbsValY, fl4AbsValZ;
	fl4AbsValX = CmpEqSIMD( Four_Zeros, Four_Zeros ); 
	fl4AbsValY = fl4AbsValX;
	fl4AbsValZ = fl4AbsValX;
	flAbsScaleX = 0.5;
	flAbsScaleY = 0.5; 
	flAbsScaleZ = 0.5;

	// Set up single if check for absolute value inversion inside the loop
	bool m_bNoiseAbs = ( m_vecAbsValInv.x != 0.0f ) || ( m_vecAbsValInv.y != 0.0f ) || ( m_vecAbsValInv.z != 0.0f );
	// Set up values for more optimal absolute value calculations inside the loop
	if ( m_vecAbsVal.x	!= 0.0f )
	{
		fl4AbsValX = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
		flAbsScaleX = 1.0;
	}
	if ( m_vecAbsVal.y	!= 0.0f )
	{
		fl4AbsValY = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
		flAbsScaleY = 1.0;
	}
	if ( m_vecAbsVal.z	!= 0.0f )
	{
		fl4AbsValZ = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
		flAbsScaleZ = 1.0;
	}

	float ValueScaleX, ValueScaleY, ValueScaleZ, ValueBaseX, ValueBaseY, ValueBaseZ;

	ValueScaleX = ( flAbsScaleX *(m_vecOutputMax.x-m_vecOutputMin.x ) );
	ValueBaseX = (m_vecOutputMin.x+ ( ( 1.0 - flAbsScaleX ) *( m_vecOutputMax.x-m_vecOutputMin.x ) ) );

	ValueScaleY = ( flAbsScaleY *(m_vecOutputMax.y-m_vecOutputMin.y ) );
	ValueBaseY = (m_vecOutputMin.y+ ( ( 1.0 - flAbsScaleY ) *( m_vecOutputMax.y-m_vecOutputMin.y ) ) );

	ValueScaleZ = ( flAbsScaleZ *(m_vecOutputMax.z-m_vecOutputMin.z ) );
	ValueBaseZ = (m_vecOutputMin.z+ ( ( 1.0 - flAbsScaleZ ) *( m_vecOutputMax.z-m_vecOutputMin.z ) ) );

	fltx4 fl4ValueBaseX = ReplicateX4( ValueBaseX );
	fltx4 fl4ValueBaseY = ReplicateX4( ValueBaseY );
	fltx4 fl4ValueBaseZ = ReplicateX4( ValueBaseZ );

	fltx4 fl4ValueScaleX = ReplicateX4( ValueScaleX );
	fltx4 fl4ValueScaleY = ReplicateX4( ValueScaleY );
	fltx4 fl4ValueScaleZ = ReplicateX4( ValueScaleZ );

	float CoordScale = m_flNoiseScale;
	float CoordScaleLoc = m_flNoiseScaleLoc;

	Vector ofs_y = Vector( 100000.5, 300000.25, 9000000.75 );
	Vector ofs_z = Vector( 110000.25, 310000.75, 9100000.5 );

	size_t attr_stride;

	const FourVectors *xyz = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
	xyz += attr_stride * start_block;
	FourVectors *pxyz = pParticles->Get4VAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, &attr_stride );
	pxyz += attr_stride * start_block;
	const fltx4 *pCreationTime = pParticles->GetM128AttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, &attr_stride );
	pCreationTime += attr_stride * start_block;

	// setup
	fltx4 fl4Offset = ReplicateX4( m_flOffset );
	FourVectors fvOffsetLoc;
	fvOffsetLoc.DuplicateVector( m_vecOffsetLoc );
	CParticleSIMDTransformation CPTransform;
	float flCreationTime = SubFloat( *pCreationTime, 0 );
	pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, flCreationTime, &CPTransform );

	while( n_blocks-- )
	{	
		FourVectors fvCoordLoc = *xyz;
		fvCoordLoc += fvOffsetLoc;

		FourVectors fvCoord;
		fvCoord.x = AddSIMD(*pCreationTime, fl4Offset);
		fvCoord.y = AddSIMD(*pCreationTime, fl4Offset);
		fvCoord.z = AddSIMD(*pCreationTime, fl4Offset);
		fvCoordLoc *= CoordScaleLoc;
		fvCoord *= CoordScale;
		fvCoord += fvCoordLoc;

		FourVectors fvCoord2 = fvCoord;
		FourVectors fvOffsetTemp;
		fvOffsetTemp.DuplicateVector( ofs_y );
		fvCoord2 +=  fvOffsetTemp;
		FourVectors fvCoord3 = fvCoord;
		fvOffsetTemp.DuplicateVector( ofs_z );
		fvCoord3 += fvOffsetTemp;

		fltx4 fl4NoiseX;
		fltx4 fl4NoiseY;
		fltx4 fl4NoiseZ;

		fl4NoiseX = NoiseSIMD( fvCoord );

		fl4NoiseY = NoiseSIMD( fvCoord2 );

		fl4NoiseZ = NoiseSIMD( fvCoord3 );

		fl4NoiseX = AndSIMD ( fl4NoiseX, fl4AbsValX );
		fl4NoiseY = AndSIMD ( fl4NoiseY, fl4AbsValY );
		fl4NoiseZ = AndSIMD ( fl4NoiseZ, fl4AbsValZ );

		if ( m_bNoiseAbs )
		{
			if ( m_vecAbsValInv.x	!= 0.0f )
			{
				fl4NoiseX = SubSIMD( Four_Ones, fl4NoiseX );
			}

			if ( m_vecAbsValInv.y	!= 0.0f )
			{											   
				fl4NoiseY = SubSIMD( Four_Ones, fl4NoiseY );
			}
			if ( m_vecAbsValInv.z	!= 0.0f )
			{
				fl4NoiseZ = SubSIMD( Four_Ones, fl4NoiseZ );
			}
		}

		FourVectors fvOffset;

		fvOffset.x = AddSIMD( fl4ValueBaseX, ( MulSIMD( fl4ValueScaleX , fl4NoiseX ) ) );
		fvOffset.y = AddSIMD( fl4ValueBaseY, ( MulSIMD( fl4ValueScaleY , fl4NoiseY ) ) );
		fvOffset.z = AddSIMD( fl4ValueBaseZ, ( MulSIMD( fl4ValueScaleZ , fl4NoiseZ ) ) );

		fvOffset *= pParticles->m_flPreviousDt;  

		if ( m_bLocalSpace )
		{
			CPTransform.VectorRotate( fvOffset );
		}

		*pxyz -= fvOffset;

		xyz += attr_stride;
		pxyz += attr_stride;
		pCreationTime += attr_stride;

	}
}


void C_INIT_InitialVelocityNoise::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	float	flAbsScaleX, flAbsScaleY, flAbsScaleZ;
	int		nAbsValX, nAbsValY, nAbsValZ;
	nAbsValX = 0xffffffff; 
	nAbsValY = 0xffffffff;
	nAbsValZ = 0xffffffff;
	flAbsScaleX = 0.5;
	flAbsScaleY = 0.5; 
	flAbsScaleZ = 0.5;
	// Set up single if check for absolute value inversion inside the loop
	bool m_bNoiseAbs = ( m_vecAbsValInv.x != 0.0f ) || ( m_vecAbsValInv.y != 0.0f ) || ( m_vecAbsValInv.z != 0.0f );
	// Set up values for more optimal absolute value calculations inside the loop
	if ( m_vecAbsVal.x	!= 0.0f )
	{
		nAbsValX = 0x7fffffff;
		flAbsScaleX = 1.0;
	}
	if ( m_vecAbsVal.y	!= 0.0f )
	{
		nAbsValY = 0x7fffffff;
		flAbsScaleY = 1.0;
	}
	if ( m_vecAbsVal.z	!= 0.0f )
	{
		nAbsValZ = 0x7fffffff;
		flAbsScaleZ = 1.0;
	}

	float ValueScaleX, ValueScaleY, ValueScaleZ, ValueBaseX, ValueBaseY, ValueBaseZ;

	ValueScaleX = ( flAbsScaleX *(m_vecOutputMax.x-m_vecOutputMin.x ) );
	ValueBaseX = (m_vecOutputMin.x+ ( ( 1.0 - flAbsScaleX ) *( m_vecOutputMax.x-m_vecOutputMin.x ) ) );

	ValueScaleY = ( flAbsScaleY *(m_vecOutputMax.y-m_vecOutputMin.y ) );
	ValueBaseY = (m_vecOutputMin.y+ ( ( 1.0 - flAbsScaleY ) *( m_vecOutputMax.y-m_vecOutputMin.y ) ) );

	ValueScaleZ = ( flAbsScaleZ *(m_vecOutputMax.z-m_vecOutputMin.z ) );
	ValueBaseZ = (m_vecOutputMin.z+ ( ( 1.0 - flAbsScaleZ ) *( m_vecOutputMax.z-m_vecOutputMin.z ) ) );


	float CoordScale = m_flNoiseScale;
	float CoordScaleLoc = m_flNoiseScaleLoc;

	Vector ofs_y = Vector( 100000.5, 300000.25, 9000000.75 );
	Vector ofs_z = Vector( 110000.25, 310000.75, 9100000.5 );

	for( ; nParticleCount--; start_p++ )
	{	
		const float *xyz = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );		
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
		const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
	
		Vector Coord, Coord2, Coord3, CoordLoc;
		SetVectorFromAttribute( CoordLoc, xyz );
		CoordLoc += m_vecOffsetLoc;

		float Offset = m_flOffset;
		Coord = Vector ( (*pCreationTime + Offset), (*pCreationTime + Offset), (*pCreationTime + Offset) );

		Coord *= CoordScale;
		CoordLoc *= CoordScaleLoc;
		Coord += CoordLoc;

		Coord2 = ( Coord );
		Coord3 = ( Coord );

		fltx4 flNoise128;
		FourVectors fvNoise;

		fvNoise.DuplicateVector( Coord );
		flNoise128 = NoiseSIMD( fvNoise );
		float flNoiseX = SubFloat( flNoise128, 0 );

		fvNoise.DuplicateVector( Coord2 + ofs_y );
		flNoise128 = NoiseSIMD( fvNoise );
		float flNoiseY = SubFloat( flNoise128, 0 );

		fvNoise.DuplicateVector( Coord3 + ofs_z );
		flNoise128 = NoiseSIMD( fvNoise );
		float flNoiseZ = SubFloat( flNoise128, 0 );

		*( (int *) &flNoiseX)  &= nAbsValX;
		*( (int *) &flNoiseY)  &= nAbsValY;
		*( (int *) &flNoiseZ)  &= nAbsValZ;

		if ( m_bNoiseAbs )
		{
			if ( m_vecAbsValInv.x	!= 0.0f )
			{
				flNoiseX = 1.0 - flNoiseX;
			}

			if ( m_vecAbsValInv.y	!= 0.0f )
			{											   
				flNoiseY = 1.0 - flNoiseY;
			}
			if ( m_vecAbsValInv.z	!= 0.0f )
			{
				flNoiseZ = 1.0 - flNoiseZ;
			}
		}

		Vector poffset;
		poffset.x = ( ValueBaseX + ( ValueScaleX * flNoiseX ) );
		poffset.y = ( ValueBaseY + ( ValueScaleY * flNoiseY ) );
		poffset.z = ( ValueBaseZ + ( ValueScaleZ * flNoiseZ ) );

		poffset *= pParticles->m_flPreviousDt;  

		if ( m_bLocalSpace )
		{
			matrix3x4_t mat;
			pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *pCreationTime, &mat );
			Vector vecTransformLocal = vec3_origin;
			VectorRotate( poffset, mat, vecTransformLocal );
			poffset = vecTransformLocal;
		}
		pxyz[0] -= poffset.x;
		pxyz[4] -= poffset.y;
		pxyz[8] -= poffset.z;
	}
}




class C_INIT_RandomLifeTime : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomLifeTime );

	float m_fLifetimeMin;
	float m_fLifetimeMax;
	float m_fLifetimeRandExponent;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask, void *pContext ) const;

	void InitNewParticlesBlock( CParticleCollection *pParticles, 
										int start_block, int n_blocks, int nAttributeWriteMask,
										void *pContext ) const
	{
		if ( m_fLifetimeRandExponent != 1.0f )
		{
			InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_LIFE_DURATION,
													m_fLifetimeMin, m_fLifetimeMax, m_fLifetimeRandExponent,
													pParticles, start_block, n_blocks );
		}
		else
		{
			InitScalarAttributeRandomRangeBlock( PARTICLE_ATTRIBUTE_LIFE_DURATION,
													m_fLifetimeMin, m_fLifetimeMax, pParticles, start_block, n_blocks );
		}

	}

};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomLifeTime, "Lifetime Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomLifeTime ) 
	DMXELEMENT_UNPACK_FIELD( "lifetime_min", "0", float, m_fLifetimeMin )
	DMXELEMENT_UNPACK_FIELD( "lifetime_max", "0", float, m_fLifetimeMax )
	DMXELEMENT_UNPACK_FIELD( "lifetime_random_exponent", "1", float, m_fLifetimeRandExponent )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomLifeTime )

void C_INIT_RandomLifeTime::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	for( ; nParticleCount--; start_p++ )
	{
		float *dtime = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
		*dtime = pParticles->RandomFloatExp( m_fLifetimeMin, m_fLifetimeMax, m_fLifetimeRandExponent );
	}
}


//-----------------------------------------------------------------------------
// Random radius
//-----------------------------------------------------------------------------
class C_INIT_RandomRadius : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomRadius );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_RADIUS_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask, void *pContext ) const;

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
										int start_block, int n_blocks, int nAttributeWriteMask,
										void *pContext ) const
	{
		if ( m_flRadiusRandExponent != 1.0f )
		{
			InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_RADIUS,
				m_flRadiusMin, m_flRadiusMax, m_flRadiusRandExponent,
				pParticles, start_block, n_blocks );
		}
		else
		{
			InitScalarAttributeRandomRangeBlock( PARTICLE_ATTRIBUTE_RADIUS,
				m_flRadiusMin, m_flRadiusMax, 
				pParticles, start_block, n_blocks );
		}

	}

	float m_flRadiusMin;
	float m_flRadiusMax;
	float m_flRadiusRandExponent;
};


DEFINE_PARTICLE_OPERATOR( C_INIT_RandomRadius, "Radius Random", OPERATOR_PI_RADIUS );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRadius ) 
	DMXELEMENT_UNPACK_FIELD( "radius_min", "1", float, m_flRadiusMin )
	DMXELEMENT_UNPACK_FIELD( "radius_max", "1", float, m_flRadiusMax )
	DMXELEMENT_UNPACK_FIELD( "radius_random_exponent", "1", float, m_flRadiusRandExponent )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRadius )

void C_INIT_RandomRadius::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask,
	void *pContext) const
{
	for( ; nParticleCount--; start_p++ )
	{
		float *r = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_RADIUS, start_p );
		*r = pParticles->RandomFloatExp( m_flRadiusMin, m_flRadiusMax, m_flRadiusRandExponent );
	}
}


//-----------------------------------------------------------------------------
// Random alpha
//-----------------------------------------------------------------------------
class C_INIT_RandomAlpha : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomAlpha );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_ALPHA_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_flAlphaMin = m_nAlphaMin / 255.0f;
		m_flAlphaMax = m_nAlphaMax / 255.0f;
	}

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
	{
		if ( m_flAlphaRandExponent != 1.0f )
		{
			InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_ALPHA,
				m_flAlphaMin, m_flAlphaMax, m_flAlphaRandExponent,
				pParticles, start_block, n_blocks );
		}
		else
		{
			InitScalarAttributeRandomRangeBlock( PARTICLE_ATTRIBUTE_ALPHA,
				m_flAlphaMin, m_flAlphaMax,
				pParticles, start_block, n_blocks );
		}
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
	{
		for( ; nParticleCount--; start_p++ )
		{
			float *pAlpha = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_ALPHA, start_p );
			*pAlpha = pParticles->RandomFloatExp( m_flAlphaMin, m_flAlphaMax, m_flAlphaRandExponent );
		}
	}

	int m_nAlphaMin;
	int m_nAlphaMax;
	float m_flAlphaMin;
	float m_flAlphaMax;
	float m_flAlphaRandExponent;
};


DEFINE_PARTICLE_OPERATOR( C_INIT_RandomAlpha, "Alpha Random", OPERATOR_PI_ALPHA );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomAlpha ) 
	DMXELEMENT_UNPACK_FIELD( "alpha_min", "255", int, m_nAlphaMin )
	DMXELEMENT_UNPACK_FIELD( "alpha_max", "255", int, m_nAlphaMax )
	DMXELEMENT_UNPACK_FIELD( "alpha_random_exponent", "1", float, m_flAlphaRandExponent )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomAlpha )


//-----------------------------------------------------------------------------
// Random rotation
//-----------------------------------------------------------------------------
class CGeneralRandomRotation : public CParticleOperatorInstance
{
protected:
	virtual int GetAttributeToInit( void ) const = 0;

	uint32 GetWrittenAttributes( void ) const
	{
		return (1 << GetAttributeToInit() );
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_flRadians = m_flDegrees * ( M_PI / 180.0f );
		m_flRadiansMin = m_flDegreesMin * ( M_PI / 180.0f );
		m_flRadiansMax = m_flDegreesMax * ( M_PI / 180.0f );
	}

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
	{
		if ( m_flRotationRandExponent != 1.0f )
		{
			InitScalarAttributeRandomRangeExpBlock(  GetAttributeToInit(),
				m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent,
				pParticles, start_block, n_blocks );
		}
		else
		{
			InitScalarAttributeRandomRangeBlock(  GetAttributeToInit(),
				m_flRadiansMin, m_flRadiansMax,
				pParticles, start_block, n_blocks );
		}
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
	{
		for( ; nParticleCount--; start_p++ )
		{
			float *drot = pParticles->GetFloatAttributePtrForWrite( GetAttributeToInit(), start_p );
			*drot = m_flRadians + pParticles->RandomFloatExp( m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent );
		}
	}

	// User-specified range
	float m_flDegreesMin;
	float m_flDegreesMax;
	float m_flDegrees;

	// Converted range
	float m_flRadiansMin;
	float m_flRadiansMax;
	float m_flRadians;
	float m_flRotationRandExponent;
};


class CAddGeneralRandomRotation : public CParticleOperatorInstance
{
protected:
	virtual int GetAttributeToInit( void ) const = 0;

	uint32 GetWrittenAttributes( void ) const
	{
		return (1 << GetAttributeToInit() );
	}

	uint32 GetReadAttributes( void ) const
	{
		return (1 << GetAttributeToInit() );
	}

	virtual bool InitMultipleOverride() { return true; }

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_flRadians = m_flDegrees * ( M_PI / 180.0f );
		m_flRadiansMin = m_flDegreesMin * ( M_PI / 180.0f );
		m_flRadiansMax = m_flDegreesMax * ( M_PI / 180.0f );
	}

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
	{
		AddScalarAttributeRandomRangeBlock( GetAttributeToInit(),
			m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent,
			pParticles, start_block, n_blocks, m_bRandomlyFlipDirection );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
	{
		if ( !m_bRandomlyFlipDirection )
		{
			for( ; nParticleCount--; start_p++ )
			{
				float *pAttr = pParticles->GetFloatAttributePtrForWrite( GetAttributeToInit(), start_p );
				*pAttr += m_flRadians + pParticles->RandomFloatExp( m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent );
			}
		}
		else
		{
			for( ; nParticleCount--; start_p++ )
			{
				float *pAttr = pParticles->GetFloatAttributePtrForWrite( GetAttributeToInit(), start_p );
				float flSpeed = m_flRadians + pParticles->RandomFloatExp( m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent );
				bool bFlip = ( pParticles->RandomFloat( -1.0f, 1.0f ) >= 0.0f );
				*pAttr += bFlip ? -flSpeed : flSpeed; 
			}
		}
	}

	// User-specified range
	float m_flDegreesMin;
	float m_flDegreesMax;
	float m_flDegrees;

	// Converted range
	float m_flRadiansMin;
	float m_flRadiansMax;
	float m_flRadians;
	float m_flRotationRandExponent;
	bool m_bRandomlyFlipDirection;
};


//-----------------------------------------------------------------------------
// Random rotation
//-----------------------------------------------------------------------------
class C_INIT_RandomRotation : public CGeneralRandomRotation
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomRotation );

	virtual int GetAttributeToInit( void ) const
	{
		return PARTICLE_ATTRIBUTE_ROTATION;
	}
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomRotation, "Rotation Random", OPERATOR_PI_ROTATION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRotation ) 
	DMXELEMENT_UNPACK_FIELD( "rotation_initial", "0", float, m_flDegrees )
	DMXELEMENT_UNPACK_FIELD( "rotation_offset_min", "0", float, m_flDegreesMin )
	DMXELEMENT_UNPACK_FIELD( "rotation_offset_max", "360", float, m_flDegreesMax )
	DMXELEMENT_UNPACK_FIELD( "rotation_random_exponent", "1", float, m_flRotationRandExponent )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRotation )


//-----------------------------------------------------------------------------
// Random rotation speed
//-----------------------------------------------------------------------------
class C_INIT_RandomRotationSpeed : public CAddGeneralRandomRotation
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomRotationSpeed );

	virtual int GetAttributeToInit( void ) const
	{
		return PARTICLE_ATTRIBUTE_ROTATION_SPEED;
	}
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomRotationSpeed, "Rotation Speed Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRotationSpeed ) 
	DMXELEMENT_UNPACK_FIELD( "rotation_speed_constant", "0", float, m_flDegrees )
	DMXELEMENT_UNPACK_FIELD( "rotation_speed_random_min", "0", float, m_flDegreesMin )
	DMXELEMENT_UNPACK_FIELD( "rotation_speed_random_max", "360", float, m_flDegreesMax )
	DMXELEMENT_UNPACK_FIELD( "rotation_speed_random_exponent", "1", float, m_flRotationRandExponent )
	DMXELEMENT_UNPACK_FIELD( "randomly_flip_direction", "1", bool, m_bRandomlyFlipDirection )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRotationSpeed )


//-----------------------------------------------------------------------------
// Random yaw
//-----------------------------------------------------------------------------
class C_INIT_RandomYaw : public CGeneralRandomRotation
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomYaw );

	virtual int GetAttributeToInit( void ) const
	{
		return PARTICLE_ATTRIBUTE_YAW;
	}
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomYaw, "Rotation Yaw Random", OPERATOR_PI_YAW );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomYaw ) 
	DMXELEMENT_UNPACK_FIELD( "yaw_initial", "0", float, m_flDegrees )
	DMXELEMENT_UNPACK_FIELD( "yaw_offset_min", "0", float, m_flDegreesMin )
	DMXELEMENT_UNPACK_FIELD( "yaw_offset_max", "360", float, m_flDegreesMax )
	DMXELEMENT_UNPACK_FIELD( "yaw_random_exponent", "1", float, m_flRotationRandExponent )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomYaw )


//-----------------------------------------------------------------------------
// Random color
//-----------------------------------------------------------------------------
class C_INIT_RandomColor : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomColor );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_TINT_RGB_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	struct C_OP_RandomColorContext_t
	{
		Vector m_vPrevPosition;
	};

	size_t GetRequiredContextBytes( void ) const
	{
		return sizeof( C_OP_RandomColorContext_t );
	}

	virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
	{
		C_OP_RandomColorContext_t *pCtx=reinterpret_cast<C_OP_RandomColorContext_t *>( pContext );
		pCtx->m_vPrevPosition = vec3_origin;
	}

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_flNormColorMin[0] = (float) m_ColorMin[0] / 255.0f;
		m_flNormColorMin[1] = (float) m_ColorMin[1] / 255.0f;
		m_flNormColorMin[2] = (float) m_ColorMin[2] / 255.0f;

		m_flNormColorMax[0] = (float) m_ColorMax[0] / 255.0f;
		m_flNormColorMax[1] = (float) m_ColorMax[1] / 255.0f;
		m_flNormColorMax[2] = (float) m_ColorMax[2] / 255.0f;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
	{
		C_OP_RandomColorContext_t *pCtx=reinterpret_cast<C_OP_RandomColorContext_t *>( pContext );

		Color	tint( 255, 255, 255, 255 );

		// If we're factoring in luminosity or tint, then get our lighting info for this position
		if ( m_flTintPerc )
		{
			if ( pParticles->m_pParent && pParticles->m_pParent->m_LocalLightingCP == m_nTintCP )
			{
				tint = pParticles->m_pParent->m_LocalLighting;
			}
			else
			{
				// FIXME: Really, we want the emission point for each particle, but for now, we do it more cheaply
				// Get our control point
				Vector vecOrigin;
				pParticles->GetControlPointAtTime( m_nTintCP, pParticles->m_flCurTime, &vecOrigin );

				if ( ( ( pCtx->m_vPrevPosition - vecOrigin ).Length() >= m_flUpdateThreshold ) || ( pParticles->m_LocalLightingCP == -1 ) )
					{
						g_pParticleSystemMgr->Query()->GetLightingAtPoint( vecOrigin, tint );
						pParticles->m_LocalLighting = tint;
						pParticles->m_LocalLightingCP = m_nTintCP;
						pCtx->m_vPrevPosition = vecOrigin;
					}
				else
					tint = pParticles->m_LocalLighting;

			}
			tint[0] = max ( m_TintMin[0], min( tint[0], m_TintMax[0] ) );
			tint[1] = max ( m_TintMin[1], min( tint[1], m_TintMax[1] ) );
			tint[2] = max ( m_TintMin[2], min( tint[2], m_TintMax[2] ) );	
		}

		float randomPerc;
		float *pColor;
		for( ; nParticleCount--; start_p++ )
		{
			pColor = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_TINT_RGB, start_p );
			
			randomPerc = pParticles->RandomFloat( 0.0f, 1.0f );
			
			// Randomly choose a range between the two colors
			pColor[0] = m_flNormColorMin[0] + ( ( m_flNormColorMax[0] - m_flNormColorMin[0] ) * randomPerc );
			pColor[4] = m_flNormColorMin[1] + ( ( m_flNormColorMax[1] - m_flNormColorMin[1] ) * randomPerc );
			pColor[8] = m_flNormColorMin[2] + ( ( m_flNormColorMax[2] - m_flNormColorMin[2] ) * randomPerc );

			// Tint the particles
			if ( m_flTintPerc )
			{
				pColor[0] = Lerp( m_flTintPerc, (float) pColor[0], (float) tint.r() / 255.0f );
				pColor[4] = Lerp( m_flTintPerc, (float) pColor[4], (float) tint.g() / 255.0f );
				pColor[8] = Lerp( m_flTintPerc, (float) pColor[8], (float) tint.b() / 255.0f );
			}
		}
	}

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
	{
		C_OP_RandomColorContext_t *pCtx=reinterpret_cast<C_OP_RandomColorContext_t *>( pContext );

		Color	tint( 255, 255, 255, 255 );

		size_t attr_stride;

		FourVectors *pColor = pParticles->Get4VAttributePtrForWrite( PARTICLE_ATTRIBUTE_TINT_RGB, &attr_stride );
		
		pColor += attr_stride * start_block;
		
		FourVectors fvColorMin;
		fvColorMin.DuplicateVector( Vector (m_flNormColorMin[0], m_flNormColorMin[1], m_flNormColorMin[2] ) );
		FourVectors fvColorWidth;
		fvColorWidth.DuplicateVector( Vector (m_flNormColorMax[0] - m_flNormColorMin[0], m_flNormColorMax[1] - m_flNormColorMin[1], m_flNormColorMax[2] - m_flNormColorMin[2] ) );

		int nRandContext = GetSIMDRandContext();

		// If we're factoring in luminosity or tint, then get our lighting info for this position
		if ( m_flTintPerc )
		{
			if ( pParticles->m_pParent && pParticles->m_pParent->m_LocalLightingCP == m_nTintCP )
			{
				tint = pParticles->m_pParent->m_LocalLighting;
			}
			else
			{
				// FIXME: Really, we want the emission point for each particle, but for now, we do it more cheaply
				// Get our control point
				Vector vecOrigin;
				pParticles->GetControlPointAtTime( m_nTintCP, pParticles->m_flCurTime, &vecOrigin );

				if ( ( ( pCtx->m_vPrevPosition - vecOrigin ).Length() >= m_flUpdateThreshold ) || ( pParticles->m_LocalLightingCP == -1 ) )
				{
					g_pParticleSystemMgr->Query()->GetLightingAtPoint( vecOrigin, tint );
					pParticles->m_LocalLighting = tint;
					pParticles->m_LocalLightingCP = m_nTintCP;
					pCtx->m_vPrevPosition = vecOrigin;
				}
				else
					tint = pParticles->m_LocalLighting;
			}

			tint[0] = max ( m_TintMin[0], min( tint[0], m_TintMax[0] ) );
			tint[1] = max ( m_TintMin[1], min( tint[1], m_TintMax[1] ) );
			tint[2] = max ( m_TintMin[2], min( tint[2], m_TintMax[2] ) );

			FourVectors fvTint;
			fvTint.DuplicateVector( Vector ( tint[0], tint[1], tint[2] ) );
			fltx4 fl4Divisor = ReplicateX4( 1.0f / 255.0f );
			fvTint *= fl4Divisor;
			fltx4 fl4TintPrc = ReplicateX4( m_flTintPerc );

			while( n_blocks-- )
			{
				FourVectors fvColor = fvColorWidth;
				FourVectors fvColor2 = fvTint;
				fvColor *= RandSIMD( nRandContext );
				fvColor += fvColorMin;
				fvColor2 -= fvColor;
				fvColor2 *= fl4TintPrc;
				fvColor2 += fvColor;
				*pColor = fvColor2;
				pColor += attr_stride;
			}
		}
		else
		{
			while( n_blocks-- )
			{
				FourVectors fvColor = fvColorWidth;
				fvColor *= RandSIMD( nRandContext );
				fvColor += fvColorMin;
				*pColor = fvColor;
				pColor += attr_stride;
			}
		}
		ReleaseSIMDRandContext( nRandContext );
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nTintCP;
	}

	float	m_flNormColorMin[3];
	float	m_flNormColorMax[3];
	Color	m_ColorMin;
	Color	m_ColorMax;
	Color	m_TintMin;
	Color	m_TintMax;
	float	m_flTintPerc;
	float	m_flUpdateThreshold;
	int		m_nTintCP;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomColor, "Color Random", OPERATOR_PI_TINT_RGB );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomColor ) 
	DMXELEMENT_UNPACK_FIELD( "color1", "255 255 255 255", Color, m_ColorMin )
	DMXELEMENT_UNPACK_FIELD( "color2", "255 255 255 255", Color, m_ColorMax )
	DMXELEMENT_UNPACK_FIELD( "tint_perc", "0.0", float, m_flTintPerc )
	DMXELEMENT_UNPACK_FIELD( "tint control point", "0", int, m_nTintCP )
	DMXELEMENT_UNPACK_FIELD( "tint clamp min", "0 0 0 0", Color, m_TintMin )
	DMXELEMENT_UNPACK_FIELD( "tint clamp max", "255 255 255 255", Color, m_TintMax )
	DMXELEMENT_UNPACK_FIELD( "tint update movement threshold", "32", float, m_flUpdateThreshold )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomColor )


//-----------------------------------------------------------------------------
// Trail Length
//-----------------------------------------------------------------------------
class C_INIT_RandomTrailLength : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomTrailLength );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_TRAIL_LENGTH_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
	}

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
	{
		if ( m_flLengthRandExponent != 1.0f )
		{
			InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_TRAIL_LENGTH,
				m_flMinLength, m_flMaxLength, m_flLengthRandExponent,
				pParticles, start_block, n_blocks );
		}
		else
		{
			InitScalarAttributeRandomRangeBlock( PARTICLE_ATTRIBUTE_TRAIL_LENGTH,
				m_flMinLength, m_flMaxLength,
				pParticles, start_block, n_blocks );
		}
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
	{
		float *pLength;
		for( ; nParticleCount--; start_p++ )
		{
			pLength = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_TRAIL_LENGTH, start_p );
			*pLength = pParticles->RandomFloatExp( m_flMinLength, m_flMaxLength, m_flLengthRandExponent );
		}
	}

	float m_flMinLength;
	float m_flMaxLength;
	float m_flLengthRandExponent;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomTrailLength, "Trail Length Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomTrailLength ) 
	DMXELEMENT_UNPACK_FIELD( "length_min", "0.1", float, m_flMinLength )
	DMXELEMENT_UNPACK_FIELD( "length_max", "0.1", float, m_flMaxLength )
	DMXELEMENT_UNPACK_FIELD( "length_random_exponent", "1", float, m_flLengthRandExponent )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomTrailLength )

//-----------------------------------------------------------------------------
// Random sequence
//-----------------------------------------------------------------------------
class C_INIT_RandomSequence : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomSequence );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		// TODO: Validate the ranges here!
	}

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
	{
		InitScalarAttributeRandomRangeBlock( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER,
			m_nSequenceMin, m_nSequenceMax,
			pParticles, start_block, n_blocks );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
	{
		float *pSequence;
		for( ; nParticleCount--; start_p++ )
		{
			pSequence = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER, start_p );
			*pSequence = pParticles->RandomInt( m_nSequenceMin, m_nSequenceMax );
		}
	}

	int m_nSequenceMin;
	int m_nSequenceMax;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomSequence, "Sequence Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomSequence ) 
	DMXELEMENT_UNPACK_FIELD( "sequence_min", "0", int, m_nSequenceMin )
	DMXELEMENT_UNPACK_FIELD( "sequence_max", "0", int, m_nSequenceMax )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomSequence )

 
//-----------------------------------------------------------------------------
// Position Warp Initializer
// Scales initial position and velocity of particles within a random vector range
//-----------------------------------------------------------------------------
class C_INIT_PositionWarp : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_PositionOffset );

	Vector m_vecWarpMin;
	Vector m_vecWarpMax;
	int m_nControlPointNumber;
	float m_flWarpTime, m_flWarpStartTime;
	bool m_bInvertWarp;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
	}

	bool InitMultipleOverride ( void ) { return true; }

};

DEFINE_PARTICLE_OPERATOR( C_INIT_PositionWarp, "Position Modify Warp Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_PositionWarp ) 
	DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
	DMXELEMENT_UNPACK_FIELD( "warp min", "1 1 1", Vector, m_vecWarpMin )
	DMXELEMENT_UNPACK_FIELD( "warp max", "1 1 1", Vector, m_vecWarpMax )
	DMXELEMENT_UNPACK_FIELD( "warp transition time (treats min/max as start/end sizes)", "0", float , m_flWarpTime )
	DMXELEMENT_UNPACK_FIELD( "warp transition start time", "0", float , m_flWarpStartTime )
	DMXELEMENT_UNPACK_FIELD( "reverse warp (0/1)", "0", bool , m_bInvertWarp )	
END_PARTICLE_OPERATOR_UNPACK( C_INIT_PositionWarp )


void C_INIT_PositionWarp::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	Vector vecWarpStart = m_vecWarpMin;
	Vector vecWarpEnd = m_vecWarpMax;

	if ( m_bInvertWarp )
	{
		vecWarpStart = m_vecWarpMax;
		vecWarpEnd = m_vecWarpMin;
	}

	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		
		Vector randpos;
		
		if ( m_flWarpTime != 0.0f )
		{ 
			float flWarpEnd = m_flWarpStartTime + m_flWarpTime;
			float flPercentage = RemapValClamped( *ct, m_flWarpStartTime, flWarpEnd, 0.0, 1.0 );
			VectorLerp( vecWarpStart, vecWarpEnd, flPercentage, randpos );
		}
		else
		{
			pParticles->RandomVector( m_vecWarpMin, m_vecWarpMax, &randpos );
		}


		matrix3x4_t mat;
		pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *ct, &mat );
		Vector vecTransformLocal = vec3_origin;
		Vector vecParticlePosition, vecParticlePosition_prev ;
		SetVectorFromAttribute( vecParticlePosition, xyz ); 
		SetVectorFromAttribute( vecParticlePosition_prev, pxyz );
		// rotate particles from world space into local
		VectorITransform( vecParticlePosition, mat, vecTransformLocal );
		// multiply position by desired amount
		vecTransformLocal.x *= randpos.x;
		vecTransformLocal.y *= randpos.y;
		vecTransformLocal.z *= randpos.z;
		// rotate back into world space
		VectorTransform( vecTransformLocal, mat, vecParticlePosition );
		// rinse, repeat
		VectorITransform( vecParticlePosition_prev, mat, vecTransformLocal ); 
		vecTransformLocal.x *= randpos.x;
		vecTransformLocal.y *= randpos.y;
		vecTransformLocal.z *= randpos.z;
		VectorTransform( vecTransformLocal, mat, vecParticlePosition_prev );
		// set positions into floats
		SetVectorAttribute( xyz, vecParticlePosition ); 
		SetVectorAttribute( pxyz, vecParticlePosition_prev ); 
	}
}


//-----------------------------------------------------------------------------
// noise initializer
//-----------------------------------------------------------------------------
class C_INIT_CreationNoise : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreationNoise );

	uint32 GetWrittenAttributes( void ) const
	{
		return 1 << m_nFieldOutput;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

	void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const;

	virtual bool IsScrubSafe() { return true; }
	int		m_nFieldOutput;
	bool	m_bAbsVal, m_bAbsValInv;
	float	m_flOffset;
	float	m_flOutputMin;
	float	m_flOutputMax;
	float	m_flNoiseScale, m_flNoiseScaleLoc;
	Vector  m_vecOffsetLoc;
	float   m_flWorldTimeScale;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreationNoise, "Remap Noise to Scalar", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreationNoise )
	DMXELEMENT_UNPACK_FIELD( "time noise coordinate scale","0.1",float,m_flNoiseScale)
	DMXELEMENT_UNPACK_FIELD( "spatial noise coordinate scale","0.001",float,m_flNoiseScaleLoc)
	DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
	DMXELEMENT_UNPACK_FIELD( "time coordinate offset","0", float, m_flOffset )
	DMXELEMENT_UNPACK_FIELD( "spatial coordinate offset","0 0 0", Vector, m_vecOffsetLoc )
	DMXELEMENT_UNPACK_FIELD( "absolute value","0", bool, m_bAbsVal )
	DMXELEMENT_UNPACK_FIELD( "invert absolute value","0", bool, m_bAbsValInv )
	DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
	DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
	DMXELEMENT_UNPACK_FIELD( "world time noise coordinate scale","0", float, m_flWorldTimeScale )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreationNoise );




void C_INIT_CreationNoise::InitNewParticlesBlock( CParticleCollection *pParticles, 
												 int start_block, int n_blocks, int nAttributeWriteMask,
												 void *pContext ) const
{
	float		flAbsScale;
	fltx4 		fl4AbsVal;
	fl4AbsVal = CmpEqSIMD( Four_Zeros, Four_Zeros ); 
	flAbsScale = 0.5;

	// Set up values for more optimal absolute value calculations inside the loop
	if ( m_bAbsVal )
	{
		fl4AbsVal = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
		flAbsScale = 1.0;
	}

	float fMin = m_flOutputMin;
	float fMax = m_flOutputMax;	

	if ( ATTRIBUTES_WHICH_ARE_ANGLES & (1 << m_nFieldOutput ) )
	{
		fMin *= ( M_PI / 180.0f );
		fMax *= ( M_PI / 180.0f );
	}	

	float CoordScale = m_flNoiseScale;
	float CoordScaleLoc = m_flNoiseScaleLoc;

	float ValueScale, ValueBase;
	ValueScale = ( flAbsScale *( fMax - fMin ) );
	ValueBase = ( fMin+ ( ( 1.0 - flAbsScale ) *( fMax - fMin ) ) );

	fltx4 fl4ValueBase = ReplicateX4( ValueBase );
	fltx4 fl4ValueScale = ReplicateX4( ValueScale );

	size_t attr_stride;

	fltx4 *pAttr = pParticles->GetM128AttributePtrForWrite( m_nFieldOutput, &attr_stride );
	pAttr += attr_stride * start_block;
	const FourVectors *pxyz = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
	pxyz += attr_stride * start_block;
	const fltx4 *pCreationTime = pParticles->GetM128AttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, &attr_stride );
	pCreationTime += attr_stride * start_block;

	//setup
	fltx4 fl4Offset = ReplicateX4( m_flOffset );
	FourVectors fvOffsetLoc;
	fvOffsetLoc.DuplicateVector( m_vecOffsetLoc );
	FourVectors fvCoordBase;
	fvCoordBase.x = AddSIMD(*pCreationTime, fl4Offset);
	fvCoordBase.y = AddSIMD(*pCreationTime, fl4Offset);
	fvCoordBase.z = AddSIMD(*pCreationTime, fl4Offset);
	fvCoordBase *= CoordScale;

	while( n_blocks-- )
	{	
		FourVectors fvCoordLoc = *pxyz;
		fvCoordLoc += fvOffsetLoc;
		FourVectors fvCoord = fvCoordBase;
		fvCoordLoc *= CoordScaleLoc;
		fvCoord += fvCoordLoc;

		fltx4 fl4Noise;

		fl4Noise = NoiseSIMD( fvCoord );

		fl4Noise = AndSIMD ( fl4Noise, fl4AbsVal );

		if ( m_bAbsValInv )
		{
			fl4Noise = SubSIMD( Four_Ones, fl4Noise );
		}

		fltx4 fl4InitialNoise;

		fl4InitialNoise = AddSIMD( fl4ValueBase, ( MulSIMD( fl4ValueScale, fl4Noise ) ) );

		if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & (1 << m_nFieldOutput ) )
		{
			fl4InitialNoise = MinSIMD( Four_Ones, fl4InitialNoise );
			fl4InitialNoise = MaxSIMD( Four_Zeros, fl4InitialNoise );
		}

		*( pAttr ) = fl4InitialNoise;

		pAttr += attr_stride;
		pxyz += attr_stride;

	}
}



void C_INIT_CreationNoise::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	float	flAbsScale;
	int		nAbsVal;
	nAbsVal = 0xffffffff; 
	flAbsScale = 0.5;
	if ( m_bAbsVal )
	{
		nAbsVal = 0x7fffffff;
		flAbsScale = 1.0;
	}

	float fMin = m_flOutputMin;
	float fMax = m_flOutputMax;

	if ( ATTRIBUTES_WHICH_ARE_ANGLES & (1 << m_nFieldOutput ) )
	{
		fMin *= ( M_PI / 180.0f );
		fMax *= ( M_PI / 180.0f );
	}

	float CoordScale = m_flNoiseScale;
	float CoordScaleLoc = m_flNoiseScaleLoc;

    float ValueScale, ValueBase;
	ValueScale = ( flAbsScale *( fMax - fMin ) );
	ValueBase = ( fMin+ ( ( 1.0 - flAbsScale ) *( fMax - fMin ) ) );
	
	Vector CoordLoc, CoordWorldTime, CoordBase;
	const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
	float Offset = m_flOffset;
	CoordBase = Vector ( (*pCreationTime + Offset), (*pCreationTime + Offset), (*pCreationTime + Offset) );
	CoordBase *= CoordScale;
	CoordWorldTime = Vector( (Plat_MSTime() * m_flWorldTimeScale), (Plat_MSTime() * m_flWorldTimeScale), (Plat_MSTime() * m_flWorldTimeScale) );
	CoordBase += CoordWorldTime;

	for( ; nParticleCount--; start_p++ )
	{	
		const float *pxyz = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );
		float *pAttr = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );	

		Vector Coord = CoordBase;

		CoordLoc.x = pxyz[0]; 
		CoordLoc.y = pxyz[4];
		CoordLoc.z = pxyz[8];
		CoordLoc += m_vecOffsetLoc;

		CoordLoc *= CoordScaleLoc;
		Coord += CoordLoc;

		fltx4 flNoise128;
		FourVectors fvNoise;

		fvNoise.DuplicateVector( Coord );
		flNoise128 = NoiseSIMD( fvNoise );
		float flNoise = SubFloat( flNoise128, 0 );

		*( (int *) &flNoise)  &= nAbsVal;

		if ( m_bAbsValInv )
		{
			flNoise = 1.0 - flNoise;
		}
		    
		float flInitialNoise = ( ValueBase + ( ValueScale * flNoise ) );

		if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & (1 << m_nFieldOutput ) )
		{
			flInitialNoise = clamp(flInitialNoise, 0.0f, 1.0f );
		}

		*( pAttr ) = flInitialNoise;
	}
}






class C_INIT_CreateAlongPath : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateAlongPath );

	float m_fMaxDistance;
	struct CPathParameters m_PathParams;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		uint64 nStartMask = ( 1ULL << m_PathParams.m_nStartControlPointNumber ) - 1;
		uint64 nEndMask = ( 1ULL << ( m_PathParams.m_nEndControlPointNumber + 1 ) ) - 1;
		return nEndMask & (~nStartMask);
	}

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_PathParams.ClampControlPointIndices();
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateAlongPath, "Position Along Path Random", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateAlongPath ) 
	DMXELEMENT_UNPACK_FIELD( "maximum distance", "0", float, m_fMaxDistance )
	DMXELEMENT_UNPACK_FIELD( "bulge", "0", float, m_PathParams.m_flBulge )
	DMXELEMENT_UNPACK_FIELD( "start control point number", "0", int, m_PathParams.m_nStartControlPointNumber )
	DMXELEMENT_UNPACK_FIELD( "end control point number", "0", int, m_PathParams.m_nEndControlPointNumber )
	DMXELEMENT_UNPACK_FIELD( "bulge control 0=random 1=orientation of start pnt 2=orientation of end point", "0", int, m_PathParams.m_nBulgeControl )
	DMXELEMENT_UNPACK_FIELD( "mid point position", "0.5", float, m_PathParams.m_flMidPoint )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateAlongPath )


void C_INIT_CreateAlongPath::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );


		Vector StartPnt, MidP, EndPnt;
		pParticles->CalculatePathValues( m_PathParams, *ct, &StartPnt, &MidP, &EndPnt);

		float t=pParticles->RandomFloat( 0.0, 1.0 );
		
		Vector randpos;
		pParticles->RandomVector( -m_fMaxDistance, m_fMaxDistance, &randpos );

		// form delta terms needed for quadratic bezier
		Vector Delta0=MidP-StartPnt;
		Vector Delta1 = EndPnt-MidP;

		Vector L0 = StartPnt+t*Delta0;
		Vector L1 = MidP+t*Delta1;

		Vector Pnt = L0+(L1-L0)*t;

		Pnt+=randpos;

		xyz[0] = Pnt.x;
		xyz[4] = Pnt.y;
		xyz[8] = Pnt.z;
		if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
		{
			pxyz[0] = Pnt.x;
			pxyz[4] = Pnt.y;
			pxyz[8] = Pnt.z;
		}
	}
}





class C_INIT_MoveBetweenPoints : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_MoveBetweenPoints );

	float m_flSpeedMin, m_flSpeedMax;
	float m_flEndSpread;
	float m_flStartOffset;
	int m_nEndControlPointNumber;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nEndControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

};

DEFINE_PARTICLE_OPERATOR( C_INIT_MoveBetweenPoints, "Move Particles Between 2 Control Points", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_MoveBetweenPoints ) 
	DMXELEMENT_UNPACK_FIELD( "minimum speed", "1", float, m_flSpeedMin )
	DMXELEMENT_UNPACK_FIELD( "maximum speed", "1", float, m_flSpeedMax )
	DMXELEMENT_UNPACK_FIELD( "end spread", "0", float, m_flEndSpread )
	DMXELEMENT_UNPACK_FIELD( "start offset", "0", float, m_flStartOffset )
	DMXELEMENT_UNPACK_FIELD( "end control point", "1", int, m_nEndControlPointNumber )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_MoveBetweenPoints )


void C_INIT_MoveBetweenPoints::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	bool bMoveStartPnt = ( m_flStartOffset > 0.0 );
	for( ; nParticleCount--; start_p++ )
	{
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		float *pPrevXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );

		float *dtime = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );


		Vector StartPnt( pxyz[0], pxyz[4], pxyz[8] );

		Vector vecControlPoint;

		pParticles->GetControlPointAtTime( m_nEndControlPointNumber, *ct, &vecControlPoint );

		Vector randpos(0,0,0);

		if ( m_flEndSpread > 0.0 )
		{
			pParticles->RandomVectorInUnitSphere( &randpos );
			randpos *= m_flEndSpread;
		}
		
		vecControlPoint += randpos;

		Vector vDelta = vecControlPoint - StartPnt;
		float flLen = VectorLength( vDelta );

		if ( bMoveStartPnt )
		{
			StartPnt += ( m_flStartOffset/(flLen+FLT_EPSILON) ) * vDelta;
			vDelta = vecControlPoint - StartPnt;			
			flLen = VectorLength( vDelta );
		}

		float flVel = pParticles->RandomFloat( m_flSpeedMin, m_flSpeedMax );

		*dtime = flLen/( flVel+FLT_EPSILON);

		Vector poffset = vDelta * (flVel/flLen ) ;

		poffset *= pParticles->m_flPreviousDt;

		if ( bMoveStartPnt )
		{
			pxyz[0] = StartPnt.x;
			pxyz[1] = StartPnt.y;
			pxyz[2] = StartPnt.z;
		}

		pPrevXYZ[0] = pxyz[0] - poffset.x;
		pPrevXYZ[4] = pxyz[4] - poffset.y;
		pPrevXYZ[8] = pxyz[8] - poffset.z;
	}
}




//-----------------------------------------------------------------------------
// Remap Scalar Initializer
//-----------------------------------------------------------------------------
class C_INIT_RemapScalar : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RemapScalar );

	uint32 GetWrittenAttributes( void ) const
	{
		return 1 << m_nFieldOutput;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 1 << m_nFieldInput;
	}

	bool InitMultipleOverride ( void ) { return true; }
	
	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	int		m_nFieldInput;
	int		m_nFieldOutput;
	float	m_flInputMin;
	float	m_flInputMax;
	float	m_flOutputMin;
	float	m_flOutputMax;
	float	m_flStartTime;
	float	m_flEndTime;
	bool	m_bScaleInitialRange;
	bool	m_bActiveRange;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RemapScalar, "Remap Initial Scalar", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapScalar )
	DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
	DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
	DMXELEMENT_UNPACK_FIELD_USERDATA( "input field", "8", int, m_nFieldInput, "intchoice particlefield_scalar" )
	DMXELEMENT_UNPACK_FIELD( "input minimum","0", float, m_flInputMin )
	DMXELEMENT_UNPACK_FIELD( "input maximum","1", float, m_flInputMax )
	DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
	DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
	DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
	DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
	DMXELEMENT_UNPACK_FIELD( "only active within specified input range","0", bool, m_bActiveRange )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapScalar )

void C_INIT_RemapScalar::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	const float *pCreationTime;
	// clamp the result to 0 and 1 if it's alpha
	float flMin=m_flOutputMin;
	float flMax=m_flOutputMax;
	if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
	{
		flMin = clamp(m_flOutputMin, 0.0f, 1.0f );
		flMax = clamp(m_flOutputMax, 0.0f, 1.0f );
	}

	// FIXME: SSE-ize
	for( ; nParticleCount--; start_p++ )
	{
		pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		// using raw creation time to map to emitter lifespan
		float flLifeTime = *pCreationTime;  

		float flInput;
		if ( ATTRIBUTES_WHICH_ARE_INTS & ( 1 << m_nFieldInput ) )
		{
			const int *pInput = pParticles->GetIntAttributePtr( m_nFieldInput, start_p );
			flInput = float( *pInput );
		}
		else
		{
			const float *pInput = pParticles->GetFloatAttributePtr( m_nFieldInput, start_p );
			flInput = *pInput;
		}

		// only use within start/end time frame and, if set, active input range
		if ( ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) ) || ( m_bActiveRange && ( flInput < m_flInputMin || flInput > m_flInputMax ) ) )
			continue;

		float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
		float flOutput = RemapValClamped( flInput, m_flInputMin, m_flInputMax, flMin, flMax  );
		if ( m_bScaleInitialRange )
		{
			flOutput = *pOutput * flOutput;
		}
		if ( ATTRIBUTES_WHICH_ARE_INTS & ( 1 << m_nFieldOutput ) )
		{
			*pOutput = int ( flOutput );
		}
		else
		{
			*pOutput = flOutput;
		}
	}
}




//-----------------------------------------------------------------------------
// Inherit Velocity Initializer
// Causes particles to inherit the velocity of their CP at spawn
// 
//-----------------------------------------------------------------------------
class C_INIT_InheritVelocity : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_InheritVelocity );

	int m_nControlPointNumber;
	float m_flVelocityScale;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK ;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
	}

	bool InitMultipleOverride ( void ) { return true; }

};

DEFINE_PARTICLE_OPERATOR( C_INIT_InheritVelocity, "Velocity Inherit from Control Point", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_InheritVelocity ) 
DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
DMXELEMENT_UNPACK_FIELD( "velocity scale", "1", float, m_flVelocityScale )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_InheritVelocity )


void C_INIT_InheritVelocity::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		
		Vector vecControlPoint;
		pParticles->GetControlPointAtTime( m_nControlPointNumber, *ct, &vecControlPoint );
		Vector vecControlPointPrev;
		pParticles->GetControlPointAtPrevTime( m_nControlPointNumber, &vecControlPointPrev );

		Vector vecDeltaPos = (vecControlPoint - vecControlPointPrev);
		//Vector vecDeltaPos = (vecControlPoint - vecControlPointPrev) * pParticles->m_flDt;
		vecDeltaPos.x *= m_flVelocityScale;
		vecDeltaPos.y *= m_flVelocityScale;
		vecDeltaPos.z *= m_flVelocityScale;

		xyz[0] += vecDeltaPos.x;
		xyz[4] += vecDeltaPos.y;
		xyz[8] += vecDeltaPos.z;
	}
}


//-----------------------------------------------------------------------------
// Pre-Age Noise
// Sets particle creation time back to treat newly spawned particle as if 
// part of its life has already elapsed.
//-----------------------------------------------------------------------------
class C_INIT_AgeNoise : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_AgeNoise );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	bool InitMultipleOverride ( void ) { return true; }

	bool	m_bAbsVal, m_bAbsValInv;
	float	m_flOffset;
	float	m_flAgeMin;
	float	m_flAgeMax;
	float	m_flNoiseScale, m_flNoiseScaleLoc;
	Vector  m_vecOffsetLoc;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_AgeNoise, "Lifetime Pre-Age Noise", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_AgeNoise )
DMXELEMENT_UNPACK_FIELD( "time noise coordinate scale","1.0",float,m_flNoiseScale)
DMXELEMENT_UNPACK_FIELD( "spatial noise coordinate scale","1.0",float,m_flNoiseScaleLoc)
DMXELEMENT_UNPACK_FIELD( "time coordinate offset","0", float, m_flOffset )
DMXELEMENT_UNPACK_FIELD( "spatial coordinate offset","0 0 0", Vector, m_vecOffsetLoc )
DMXELEMENT_UNPACK_FIELD( "absolute value","0", bool, m_bAbsVal )
DMXELEMENT_UNPACK_FIELD( "invert absolute value","0", bool, m_bAbsValInv )
DMXELEMENT_UNPACK_FIELD( "start age minimum","0", float, m_flAgeMin )
DMXELEMENT_UNPACK_FIELD( "start age maximum","1", float, m_flAgeMax )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_AgeNoise );

void C_INIT_AgeNoise::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	float	flAbsScale;
	int		nAbsVal;
	nAbsVal = 0xffffffff; 
	flAbsScale = 0.5;
	if ( m_bAbsVal )
	{
		nAbsVal = 0x7fffffff;
		flAbsScale = 1.0;
	}

	float fMin = m_flAgeMin;
	float fMax = m_flAgeMax;

	float CoordScale = m_flNoiseScale;
	float CoordScaleLoc = m_flNoiseScaleLoc;

	for( ; nParticleCount--; start_p++ )
	{	
		const float *pxyz = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );
		const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		const float *pLifespan = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
		float *pAttr = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );		

		float ValueScale, ValueBase;

		Vector Coord, CoordLoc;
		CoordLoc.x = pxyz[0]; 
		CoordLoc.y = pxyz[4];
		CoordLoc.z = pxyz[8];
		CoordLoc += m_vecOffsetLoc;

		float Offset = m_flOffset;
		Coord = Vector ( (*pCreationTime + Offset), (*pCreationTime + Offset), (*pCreationTime + Offset) );
		Coord *= CoordScale;
		CoordLoc *= CoordScaleLoc;
		Coord += CoordLoc;

		fltx4 flNoise128;
		FourVectors fvNoise;

		fvNoise.DuplicateVector( Coord );
		flNoise128 = NoiseSIMD( fvNoise );
		float flNoise = SubFloat( flNoise128, 0 );

		*( (int *) &flNoise)  &= nAbsVal;

		ValueScale = ( flAbsScale *( fMax - fMin ) );
		ValueBase = ( fMin+ ( ( 1.0 - flAbsScale ) *( fMax - fMin ) ) );

		if ( m_bAbsValInv )
		{
			flNoise = 1.0 - flNoise;
		}

		float flInitialNoise = ( ValueBase + ( ValueScale * flNoise ) );


		flInitialNoise = clamp(flInitialNoise, 0.0f, 1.0f );
		flInitialNoise *= *pLifespan;

		*( pAttr ) = *pCreationTime - flInitialNoise;
	}
}




//-----------------------------------------------------------------------------
// LifeTime Sequence Length
//-----------------------------------------------------------------------------
class C_INIT_SequenceLifeTime : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_SequenceLifeTime );

	float m_flFramerate;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER_MASK;
	}

	bool InitMultipleOverride ( void ) { return true; }

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask, void *pContext ) const;

};

DEFINE_PARTICLE_OPERATOR( C_INIT_SequenceLifeTime, "Lifetime From Sequence", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_SequenceLifeTime ) 
DMXELEMENT_UNPACK_FIELD( "Frames Per Second", "30", float, m_flFramerate )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_SequenceLifeTime )

void C_INIT_SequenceLifeTime::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	if ( ( m_flFramerate != 0.0f ) && ( pParticles->m_Sheet() ) )
	{
		for( ; nParticleCount--; start_p++ )
		{
			const float *flSequence = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER, start_p );
			float *dtime = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
			int nSequence = *flSequence;

			if ( pParticles->m_Sheet()->m_flFrameSpan[nSequence] != 0 )
			{
				*dtime = pParticles->m_Sheet()->m_flFrameSpan[nSequence] / m_flFramerate;
			}
			else
			{
				*dtime = 1.0;
			}
		}
	}
}




//-----------------------------------------------------------------------------
// Create In Hierarchy
//-----------------------------------------------------------------------------
class C_INIT_CreateInHierarchy : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateInHierarchy );

	float m_fMaxDistance;
	float m_flGrowthTime;
	//float m_flTraceDist; 
	float m_flDesiredMidPoint;
	int m_nOrientation;
	float m_flBulgeFactor;
	int m_nDesiredEndPoint;
	int m_nDesiredStartPoint;
	bool m_bUseHighestEndCP;
	Vector m_vecDistanceBias, m_vecDistanceBiasAbs;
	bool m_bDistanceBias, m_bDistanceBiasAbs;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		uint64 nStartMask = ( 1ULL << m_nDesiredStartPoint ) - 1;
		uint64 nEndMask = m_bUseHighestEndCP ? 0xFFFFFFFFFFFFFFFFll : ( 1ULL << ( m_nDesiredEndPoint + 1 ) ) - 1;
		return nEndMask & (~nStartMask);
	}

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		//fixme - confirm CPs
		//		m_PathParams.ClampControlPointIndices();
		m_bDistanceBias = ( m_vecDistanceBias.x != 1.0f ) || ( m_vecDistanceBias.y != 1.0f ) || ( m_vecDistanceBias.z != 1.0f );
		m_bDistanceBiasAbs = ( m_vecDistanceBiasAbs.x != 0.0f ) || ( m_vecDistanceBiasAbs.y != 0.0f ) || ( m_vecDistanceBiasAbs.z != 0.0f );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
								 int nParticleCount, int nAttributeWriteMask,
								 void *pContext) const;

};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateInHierarchy, "Position In CP Hierarchy", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateInHierarchy ) 
	DMXELEMENT_UNPACK_FIELD( "maximum distance", "0", float, m_fMaxDistance )
	DMXELEMENT_UNPACK_FIELD( "bulge", "0", float, m_flBulgeFactor )
	DMXELEMENT_UNPACK_FIELD( "start control point number", "0", int, m_nDesiredStartPoint )
	DMXELEMENT_UNPACK_FIELD( "end control point number", "1", int, m_nDesiredEndPoint )
	DMXELEMENT_UNPACK_FIELD( "bulge control 0=random 1=orientation of start pnt 2=orientation of end point", "0", int, m_nOrientation )
	DMXELEMENT_UNPACK_FIELD( "mid point position", "0.5", float, m_flDesiredMidPoint )
	DMXELEMENT_UNPACK_FIELD( "growth time", "0.0", float, m_flGrowthTime )
	//DMXELEMENT_UNPACK_FIELD( "trace distance for optional culling", "0.0", float, m_flTraceDist )
	DMXELEMENT_UNPACK_FIELD( "use highest supplied end point", "0", bool, m_bUseHighestEndCP )
	DMXELEMENT_UNPACK_FIELD( "distance_bias", "1 1 1", Vector, m_vecDistanceBias )
	DMXELEMENT_UNPACK_FIELD( "distance_bias_absolute_value", "0 0 0", Vector, m_vecDistanceBiasAbs )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateInHierarchy )


void C_INIT_CreateInHierarchy::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	int nEndCP;
	float flGrowth;
	struct CPathParameters PathParams;
	PathParams.m_flBulge = m_flBulgeFactor;
	PathParams.m_nBulgeControl = m_nOrientation;
	PathParams.m_flMidPoint = m_flDesiredMidPoint;
	int nRealEndPoint;

	if ( m_bUseHighestEndCP )
	{
		nRealEndPoint = pParticles->GetHighestControlPoint();
	}
	else
	{
		nRealEndPoint = m_nDesiredEndPoint;
	}

	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );

		if ( ( pParticles->m_flCurTime <= m_flGrowthTime ) && ( nRealEndPoint > 0 ) )
		{
			float flCurrentEndCP = RemapValClamped( *ct, 0.0f, m_flGrowthTime, min( m_nDesiredStartPoint + 1, nRealEndPoint ), nRealEndPoint );
			nEndCP =  pParticles->RandomInt( min( m_nDesiredStartPoint + 1, (int)flCurrentEndCP ), flCurrentEndCP );

			// clamp growth to the appropriate values...
			float flEndTime = flCurrentEndCP / float(nRealEndPoint) ;
			flGrowth = RemapValClamped( *ct, 0.0f, m_flGrowthTime, 0.0, flEndTime );
		}
		else
		{
			int nLowestStartPoint =  min( m_nDesiredStartPoint + 1, nRealEndPoint );
			nEndCP =  pParticles->RandomInt( nLowestStartPoint, nRealEndPoint );
			flGrowth = 1.0;
		}


		PathParams.m_nStartControlPointNumber = pParticles->m_ControlPoints[nEndCP].m_nParent;
		PathParams.m_nEndControlPointNumber = nEndCP;
		Vector StartPnt, MidP, EndPnt;

		pParticles->CalculatePathValues( PathParams, *ct, &StartPnt, &MidP, &EndPnt);
		EndPnt *= flGrowth;

		float t=pParticles->RandomFloat( 0.0, 1.0 );
		
		Vector randpos;
		pParticles->RandomVector( -m_fMaxDistance, m_fMaxDistance, &randpos );

		if ( m_bDistanceBiasAbs	)
		{
			if ( m_vecDistanceBiasAbs.x	!= 0.0f )
			{
				randpos.x = fabs(randpos.x);
			}
			if ( m_vecDistanceBiasAbs.y	!= 0.0f )
			{
				randpos.y = fabs(randpos.y);
			}
			if ( m_vecDistanceBiasAbs.z	!= 0.0f )
			{
				randpos.z = fabs(randpos.z);
			}
		}
		randpos *= m_vecDistanceBias;

		// form delta terms needed for quadratic bezier
		Vector Delta0=MidP-StartPnt;
		Vector Delta1 = EndPnt-MidP;

		Vector L0 = StartPnt+t*Delta0;
		Vector L1 = MidP+t*Delta1;

		Vector Pnt = L0+(L1-L0)*t;

		Pnt+=randpos;
		// Optional Culling based on configurable trace distance.  Failing particle are destroyed
		//disabled for now.
		//if ( m_flTraceDist != 0.0f )
		//{
		//	// Trace down
		//	Vector TraceDir=Vector(0, 0, -1);
		//	// now set the trace distance
		//	// note - probably need to offset Pnt upwards for some fudge factor on irregular surfaces
		//	CBaseTrace tr;
		//	Vector RayStart=Pnt;
		//	float flRadius = m_flTraceDist;
		//	g_pParticleSystemMgr->Query()->TraceLine( RayStart, ( RayStart + ( TraceDir * flRadius ) ), MASK_SOLID, NULL, COLLISION_GROUP_NONE, &tr );
		//	if ( tr.fraction == 1.0 )
		//	{
		//		//If the trace hit nothing, kill the particle.
		//		pParticles->KillParticle( start_p );
		//	}
		//	else
		//	{
		//		//If we hit something, set particle position to collision position
		//		Pnt += tr.endpos;
		//		//FIXME - if we add a concept of a particle normal (for example, aligned quads or decals, set it here)
		//	}
		//}

		xyz[0] = Pnt.x;
		xyz[4] = Pnt.y;
		xyz[8] = Pnt.z;
		if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
		{
			pxyz[0] = Pnt.x;
			pxyz[4] = Pnt.y;
			pxyz[8] = Pnt.z;
		}
	}
}



//-----------------------------------------------------------------------------
// Remap initial Scalar to Vector Initializer
//-----------------------------------------------------------------------------
class C_INIT_RemapScalarToVector : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RemapScalarToVector );

	uint32 GetWrittenAttributes( void ) const
	{
		return 1 << m_nFieldOutput | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 1 << m_nFieldInput;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	bool InitMultipleOverride ( void ) { return true; }

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	int		m_nFieldInput;
	int		m_nFieldOutput;
	float	m_flInputMin;
	float	m_flInputMax;
	Vector	m_vecOutputMin;
	Vector	m_vecOutputMax;
	float	m_flStartTime;
	float	m_flEndTime;
	bool	m_bScaleInitialRange;
	int		m_nControlPointNumber;
	bool	m_bLocalCoords;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RemapScalarToVector, "Remap Scalar to Vector", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapScalarToVector )
DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
DMXELEMENT_UNPACK_FIELD_USERDATA( "input field", "8", int, m_nFieldInput, "intchoice particlefield_scalar" )
DMXELEMENT_UNPACK_FIELD( "input minimum","0", float, m_flInputMin )
DMXELEMENT_UNPACK_FIELD( "input maximum","1", float, m_flInputMax )
DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
DMXELEMENT_UNPACK_FIELD( "output minimum","0 0 0", Vector, m_vecOutputMin )
DMXELEMENT_UNPACK_FIELD( "output maximum","1 1 1", Vector, m_vecOutputMax )
DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
DMXELEMENT_UNPACK_FIELD( "use local system", "1", bool, m_bLocalCoords )
DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapScalarToVector )

void C_INIT_RemapScalarToVector::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	const float *pCreationTime;
	// FIXME: SSE-ize
	for( ; nParticleCount--; start_p++ )
	{
		pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		// using raw creation time to map to emitter lifespan
		float flLifeTime = *pCreationTime;  

		// only use within start/end time frame
		if ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
			continue;

		const float *pInput = pParticles->GetFloatAttributePtr( m_nFieldInput, start_p );
		float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
		Vector vecOutput = vec3_origin;
		vecOutput.x = RemapValClamped( *pInput, m_flInputMin, m_flInputMax, m_vecOutputMin.x, m_vecOutputMax.x  );
		vecOutput.y = RemapValClamped( *pInput, m_flInputMin, m_flInputMax, m_vecOutputMin.y, m_vecOutputMax.y  );
		vecOutput.z = RemapValClamped( *pInput, m_flInputMin, m_flInputMax, m_vecOutputMin.z, m_vecOutputMax.z  );


		if ( m_nFieldOutput == 0 )
		{
			float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
			if ( !m_bLocalCoords )
			{
				Vector vecControlPoint;
				pParticles->GetControlPointAtTime( m_nControlPointNumber, *pCreationTime, &vecControlPoint );
				vecOutput += vecControlPoint;
				Vector vecOutputPrev = vecOutput;
				if ( m_bScaleInitialRange )
				{
					Vector vecScaleInitial;
					Vector vecScaleInitialPrev;
					SetVectorFromAttribute ( vecScaleInitial, pOutput );
					SetVectorFromAttribute ( vecScaleInitialPrev, pxyz );
					vecOutput *= vecScaleInitial;
					vecOutputPrev *= vecScaleInitialPrev;
				}
				SetVectorAttribute( pOutput, vecOutput );
				SetVectorAttribute( pxyz, vecOutputPrev ); 
			}
			else
			{
				matrix3x4_t mat;
				pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *pCreationTime, &mat );
				Vector vecTransformLocal = vec3_origin;
				VectorTransform( vecOutput, mat, vecTransformLocal );
				vecOutput = vecTransformLocal;
				Vector vecOutputPrev = vecOutput;
				if ( m_bScaleInitialRange )
				{
					Vector vecScaleInitial;
					Vector vecScaleInitialPrev;
					SetVectorFromAttribute ( vecScaleInitial, pOutput );
					SetVectorFromAttribute ( vecScaleInitialPrev, pxyz );
					vecOutput *= vecScaleInitial;
					vecOutputPrev *= vecScaleInitialPrev;
				}
				SetVectorAttribute( pOutput, vecOutput );
				SetVectorAttribute( pxyz, vecOutput ); 
			}
		}
		else
		{
			if ( m_bScaleInitialRange )
			{
				Vector vecScaleInitial;
				SetVectorFromAttribute ( vecScaleInitial, pOutput );
				vecOutput *= vecScaleInitial;
			}
			SetVectorAttribute( pOutput, vecOutput ); 
		}
	}
}


//-----------------------------------------------------------------------------
// Create particles sequentially along a path
//-----------------------------------------------------------------------------
struct SequentialPathContext_t
{
	int		m_nParticleCount;
	float	m_flStep;
	int		m_nCountAmount;
};
class C_INIT_CreateSequentialPath : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateSequentialPath );

	float m_fMaxDistance;
	float m_flNumToAssign;
	bool m_bLoop;
	struct CPathParameters m_PathParams;

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		uint64 nStartMask = ( 1ULL << m_PathParams.m_nStartControlPointNumber ) - 1;
		uint64 nEndMask = ( 1ULL << ( m_PathParams.m_nEndControlPointNumber + 1 ) ) - 1;
		return nEndMask & (~nStartMask);
	}

	virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
	{
		SequentialPathContext_t *pCtx = reinterpret_cast<SequentialPathContext_t *>( pContext );
		pCtx->m_nParticleCount = 0;
		if ( m_flNumToAssign > 1.0f )
		{
			pCtx->m_flStep = 1.0f / ( m_flNumToAssign - 1 );
		}
		else
		{
			pCtx->m_flStep = 0.0f;
		}
		pCtx->m_nCountAmount = 1;
	}

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_PathParams.ClampControlPointIndices();


	}

	size_t GetRequiredContextBytes( void ) const
	{
		return sizeof( SequentialPathContext_t );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateSequentialPath, "Position Along Path Sequential", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateSequentialPath ) 
DMXELEMENT_UNPACK_FIELD( "maximum distance", "0", float, m_fMaxDistance )
DMXELEMENT_UNPACK_FIELD( "bulge", "0", float, m_PathParams.m_flBulge )
DMXELEMENT_UNPACK_FIELD( "start control point number", "0", int, m_PathParams.m_nStartControlPointNumber )
DMXELEMENT_UNPACK_FIELD( "end control point number", "0", int, m_PathParams.m_nEndControlPointNumber )
DMXELEMENT_UNPACK_FIELD( "bulge control 0=random 1=orientation of start pnt 2=orientation of end point", "0", int, m_PathParams.m_nBulgeControl )
DMXELEMENT_UNPACK_FIELD( "mid point position", "0.5", float, m_PathParams.m_flMidPoint )
DMXELEMENT_UNPACK_FIELD( "particles to map from start to end", "100", float, m_flNumToAssign )
DMXELEMENT_UNPACK_FIELD( "restart behavior (0 = bounce, 1 = loop )", "1", bool, m_bLoop )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateSequentialPath )


void C_INIT_CreateSequentialPath::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	// NOTE: Using C_OP_ContinuousEmitter:: avoids a virtual function call
	SequentialPathContext_t *pCtx = reinterpret_cast<SequentialPathContext_t *>( pContext );

	for( ; nParticleCount--; start_p++ )
	{
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );

		Vector StartPnt, MidP, EndPnt;
		pParticles->CalculatePathValues( m_PathParams, *ct, &StartPnt, &MidP, &EndPnt);
		if ( pCtx->m_nParticleCount >= m_flNumToAssign || pCtx->m_nParticleCount < 0 )
		{
			if ( m_bLoop )
			{
				pCtx->m_nParticleCount = 0;
			}
			else
			{
				pCtx->m_nCountAmount *= -1;
				pCtx->m_nParticleCount = min ( pCtx->m_nParticleCount, (int)( m_flNumToAssign - 1) );
				pCtx->m_nParticleCount = max ( pCtx->m_nParticleCount, 1 );
			}
		}

		float t= pCtx->m_nParticleCount * pCtx->m_flStep;

		Vector randpos;
		pParticles->RandomVector( -m_fMaxDistance, m_fMaxDistance, &randpos );

		// form delta terms needed for quadratic bezier
		Vector Delta0=MidP-StartPnt;
		Vector Delta1 = EndPnt-MidP;

		Vector L0 = StartPnt+t*Delta0;
		Vector L1 = MidP+t*Delta1;

		Vector Pnt = L0+(L1-L0)*t;

		Pnt+=randpos;

		xyz[0] = Pnt.x;
		xyz[4] = Pnt.y;
		xyz[8] = Pnt.z;
		if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
		{
			pxyz[0] = Pnt.x;
			pxyz[4] = Pnt.y;
			pxyz[8] = Pnt.z;
		}
		pCtx->m_nParticleCount += pCtx->m_nCountAmount;
	}
}


//-----------------------------------------------------------------------------
//   Initial Repulsion Velocity - repulses the particles from nearby surfaces 
//	 on spawn
//-----------------------------------------------------------------------------
class C_INIT_InitialRepulsionVelocity : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_InitialRepulsionVelocity );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_RADIUS_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nControlPointNumber;
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;	

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nCollisionGroupNumber = g_pParticleSystemMgr->Query()->GetCollisionGroupFromName( m_CollisionGroupName );
		m_nControlPointNumber = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
	}

	bool InitMultipleOverride ( void ) { return true; }

	char m_CollisionGroupName[128];
	int m_nCollisionGroupNumber;
	Vector	m_vecOutputMin;
	Vector	m_vecOutputMax;
	int		nRemainingBlocks;
	int		m_nControlPointNumber;
	bool	m_bPerParticle;
	bool	m_bTranslate;
	bool	m_bProportional;
	float	m_flTraceLength;
	bool	m_bPerParticleTR;
	bool	m_bInherit;
	int		m_nChildCP;
	int		m_nChildGroupID;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_InitialRepulsionVelocity, "Velocity Repulse from World", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_InitialRepulsionVelocity )
DMXELEMENT_UNPACK_FIELD( "minimum velocity","0 0 0", Vector, m_vecOutputMin )
DMXELEMENT_UNPACK_FIELD( "maximum velocity","1 1 1", Vector, m_vecOutputMax )
DMXELEMENT_UNPACK_FIELD_STRING( "collision group", "NONE", m_CollisionGroupName )
DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
DMXELEMENT_UNPACK_FIELD( "Per Particle World Collision Tests", "0", bool, m_bPerParticle )
DMXELEMENT_UNPACK_FIELD( "Use radius for Per Particle Trace Length", "0", bool, m_bPerParticleTR )
DMXELEMENT_UNPACK_FIELD( "Offset instead of accelerate", "0", bool, m_bTranslate )
DMXELEMENT_UNPACK_FIELD( "Offset proportional to radius 0/1", "0", bool, m_bProportional )
DMXELEMENT_UNPACK_FIELD( "Trace Length", "64.0", float, m_flTraceLength )
DMXELEMENT_UNPACK_FIELD( "Inherit from Parent", "0", bool, m_bInherit )
DMXELEMENT_UNPACK_FIELD( "control points to broadcast to children (n + 1)", "-1", int, m_nChildCP )
DMXELEMENT_UNPACK_FIELD( "Child Group ID to affect", "0", int, m_nChildGroupID )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_InitialRepulsionVelocity );


void C_INIT_InitialRepulsionVelocity::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{

	Vector	d[6];

	//All cardinal directions
	d[0] = Vector(  1,  0,  0 );
	d[1] = Vector( -1,  0,  0 );
	d[2] = Vector(  0,  1,  0 );
	d[3] = Vector(  0, -1,  0 );
	d[4] = Vector(  0,  0,  1 );
	d[5] = Vector(  0,  0, -1 );

	//Init the results
	Vector resultDirection;
	float resultForce;
	if ( m_bPerParticle )
	{
		for( ; nParticleCount--; start_p++ )
		{	

			float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
			float *pxyz_prev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
			const float *radius = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_RADIUS, start_p );
			Vector vecCurrentPos;
			SetVectorFromAttribute( vecCurrentPos, pxyz );

			resultDirection.Init();
			resultForce = 0.0f;

			//Get the aggregate force vector
			for ( int i = 0; i < 6; i++ )
			{
				//Press out
				float flTraceDistance = m_flTraceLength;
				if ( m_bPerParticleTR )
				{
					flTraceDistance = *radius;
				}
				Vector endpos = vecCurrentPos + ( d[i] * flTraceDistance );

				//Trace into the world
				CBaseTrace tr;
				g_pParticleSystemMgr->Query()->TraceLine( vecCurrentPos, endpos, CONTENTS_SOLID, NULL, m_nCollisionGroupNumber, &tr );

				//Push back a proportional amount to the probe
				d[i] = -d[i] * (1.0f-tr.fraction);

				assert(( 1.0f - tr.fraction ) >= 0.0f );

				resultForce += 1.0f-tr.fraction;
				resultDirection += d[i];
			}

			//If we've hit nothing, then point up
			if ( resultDirection == vec3_origin )
			{
				resultDirection = Vector( 0, 0, 1 );
				resultForce = 0.0f;
			}

			//Just return the direction
			VectorNormalize( resultDirection );
			resultDirection *= resultForce;

			Vector vecRepulsionAmount;

			vecRepulsionAmount.x = Lerp( resultForce, m_vecOutputMin.x, m_vecOutputMax.x );
			vecRepulsionAmount.y = Lerp( resultForce, m_vecOutputMin.y, m_vecOutputMax.y );
			vecRepulsionAmount.z = Lerp( resultForce, m_vecOutputMin.z, m_vecOutputMax.z );


			vecRepulsionAmount *= resultDirection;


			if ( m_bProportional )
			{
				vecRepulsionAmount *= *radius;
			}

			pxyz[0] += vecRepulsionAmount.x;
			pxyz[4] += vecRepulsionAmount.y;
			pxyz[8] += vecRepulsionAmount.z;

			if ( m_bTranslate )
			{
				pxyz_prev[0] += vecRepulsionAmount.x;
				pxyz_prev[4] += vecRepulsionAmount.y;
				pxyz_prev[8] += vecRepulsionAmount.z;
			}
		}
	}
	else
	{
		
		Vector vecRepulsionAmount;

		if ( m_bInherit )
		{
			float *ct = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
			pParticles->GetControlPointAtTime( m_nControlPointNumber, *ct, &resultDirection );
			Vector vecPassedForce;
			pParticles->GetControlPointAtTime( m_nControlPointNumber+1, *ct, &vecPassedForce );

			vecRepulsionAmount.x = Lerp( vecPassedForce.x, m_vecOutputMin.x, m_vecOutputMax.x );
			vecRepulsionAmount.y = Lerp( vecPassedForce.x, m_vecOutputMin.y, m_vecOutputMax.y );
			vecRepulsionAmount.z = Lerp( vecPassedForce.x, m_vecOutputMin.z, m_vecOutputMax.z );

			vecRepulsionAmount *= resultDirection;
		}
		else
		{
			float *ct = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
			Vector vecControlPoint;
			pParticles->GetControlPointAtTime( m_nControlPointNumber, *ct, &vecControlPoint );

			Vector vecCurrentPos = vecControlPoint;

			resultDirection.Init();
			resultForce = 0.0f;

			//Get the aggregate force vector
			for ( int i = 0; i < 6; i++ )
			{
				//Press out
				Vector endpos = vecCurrentPos + ( d[i] * m_flTraceLength );

				//Trace into the world
				CBaseTrace tr;
				g_pParticleSystemMgr->Query()->TraceLine( vecCurrentPos, endpos, CONTENTS_SOLID, NULL, m_nCollisionGroupNumber, &tr );

				//Push back a proportional amount to the probe
				d[i] = -d[i] * (1.0f-tr.fraction);

				assert(( 1.0f - tr.fraction ) >= 0.0f );

				resultForce += 1.0f-tr.fraction;
				resultDirection += d[i];
			}

			//If we've hit nothing, then point up
			if ( resultDirection == vec3_origin )
			{
				resultDirection = Vector( 0, 0, 1 );
				resultForce = 0.0f;
			}

			//Just return the direction
			VectorNormalize( resultDirection );
			resultDirection *= resultForce;

			vecRepulsionAmount.x = Lerp( resultForce, m_vecOutputMin.x, m_vecOutputMax.x );
			vecRepulsionAmount.y = Lerp( resultForce, m_vecOutputMin.y, m_vecOutputMax.y );
			vecRepulsionAmount.z = Lerp( resultForce, m_vecOutputMin.z, m_vecOutputMax.z );

			vecRepulsionAmount *= resultDirection;

			if ( m_nChildCP != -1 )
			{
				for( CParticleCollection *pChild = pParticles->m_Children.m_pHead; pChild; pChild = pChild->m_pNext )
				{
					if ( pChild->GetGroupID() == m_nChildGroupID )
					{
						Vector vecPassForce = Vector(resultForce, 0, 0);
						pChild->SetControlPoint( m_nChildCP, resultDirection );
						pChild->SetControlPoint( m_nChildCP+1, vecPassForce );
					}
				}
			}
		}
		
		for( ; nParticleCount--; start_p++ )
		{	

			float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
			float *pxyz_prev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
			const float *radius = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_RADIUS, start_p );

			if ( m_bProportional )
			{
				vecRepulsionAmount *= *radius;
			}

			pxyz[0] += vecRepulsionAmount.x;
			pxyz[4] += vecRepulsionAmount.y;
			pxyz[8] += vecRepulsionAmount.z;

			if ( m_bTranslate )
			{
				pxyz_prev[0] += vecRepulsionAmount.x;
				pxyz_prev[4] += vecRepulsionAmount.y;
				pxyz_prev[8] += vecRepulsionAmount.z;
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Random Yaw Flip
//-----------------------------------------------------------------------------
class C_INIT_RandomYawFlip : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomYawFlip );
	
	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_YAW_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	bool InitMultipleOverride ( void ) { return true; }

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask, void *pContext ) const;

	float m_flPercent;

};

DEFINE_PARTICLE_OPERATOR( C_INIT_RandomYawFlip, "Rotation Yaw Flip Random", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomYawFlip ) 
DMXELEMENT_UNPACK_FIELD( "Flip Percentage", ".5", float, m_flPercent )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomYawFlip )

void C_INIT_RandomYawFlip::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	for( ; nParticleCount--; start_p++ )
	{
		float flChance = pParticles->RandomFloat( 0.0, 1.0 );
		if ( flChance < m_flPercent )
		{
			float flRadians = 180 * ( M_PI / 180.0f );
			float *drot = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_YAW, start_p );
			*drot += flRadians;
		}
	}
}



//-----------------------------------------------------------------------------
// Random second sequence
//-----------------------------------------------------------------------------
class C_INIT_RandomSecondSequence : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RandomSecondSequence );
 
	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER1_MASK;
	}
 
	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}
 
	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		// TODO: Validate the ranges here!
	}

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
	{
		InitScalarAttributeRandomRangeBlock( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER1,
			m_nSequenceMin, m_nSequenceMax,
			pParticles, start_block, n_blocks );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
	{
		float *pSequence;
		for( ; nParticleCount--; start_p++ )
		{
			pSequence = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER1, start_p );
			*pSequence = pParticles->RandomInt( m_nSequenceMin, m_nSequenceMax );
		}
	}
 
	int m_nSequenceMin;
	int m_nSequenceMax;
};
 
DEFINE_PARTICLE_OPERATOR( C_INIT_RandomSecondSequence, "Sequence Two Random", OPERATOR_GENERIC );
 
BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomSecondSequence ) 
	DMXELEMENT_UNPACK_FIELD( "sequence_min", "0", int, m_nSequenceMin )
	DMXELEMENT_UNPACK_FIELD( "sequence_max", "0", int, m_nSequenceMax )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomSecondSequence )



//-----------------------------------------------------------------------------
// Remap CP to Scalar Initializer
//-----------------------------------------------------------------------------
class C_INIT_RemapCPtoScalar : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RemapCPtoScalar );

	uint32 GetWrittenAttributes( void ) const
	{
		return 1 << m_nFieldOutput;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nCPInput;
	}

	bool InitMultipleOverride ( void ) { return true; }

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nField = int (clamp (m_nField, 0, 2));
	}

	int		m_nCPInput;                                                             
	int		m_nFieldOutput;
	int		m_nField;
	float	m_flInputMin;
	float	m_flInputMax;
	float	m_flOutputMin;
	float	m_flOutputMax;
	float	m_flStartTime;
	float	m_flEndTime;
	bool	m_bScaleInitialRange;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RemapCPtoScalar, "Remap Control Point to Scalar", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapCPtoScalar )
DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
DMXELEMENT_UNPACK_FIELD( "input control point number", "0", int, m_nCPInput )
DMXELEMENT_UNPACK_FIELD( "input minimum","0", float, m_flInputMin )
DMXELEMENT_UNPACK_FIELD( "input maximum","1", float, m_flInputMax )
DMXELEMENT_UNPACK_FIELD( "input field 0-2 X/Y/Z","0", int, m_nField )
DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapCPtoScalar )

void C_INIT_RemapCPtoScalar::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	const float *pCreationTime;
	// clamp the result to 0 and 1 if it's alpha
	float flMin=m_flOutputMin;
	float flMax=m_flOutputMax;
	if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
	{
		flMin = clamp(m_flOutputMin, 0.0f, 1.0f );
		flMax = clamp(m_flOutputMax, 0.0f, 1.0f );
	}
	Vector vecControlPoint;
	float *ct = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
	pParticles->GetControlPointAtTime( m_nCPInput, *ct, &vecControlPoint );

	float flInput = vecControlPoint[m_nField];

	// FIXME: SSE-ize
	for( ; nParticleCount--; start_p++ )
	{
		pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		// using raw creation time to map to emitter lifespan
		float flLifeTime = *pCreationTime;  

		// only use within start/end time frame
		if ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
			continue;


		float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
		float flOutput = RemapValClamped( flInput, m_flInputMin, m_flInputMax, flMin, flMax  );
		if ( m_bScaleInitialRange )
		{
			flOutput = *pOutput * flOutput;
		}
		if ( ATTRIBUTES_WHICH_ARE_INTS & ( 1 << m_nFieldOutput ) )
		{
			*pOutput = int ( flOutput );
		}
		else
		{
			*pOutput = flOutput;
		}
	}
}



//-----------------------------------------------------------------------------
// Remap CP to Vector Initializer
//-----------------------------------------------------------------------------
class C_INIT_RemapCPtoVector : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_RemapCPtoVector );

	uint32 GetWrittenAttributes( void ) const
	{
		return 1 << m_nFieldOutput | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		uint64 nMask = ( 1ULL << m_nCPInput );
		if ( m_nLocalSpaceCP != -1 )
		{
			nMask |= ( 1ULL << m_nLocalSpaceCP );
		}
		return nMask;
	}

	bool InitMultipleOverride ( void ) { return true; }

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nField = int (clamp (m_nField, 0, 2));
	}

	int		m_nCPInput;                                                             
	int		m_nFieldOutput;
	int		m_nField;
	Vector	m_vInputMin;
	Vector	m_vInputMax;
	Vector	m_vOutputMin;
	Vector	m_vOutputMax;
	float	m_flStartTime;
	float	m_flEndTime;
	bool	m_bScaleInitialRange;
	bool	m_bOffset;
	bool	m_bAccelerate;
	int		m_nLocalSpaceCP;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_RemapCPtoVector, "Remap Control Point to Vector", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapCPtoVector )
DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
DMXELEMENT_UNPACK_FIELD( "input control point number", "0", int, m_nCPInput )
DMXELEMENT_UNPACK_FIELD( "input minimum","0 0 0", Vector, m_vInputMin )
DMXELEMENT_UNPACK_FIELD( "input maximum","0 0 0", Vector, m_vInputMax )
DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
DMXELEMENT_UNPACK_FIELD( "output minimum","0 0 0", Vector, m_vOutputMin )
DMXELEMENT_UNPACK_FIELD( "output maximum","0 0 0", Vector, m_vOutputMax )
DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
DMXELEMENT_UNPACK_FIELD( "offset position","0", bool, m_bOffset )
DMXELEMENT_UNPACK_FIELD( "accelerate position","0", bool, m_bAccelerate )
DMXELEMENT_UNPACK_FIELD( "local space CP","-1", int, m_nLocalSpaceCP )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapCPtoVector )

void C_INIT_RemapCPtoVector::InitNewParticlesScalar(
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	Vector vecControlPoint;
	pParticles->GetControlPointAtTime( m_nCPInput, pParticles->m_flCurTime, &vecControlPoint );
	Vector vOutputMinLocal = m_vOutputMin;
	Vector vOutputMaxLocal = m_vOutputMax;
	if ( m_nLocalSpaceCP != -1 )
	{
		matrix3x4_t mat;
		pParticles->GetControlPointTransformAtTime( m_nLocalSpaceCP, pParticles->m_flCurTime, &mat );
		Vector vecTransformLocal = vec3_origin;
		VectorRotate( vOutputMinLocal, mat, vecTransformLocal );
		vOutputMinLocal = vecTransformLocal;
		VectorRotate( vOutputMaxLocal, mat, vecTransformLocal );
		vOutputMaxLocal = vecTransformLocal;
	}

	// FIXME: SSE-ize
	for( ; nParticleCount--; start_p++ )
	{
		const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		// using raw creation time to map to emitter lifespan
		float flLifeTime = *pCreationTime;  

		// only use within start/end time frame
		if ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
			continue;


		float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );

		Vector vOutput;
		vOutput.x = RemapValClamped( vecControlPoint.x, m_vInputMin.x, m_vInputMax.x, vOutputMinLocal.x, vOutputMaxLocal.x );
		vOutput.y = RemapValClamped( vecControlPoint.y, m_vInputMin.y, m_vInputMax.y, vOutputMinLocal.y, vOutputMaxLocal.y );
		vOutput.z = RemapValClamped( vecControlPoint.z, m_vInputMin.z, m_vInputMax.z, vOutputMinLocal.z, vOutputMaxLocal.z );		

		if ( m_bScaleInitialRange )
		{
			Vector vOrgValue;
			SetVectorFromAttribute ( vOrgValue, pOutput );
			vOutput *= vOrgValue;
		}
		if ( m_nFieldOutput == 6 )
		{
			pOutput[0] = max( 0.0f, min( vOutput.x, 1.0f) );
			pOutput[4] = max( 0.0f, min( vOutput.y, 1.0f) );
			pOutput[8] = max( 0.0f, min( vOutput.z, 1.0f) );
		}
		else
		{
			float *pXYZ_Prev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
			Vector vXYZPrev;
			if ( m_bAccelerate )
			{
				if ( m_bOffset )
				{
					Vector vOrgValue;
					SetVectorFromAttribute ( vOrgValue, pOutput );
					SetVectorFromAttribute ( vXYZPrev, pXYZ_Prev );
					vOutput += vOrgValue;
					vXYZPrev += vOutput;
					vOutput += vOutput * pParticles->m_flDt;
					SetVectorAttribute ( pOutput, vOutput );
					SetVectorAttribute ( pXYZ_Prev, vXYZPrev );
				}
				else
				{
					vOutput *= pParticles->m_flDt;
					SetVectorAttribute ( pOutput, vOutput );
				}

			}
			else
			{
				vXYZPrev = vOutput;
				if ( m_bOffset )
				{
					Vector vOrgValue;
					SetVectorFromAttribute ( vOrgValue, pOutput );
					SetVectorFromAttribute ( vXYZPrev, pXYZ_Prev );
					vOutput += vOrgValue;
					vXYZPrev += vOutput;

				}
				SetVectorAttribute ( pOutput, vOutput );
				SetVectorAttribute ( pXYZ_Prev, vXYZPrev );
			}
		}
	}
}



class C_INIT_CreateFromParentParticles : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateFromParentParticles );

	struct ParentParticlesContext_t
	{
		int		m_nCurrentParentParticle;
	};

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
	{
		ParentParticlesContext_t *pCtx = reinterpret_cast<ParentParticlesContext_t *>( pContext );
		pCtx->m_nCurrentParentParticle = 0;
	}

	size_t GetRequiredContextBytes( void ) const
	{
		return sizeof( ParentParticlesContext_t );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	float m_flVelocityScale;
	bool  m_bRandomDistribution;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateFromParentParticles, "Position From Parent Particles", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateFromParentParticles )
DMXELEMENT_UNPACK_FIELD( "Inherited Velocity Scale","0", float, m_flVelocityScale )
DMXELEMENT_UNPACK_FIELD( "Random Parent Particle Distribution","0", bool, m_bRandomDistribution )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateFromParentParticles )

void C_INIT_CreateFromParentParticles::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	if ( !pParticles->m_pParent )
	{
		for( ; nParticleCount--; start_p++ )
		{
			float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
			float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );

			SetVectorAttribute( xyz, vec3_origin );
			SetVectorAttribute( pxyz, vec3_origin );
		}
		return;
	}
	ParentParticlesContext_t *pCtx = reinterpret_cast<ParentParticlesContext_t *>( pContext );
	int nActiveParticles = pParticles->m_pParent->m_nActiveParticles;


	if ( nActiveParticles == 0 )
	{
		while( nParticleCount-- )
		{
			float *lifespan = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
			*lifespan = 0.0f;
			start_p++;
		}
		return;
	}		

	nActiveParticles = max ( 0, nActiveParticles - 1 );

	for( ; nParticleCount--; start_p++ )
	{
		if ( m_bRandomDistribution )
		{
			pCtx->m_nCurrentParentParticle = pParticles->RandomInt( 0, nActiveParticles );
		}
		else if ( pCtx->m_nCurrentParentParticle > nActiveParticles )
		{
			pCtx->m_nCurrentParentParticle = 0;
		}
		float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
		const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
		const float *pParent_xyz = pParticles->m_pParent->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, pCtx->m_nCurrentParentParticle );
		const float *pParent_pxyz = pParticles->m_pParent->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_PREV_XYZ, pCtx->m_nCurrentParentParticle );

		Vector vecParentXYZ;
		Vector vecParentPrevXYZ;
		Vector vecScaledXYZ;

		float flPrevTime = pParticles->m_flCurTime - pParticles->m_flDt;
		float flSubFrame = RemapValClamped( *ct, flPrevTime, pParticles->m_flCurTime, 0, 1 );
		

		vecParentXYZ.x = pParent_xyz[0];
		vecParentXYZ.y = pParent_xyz[4];
		vecParentXYZ.z = pParent_xyz[8];
		vecParentPrevXYZ.x = pParent_pxyz[0];
		vecParentPrevXYZ.y = pParent_pxyz[4];
		vecParentPrevXYZ.z = pParent_pxyz[8];

		VectorLerp( vecParentPrevXYZ, vecParentXYZ, flSubFrame, vecParentXYZ );
		VectorLerp( vecParentXYZ, vecParentPrevXYZ, m_flVelocityScale, vecScaledXYZ );
		SetVectorAttribute( pxyz, vecScaledXYZ );
		SetVectorAttribute( xyz, vecParentXYZ );

		pCtx->m_nCurrentParentParticle++;
	}
}




//-----------------------------------------------------------------------------
// Distance to CP Initializer
//-----------------------------------------------------------------------------
class C_INIT_DistanceToCPInit : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_DistanceToCPInit );

	uint32 GetWrittenAttributes( void ) const
	{
		return 1 << m_nFieldOutput;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK;
	}

	virtual uint64 GetReadControlPointMask() const
	{
		return 1ULL << m_nStartCP;
	}

	bool InitMultipleOverride ( void ) { return true; }

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nCollisionGroupNumber = g_pParticleSystemMgr->Query()->GetCollisionGroupFromName( m_CollisionGroupName );
		m_nStartCP = max( 0, min( MAX_PARTICLE_CONTROL_POINTS-1, m_nStartCP ) );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	int		m_nFieldOutput;
	float	m_flInputMin;
	float	m_flInputMax;
	float	m_flOutputMin;
	float	m_flOutputMax;
	int		m_nStartCP;
	bool	m_bLOS;
	char	m_CollisionGroupName[128];
	int		m_nCollisionGroupNumber;
	float	m_flMaxTraceLength;
	float	m_flLOSScale;
	bool	m_bScaleInitialRange;
	bool	m_bActiveRange;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_DistanceToCPInit, "Remap Initial Distance to Control Point to Scalar", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_DistanceToCPInit )
DMXELEMENT_UNPACK_FIELD( "distance minimum","0", float, m_flInputMin )
DMXELEMENT_UNPACK_FIELD( "distance maximum","128", float, m_flInputMax )
DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
DMXELEMENT_UNPACK_FIELD( "control point","0", int, m_nStartCP )
DMXELEMENT_UNPACK_FIELD( "ensure line of sight","0", bool, m_bLOS )
DMXELEMENT_UNPACK_FIELD_STRING( "LOS collision group", "NONE", m_CollisionGroupName )
DMXELEMENT_UNPACK_FIELD( "Maximum Trace Length", "-1", float, m_flMaxTraceLength )
DMXELEMENT_UNPACK_FIELD( "LOS Failure Scalar", "0", float, m_flLOSScale )
DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
DMXELEMENT_UNPACK_FIELD( "only active within specified distance","0", bool, m_bActiveRange )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_DistanceToCPInit )

void C_INIT_DistanceToCPInit::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	// clamp the result to 0 and 1 if it's alpha
	float flMin=m_flOutputMin;
	float flMax=m_flOutputMax;
	if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
	{
		flMin = clamp(m_flOutputMin, 0.0f, 1.0f );
		flMax = clamp(m_flOutputMax, 0.0f, 1.0f );
	}
	Vector vecControlPoint1 = pParticles->GetControlPointAtCurrentTime( m_nStartCP );

	// FIXME: SSE-ize
	for( ; nParticleCount--; start_p++ )
	{
		Vector vecPosition2;
		const float *pXYZ = pParticles->GetFloatAttributePtr(PARTICLE_ATTRIBUTE_XYZ, start_p );
		vecPosition2 = Vector(pXYZ[0], pXYZ[4], pXYZ[8]); 
		Vector vecDelta = vecControlPoint1 - vecPosition2;
		float flDistance = vecDelta.Length();
		if ( m_bActiveRange && ( flDistance < m_flInputMin || flDistance > m_flInputMax ) )
		{
			continue;
		}
		if ( m_bLOS )
		{
			Vector vecEndPoint = vecPosition2;
			if ( m_flMaxTraceLength != -1.0f && m_flMaxTraceLength < flDistance )
			{
				VectorNormalize(vecEndPoint);
				vecEndPoint *= m_flMaxTraceLength;
				vecEndPoint += vecControlPoint1;
			}
			CBaseTrace tr;
			g_pParticleSystemMgr->Query()->TraceLine( vecControlPoint1, vecEndPoint, MASK_OPAQUE_AND_NPCS, NULL , m_nCollisionGroupNumber, &tr );
			if (tr.fraction != 1.0f)
			{
				flDistance *= tr.fraction * m_flLOSScale;
			}

		}

		float flOutput = RemapValClamped( flDistance, m_flInputMin, m_flInputMax, flMin, flMax  );
		if ( m_bScaleInitialRange )
		{
			const float *pInitialOutput = pParticles->GetFloatAttributePtr( m_nFieldOutput, start_p );
			flOutput = *pInitialOutput * flOutput;
		}
		float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );

		*pOutput = flOutput;
	}
}




class C_INIT_LifespanFromVelocity : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_LifespanFromVelocity );

	Vector m_vecComponentScale;
	float m_flTraceOffset;
	float m_flMaxTraceLength;
	float m_flTraceTolerance;
	int m_nCollisionGroupNumber;
	int m_nMaxPlanes;
	int m_nAllowedPlanes;
	char	m_CollisionGroupName[128];
	

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
	}

	void InitializeContextData( CParticleCollection *pParticles,
		void *pContext ) const
	{
	}

	size_t GetRequiredContextBytes( ) const
	{
		return sizeof( CWorldCollideContextData );
	}

	bool InitMultipleOverride ( void ) { return true; }

	void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
	{
		m_nCollisionGroupNumber = g_pParticleSystemMgr->Query()->GetCollisionGroupFromName( m_CollisionGroupName );
		m_nAllowedPlanes = ( min ( MAX_WORLD_PLANAR_CONSTRAINTS, m_nMaxPlanes ) - 1 );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;

	virtual void InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const;

};

DEFINE_PARTICLE_OPERATOR( C_INIT_LifespanFromVelocity, "Lifetime from Time to Impact", OPERATOR_GENERIC );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_LifespanFromVelocity )
DMXELEMENT_UNPACK_FIELD_STRING( "trace collision group", "NONE", m_CollisionGroupName )
DMXELEMENT_UNPACK_FIELD( "maximum trace length", "1024", float, m_flMaxTraceLength )
DMXELEMENT_UNPACK_FIELD( "trace offset", "0", float, m_flTraceOffset )
DMXELEMENT_UNPACK_FIELD( "trace recycle tolerance", "64", float, m_flTraceTolerance )
DMXELEMENT_UNPACK_FIELD( "maximum points to cache", "16", int, m_nMaxPlanes )
DMXELEMENT_UNPACK_FIELD( "bias distance", "1 1 1", Vector, m_vecComponentScale )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_LifespanFromVelocity )


void C_INIT_LifespanFromVelocity::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	CWorldCollideContextData **ppCtx;
	if ( pParticles->m_pParent )
		ppCtx = &( pParticles->m_pParent->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
	else
		ppCtx = &( pParticles->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );

	CWorldCollideContextData *pCtx = NULL;
	if ( ! *ppCtx )
	{
		*ppCtx = new CWorldCollideContextData;
		(*ppCtx)->m_nActivePlanes = 0;
		(*ppCtx)->m_nActivePlanes = 0;
		(*ppCtx)->m_nNumFixedPlanes = 0;
	}
	pCtx = *ppCtx;

	float flTol = m_flTraceTolerance * m_flTraceTolerance;

	//Trace length takes the max trace and subtracts the offset to get the actual total.
	float flTotalTraceDist = m_flMaxTraceLength - m_flTraceOffset;

	//Offset percentage to account for if we've hit something within the offset (but not spawn) area
	float flOffsetPct = m_flMaxTraceLength / ( flTotalTraceDist + FLT_EPSILON );

	FourVectors v4ComponentScale;
	v4ComponentScale.DuplicateVector( m_vecComponentScale );
	while( nParticleCount-- )
	{
		float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
		float *pPrevXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );

		float *dtime = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );


		Vector vecXYZ( pxyz[0], pxyz[4], pxyz[8] );
		Vector vecXYZ_Prev( pPrevXYZ[0], pPrevXYZ[4], pPrevXYZ[8] );

		//Calculate velocity and account for frame delta time
		Vector vDelta = vecXYZ - vecXYZ_Prev;
		float flVelocity = VectorLength( vDelta );
		flVelocity /= pParticles->m_flPreviousDt;
		
		fltx4 fl4TraceOffset = ReplicateX4( m_flTraceOffset );

		//Normalize the delta and get the offset to use from the normalized delta times the offset
		VectorNormalize( vDelta );
		Vector vecOffset = vDelta * m_flTraceOffset;

		Vector vecStartPnt = vecXYZ + vecOffset;
		Vector vecEndPnt = ( vDelta * flTotalTraceDist ) + vecStartPnt;

		// Use SIMD section to interface with plane cache, even though we're not SIMD here
		// Test versus existing Data
		FourVectors fvStartPnt;
		fvStartPnt.DuplicateVector( vecStartPnt );
		FourVectors fvEndPnt;
		fvEndPnt.DuplicateVector( vecEndPnt );
		FourVectors v4PointOnPlane;
		FourVectors v4PlaneNormal;
		FourVectors v4Delta;
		fltx4 fl4ClosestDist = Four_FLT_MAX;
		for( int i = 0 ; i < pCtx->m_nActivePlanes; i++ )
		{
			if ( pCtx->m_bPlaneActive[i] )
			{
				fltx4 fl4TrialDistance = MaxSIMD( 
					fvStartPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ),
					fvEndPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ) );
				// If the trial distance is closer than the existing closest, replace.
				if ( !IsAllGreaterThan( fl4TrialDistance, fl4ClosestDist ) )
				{
					fl4ClosestDist = fl4TrialDistance;
					v4PointOnPlane = pCtx->m_PointOnPlane[i];
				}
			}
		}
		fl4ClosestDist = fabs( fl4ClosestDist );
		// If we're outside the tolerance range, do a new trace and store it.
		if ( IsAllGreaterThan( fl4ClosestDist, ReplicateX4( flTol ) ) )
		{
			//replace this with fast raycaster when available
			CBaseTrace tr;
			tr.plane.normal = vec3_invalid;
			g_pParticleSystemMgr->Query()->TraceLine( vecStartPnt, vecEndPnt, CONTENTS_SOLID, NULL , m_nCollisionGroupNumber, &tr );

			//Set the lifespan to 0 if we start solid, our trace distance is 0, or we hit within the offset area
			if ( ( tr.fraction < ( 1 - flOffsetPct ) ) || tr.startsolid || flTotalTraceDist == 0.0f )
			{
				*dtime = 0.0f;
				fl4TraceOffset = ReplicateX4( 0.0f );
				fvStartPnt.DuplicateVector( vec3_origin );
				v4PointOnPlane.DuplicateVector( vec3_origin );
			}
			else
			{
				int nIndex = pCtx->m_nNumFixedPlanes;
				Vector vPointOnPlane =  vecStartPnt + ( tr.fraction * ( vecEndPnt - vecStartPnt ) ) ;
				pCtx->m_bPlaneActive[nIndex] = true;
				pCtx->m_PointOnPlane[nIndex].DuplicateVector( vPointOnPlane );
				pCtx->m_PlaneNormal[nIndex].DuplicateVector( tr.plane.normal );
				pCtx->m_TraceStartPnt[nIndex].DuplicateVector( vecStartPnt );
				pCtx->m_TraceEndPnt[nIndex].DuplicateVector( vecEndPnt );

				fvStartPnt.DuplicateVector( vecStartPnt );
				v4PointOnPlane.DuplicateVector( vPointOnPlane );

				pCtx->m_nNumFixedPlanes = pCtx->m_nNumFixedPlanes + 1;
				if ( pCtx->m_nNumFixedPlanes > m_nAllowedPlanes )
					pCtx->m_nNumFixedPlanes = 0;
				pCtx->m_nActivePlanes = min( m_nAllowedPlanes, pCtx->m_nActivePlanes + 1 );
			}
		}

		fvStartPnt -= v4PointOnPlane;
		//Scale components to remove undesired axis
		fvStartPnt *= v4ComponentScale;
		//Find the length of the trace
		//Need to use the adjusted value of the trace length and collision point to account for the offset
		fltx4 fl4Dist = AddSIMD ( fvStartPnt.length(), fl4TraceOffset );
		flVelocity += FLT_EPSILON;
		//Divide by Velocity to get Lifespan
		*dtime = SubFloat( fl4Dist, 0) / flVelocity;

	}
}


void C_INIT_LifespanFromVelocity::InitNewParticlesBlock( CParticleCollection *pParticles, 
		int start_block, int n_blocks, int nAttributeWriteMask,
		void *pContext ) const
{
		CWorldCollideContextData **ppCtx;
		if ( pParticles->m_pParent )
			ppCtx = &( pParticles->m_pParent->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
		else
			ppCtx = &( pParticles->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );

		CWorldCollideContextData *pCtx = NULL;
		if ( ! *ppCtx )
		{
			*ppCtx = new CWorldCollideContextData;
			(*ppCtx)->m_nActivePlanes = 0;
			(*ppCtx)->m_nActivePlanes = 0;
			(*ppCtx)->m_nNumFixedPlanes = 0;
		}
		pCtx = *ppCtx;

		float flTol = m_flTraceTolerance * m_flTraceTolerance;

		size_t attr_stride;

		FourVectors *pXYZ = pParticles->Get4VAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
		pXYZ += attr_stride * start_block;
		FourVectors *pPrev_XYZ = pParticles->Get4VAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, &attr_stride );
		pPrev_XYZ += attr_stride * start_block;
		fltx4 *pLifespan = pParticles->GetM128AttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, &attr_stride );
		pLifespan += attr_stride * start_block;

		//Trace length takes the max trace and subtracts the offset to get the actual total.
		float flTotalTraceDist = m_flMaxTraceLength - m_flTraceOffset;
		fltx4 fl4TotalTraceDist = ReplicateX4( flTotalTraceDist );

		//Offset percentage to account for if we've hit something within the offset (but not spawn) area
		float flOffsetPct = m_flMaxTraceLength / ( flTotalTraceDist + FLT_EPSILON );

		fltx4 fl4PrevDT = ReplicateX4( 1.0f / pParticles->m_flPreviousDt );

		FourVectors v4ComponentScale;
		v4ComponentScale.DuplicateVector( m_vecComponentScale );

		while( n_blocks-- )
		{
			// Determine Velocity
			FourVectors fvDelta = *pXYZ;
			fvDelta -= *pPrev_XYZ;
			fltx4 fl4Velocity = fvDelta.length();
			fl4Velocity = MulSIMD ( fl4Velocity, fl4PrevDT );

			fltx4 fl4TraceOffset = ReplicateX4( m_flTraceOffset );

			//Normalize the delta and get the offset to use from the normalized delta times the offset
			FourVectors fvDeltaNormalized = fvDelta;
			fvDeltaNormalized.VectorNormalizeFast();
			FourVectors fvOffset = fvDeltaNormalized;
			fvOffset *= m_flTraceOffset;

			//Start/Endpoints for our traces
			FourVectors fvStartPnt = *pXYZ;
			fvStartPnt += fvOffset;
			FourVectors fvEndPnt = fvDeltaNormalized;
			fvEndPnt *= fl4TotalTraceDist;
			fvEndPnt += fvStartPnt;

			// Test versus existing Data
			FourVectors v4PointOnPlane;
			FourVectors v4PlaneNormal;
			fltx4 fl4ClosestDist = Four_FLT_MAX;
			for( int i = 0 ; i < pCtx->m_nActivePlanes; i++ )
			{
				if ( pCtx->m_bPlaneActive[i] )
				{
					fltx4 fl4TrialDistance = MaxSIMD( 
						fvStartPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ),
						fvEndPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ) );
					fltx4 fl4Nearestmask = CmpLeSIMD( fl4TrialDistance, fl4ClosestDist );
					fl4ClosestDist = MaskedAssign( fl4ClosestDist, fl4TrialDistance, fl4Nearestmask );
					v4PointOnPlane.x = MaskedAssign( fl4Nearestmask, pCtx->m_PointOnPlane[i].x, v4PointOnPlane.x );
					v4PointOnPlane.y = MaskedAssign( fl4Nearestmask, pCtx->m_PointOnPlane[i].y, v4PointOnPlane.y );
					v4PointOnPlane.z = MaskedAssign( fl4Nearestmask, pCtx->m_PointOnPlane[i].z, v4PointOnPlane.z );
				}
			}
			
			// If we're outside the tolerance range, do a new trace and store it.
			fltx4 fl4OutOfRange = CmpGtSIMD( fl4ClosestDist, ReplicateX4( flTol ) );
			if ( IsAnyNegative( fl4OutOfRange ) )
			{
				int nMask = TestSignSIMD( fl4OutOfRange );
				for(int i=0; i < 4; i++ )
				{
					if ( nMask & ( 1 << i ) )
					{
						Vector start = fvStartPnt.Vec( i );
						Vector end = fvEndPnt.Vec( i );

						//replace this with fast raycaster when available
						CBaseTrace tr;
						tr.plane.normal = vec3_invalid;
						g_pParticleSystemMgr->Query()->TraceLine( start, end, CONTENTS_SOLID, NULL , m_nCollisionGroupNumber, &tr );

						//Set the lifespan to 0 if we start solid, our trace distance is 0, or we hit within the offset area
						if ( ( tr.fraction < ( 1 - flOffsetPct )  ) || tr.startsolid || flTotalTraceDist == 0.0f )
						{
							SubFloat( fvStartPnt.x, i ) = 0.0f;
							SubFloat( fvStartPnt.y, i ) = 0.0f;
							SubFloat( fvStartPnt.z, i ) = 0.0f;
							SubFloat( v4PointOnPlane.x, i ) = 0.0f;
							SubFloat( v4PointOnPlane.y, i ) = 0.0f;
							SubFloat( v4PointOnPlane.z, i ) = 0.0f;
							SubFloat( fl4TraceOffset, i ) = 0.0f;
						}
						else
						{
							int nIndex = pCtx->m_nNumFixedPlanes;
							Vector vPointOnPlane =  start + ( tr.fraction * ( end - start ) ) ;
							SubFloat( v4PointOnPlane.x, i ) = vPointOnPlane.x;
							SubFloat( v4PointOnPlane.y, i ) = vPointOnPlane.y;
							SubFloat( v4PointOnPlane.z, i ) = vPointOnPlane.z;
							pCtx->m_bPlaneActive[nIndex] = true;
							pCtx->m_PointOnPlane[nIndex].DuplicateVector( vPointOnPlane );
							pCtx->m_PlaneNormal[nIndex].DuplicateVector( tr.plane.normal );
							pCtx->m_TraceStartPnt[nIndex].DuplicateVector( start );
							pCtx->m_TraceEndPnt[nIndex].DuplicateVector( end );
							pCtx->m_nNumFixedPlanes = pCtx->m_nNumFixedPlanes + 1;
							if ( pCtx->m_nNumFixedPlanes > m_nAllowedPlanes )
								pCtx->m_nNumFixedPlanes = 0;
							pCtx->m_nActivePlanes = min( m_nAllowedPlanes, pCtx->m_nActivePlanes + 1 );
						}
					}
				}
			}

			//Find the length of the trace
			fvStartPnt -= v4PointOnPlane;
			fvStartPnt *= v4ComponentScale;
			//Need to use the adjusted value of the trace length and collision point to account for the offset
			fltx4 fl4Dist = AddSIMD ( fvStartPnt.length(), fl4TraceOffset );
			fl4Velocity = AddSIMD( fl4Velocity, Four_Epsilons );
			//Divide by Velocity to get Lifespan
			*pLifespan = DivSIMD( fl4Dist, fl4Velocity );

			pXYZ += attr_stride;
			pPrev_XYZ += attr_stride;
			pLifespan += attr_stride;
		}
}





class C_INIT_CreateFromPlaneCache : public CParticleOperatorInstance
{
	DECLARE_PARTICLE_OPERATOR( C_INIT_CreateFromPlaneCache );

	uint32 GetWrittenAttributes( void ) const
	{
		return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
	}

	uint32 GetReadAttributes( void ) const
	{
		return 0;
	}

	size_t GetRequiredContextBytes( ) const
	{
		return sizeof( CWorldCollideContextData );
	}

	void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
		int nParticleCount, int nAttributeWriteMask,
		void *pContext) const;
};

DEFINE_PARTICLE_OPERATOR( C_INIT_CreateFromPlaneCache, "Position from Parent Cache", OPERATOR_PI_POSITION );

BEGIN_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateFromPlaneCache )
END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateFromPlaneCache )

void C_INIT_CreateFromPlaneCache::InitNewParticlesScalar( 
	CParticleCollection *pParticles, int start_p,
	int nParticleCount, int nAttributeWriteMask, void *pContext ) const
{
	if ( !pParticles->m_pParent )
	{
		for( ; nParticleCount--; start_p++ )
		{
			float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
			float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );

			SetVectorAttribute( xyz, vec3_origin );
			SetVectorAttribute( pxyz, vec3_origin );
		}
		return;
	}


	CWorldCollideContextData **ppCtx;
	if ( pParticles->m_pParent )
		ppCtx = &( pParticles->m_pParent->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
	else
		ppCtx = &( pParticles->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );

	CWorldCollideContextData *pCtx = NULL;
	if ( ! *ppCtx )
	{
		*ppCtx = new CWorldCollideContextData;
		(*ppCtx)->m_nActivePlanes = 0;
		(*ppCtx)->m_nNumFixedPlanes = 0;
		FourVectors fvEmpty;
		fvEmpty.DuplicateVector( vec3_origin );
		(*ppCtx)->m_PointOnPlane[0] = fvEmpty;
	}
	pCtx = *ppCtx;
	if ( pCtx->m_nActivePlanes > 0 )
	{
		for( ; nParticleCount--; start_p++ )
		{ 
			int nIndex = pParticles->RandomInt( 0, pCtx->m_nActivePlanes - 1 );
			if ( pCtx->m_PlaneNormal[nIndex].Vec( 0 ) == vec3_invalid )
			{
				float *plifespan = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
				*plifespan = 0.0f;
			}
			else
			{
				float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
				float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
				FourVectors fvPoint = pCtx->m_PointOnPlane[nIndex];
				Vector vPoint = fvPoint.Vec( 0 );
				SetVectorAttribute( xyz, vPoint );
				SetVectorAttribute( pxyz, vPoint );
			}
		}
	}
	else
	{
		for( ; nParticleCount--; start_p++ )
		{ 
			float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
			float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
			SetVectorAttribute( xyz, vec3_origin );
			SetVectorAttribute( pxyz, vec3_origin );
		}
	}
}





//
//
//
//
 
//-----------------------------------------------------------------------------
// Purpose: Add all operators to be considered active, here
//-----------------------------------------------------------------------------
void AddBuiltInParticleInitializers( void )
{
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateAlongPath );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_MoveBetweenPoints );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateWithinSphere );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_VelocityRandom );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateOnModel );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateWithinBox );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomRotationSpeed );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomLifeTime );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomAlpha );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomRadius );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomRotation );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomYaw );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomColor );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomTrailLength );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomSequence );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_PositionOffset );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_PositionWarp );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreationNoise );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InitialVelocityNoise );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapScalar );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InheritVelocity );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_AgeNoise ); 
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_SequenceLifeTime ); 
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateInHierarchy );  
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapScalarToVector );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateSequentialPath );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InitialRepulsionVelocity );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomYawFlip );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomSecondSequence );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapCPtoScalar );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapCPtoVector );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateFromParentParticles );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_DistanceToCPInit );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_LifespanFromVelocity );
	REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateFromPlaneCache );
}