Computing gun sight code with USAAF K-14A sight.
[fg:toms-fgdata.git] / Aircraft / Instruments-3d / computing-gun-sights / K14 / Nasal / K14-reticle.nas
1 # ==============================================================================\r
2 # K14 Reticle\r
3 # ==============================================================================\r
4 \r
5 var propertyTreeRoot = "/controls/armament/gunsight/";    \r
6 var sideEdge = 0.08;\r
7 var zCenterLine = getprop(propertyTreeRoot, "zCenterLine");\r
8 var topEdge = zCenterLine + 0.04; \r
9 \r
10 var pipperVisibility = func()\r
11 {\r
12     if (getprop(propertyTreeRoot, "reticleSelectorPos") < 2 and  \r
13             getprop(propertyTreeRoot, "power-on") != 0 and\r
14                 getprop("/controls/engines/engine/master-bat") == 1)\r
15         { \r
16            setprop(propertyTreeRoot, "fixedReticleOn", 1);\r
17         }\r
18         else\r
19         {\r
20            setprop(propertyTreeRoot, "fixedReticleOn", 0);\r
21         }\r
22 \r
23     if (getprop(propertyTreeRoot, "computer-on") == 1 and \r
24             getprop(propertyTreeRoot, "reticleSelectorPos") > 0 and \r
25                 getprop(propertyTreeRoot, "power-on") != 0 and\r
26                 getprop("/controls/engines/engine/master-bat") == 1)\r
27     {\r
28         var elevation = call(func getprop(propertyTreeRoot, "elevation"), var err = []);\r
29         if (!size(err))\r
30         {\r
31             elevation = elevation * 0.401885 + zCenterLine;\r
32             var azimuth = call(func getprop(propertyTreeRoot, "azimuth"), var err1 = []);\r
33             if (!size(err1)) \r
34             {             \r
35                        azimuth = azimuth * 0.401885;\r
36                    var ringSize = call(func getprop(propertyTreeRoot, "rangeRingSize"), var err2 = []);\r
37                            if (!size(err2))\r
38                            {    \r
39                                   if (elevation + ringSize > topEdge \r
40                                           or (azimuth > 0 and azimuth > sideEdge)\r
41                                           or (azimuth < 0 and azimuth < -sideEdge))\r
42                                   {\r
43                                          setprop(propertyTreeRoot, "diamondTopVisibility", 0);\r
44                                   }\r
45                                   else\r
46                                   {\r
47                                          setprop(propertyTreeRoot, "diamondTopVisibility", 1);\r
48                                   }\r
49                                 \r
50                                   if (elevation - ringSize > topEdge \r
51                                           or (azimuth > 0 and azimuth > sideEdge)\r
52                                           or (azimuth < 0 and azimuth < -sideEdge))\r
53                                   {\r
54                                          setprop(propertyTreeRoot, "diamondBottomVisibility", 0);\r
55                                   }\r
56                                   else\r
57                                   {\r
58                                          setprop(propertyTreeRoot, "diamondBottomVisibility", 1);\r
59                                   }\r
60                                   \r
61                                   if (elevation + (0.5 * ringSize) > topEdge \r
62                                            or (azimuth > 0 and azimuth + (0.866025 * ringSize) > sideEdge)\r
63                                            or (azimuth < 0 and azimuth + (0.866025 * ringSize) < -sideEdge))\r
64                                   { \r
65                                          setprop(propertyTreeRoot, "diamondURVisibility", 0);\r
66                                   }\r
67                                   else\r
68                                   {\r
69                                          setprop(propertyTreeRoot, "diamondURVisibility", 1);\r
70                                   }\r
71                                 \r
72                                   if (elevation + (0.5 * ringSize) > topEdge \r
73                                            or (azimuth < 0  and azimuth - (0.866025 * ringSize) < -sideEdge)\r
74                                            or (azimuth > 0  and azimuth - (0.866025 * ringSize) > sideEdge))\r
75                                   { \r
76                                          setprop(propertyTreeRoot, "diamondULVisibility", 0);\r
77                                   }\r
78                                   else\r
79                                   {\r
80                                          setprop(propertyTreeRoot, "diamondULVisibility", 1);\r
81                                   }\r
82                                 \r
83                                   if (elevation - (0.5 * ringSize) > topEdge\r
84                                           or (azimuth > 0 and azimuth + (0.866025 * ringSize) > sideEdge)\r
85                                           or (azimuth < 0  and azimuth + (0.866025 * ringSize) < -sideEdge))\r
86                                   {\r
87                                          setprop(propertyTreeRoot, "diamondLRVisibility", 0);\r
88                                   }\r
89                                   else\r
90                                   {\r
91                                          setprop(propertyTreeRoot, "diamondLRVisibility", 1);\r
92                                   }\r
93                                 \r
94                                   if (elevation - (0.5 * ringSize) > topEdge\r
95                                           or (azimuth < 0 and azimuth - (0.866025 * ringSize) < -sideEdge)\r
96                                           or (azimuth > 0  and azimuth - (0.866025 * ringSize) > sideEdge))\r
97                                   {\r
98                                          setprop(propertyTreeRoot, "diamondLLVisibility", 0);\r
99                                   }\r
100                                   else\r
101                                   {\r
102                                          setprop(propertyTreeRoot, "diamondLLVisibility", 1);\r
103                                   }\r
104 \r
105                                   if (elevation > topEdge\r
106                                           or (azimuth > 0 and azimuth > sideEdge)\r
107                                           or (azimuth < 0 and azimuth < -sideEdge))\r
108                                   {\r
109                                          setprop(propertyTreeRoot, "pipperVisibility", 0);\r
110                                   }\r
111                                   else\r
112                                   {\r
113                                          setprop(propertyTreeRoot, "pipperVisibility", 1);\r
114                                   }\r
115                                 }\r
116                     }\r
117                 }\r
118         }\r
119         else\r
120         {\r
121            setprop(propertyTreeRoot, "diamondTopVisibility", 0);\r
122            setprop(propertyTreeRoot, "diamondBottomVisibility", 0);\r
123            setprop(propertyTreeRoot, "diamondURVisibility", 0);\r
124            setprop(propertyTreeRoot, "diamondULVisibility", 0);\r
125            setprop(propertyTreeRoot, "diamondLRVisibility", 0);\r
126            setprop(propertyTreeRoot, "diamondLLVisibility", 0);\r
127            setprop(propertyTreeRoot, "pipperVisibility", 0);  \r
128         }\r
129 }\r
130 \r
131 var scaleRangeFindingReticle = func()\r
132 {\r
133    if (getprop(propertyTreeRoot, "gunsightComputerInitialized") == 1)\r
134    {   \r
135       var span = getprop(propertyTreeRoot, "span");   \r
136       if (span < 30)\r
137       {\r
138          setprop(propertyTreeRoot, "span", 30);\r
139       }\r
140       else if (span > 120)\r
141       {\r
142             setprop(propertyTreeRoot, "span", 120);\r
143       }\r
144    \r
145       var range = getprop(propertyTreeRoot, "range");\r
146       if (range < 600)\r
147       {\r
148              setprop(propertyTreeRoot, "range", 600);\r
149       }\r
150       else if (range > 2400)\r
151       {\r
152              setprop(propertyTreeRoot, "range", 2400);\r
153      }\r
154    \r
155       var mils = 1000 * (span / range);\r
156       setprop(propertyTreeRoot, "mils", mils); \r
157       var newRangeRingSize = 0.00019 * mils; # scale mils to meters\r
158       setprop(propertyTreeRoot, "rangeRingSize", newRangeRingSize);  \r
159    }\r
160 }   \r
161 \r
162 setlistener("/controls/armament/gunsight/gunsightComputerInitialized", scaleRangeFindingReticle, 1, 0);\r
163 setlistener("/controls/armament/gunsight/span", scaleRangeFindingReticle, 1, 0);\r
164 setlistener("/controls/armament/gunsight/range", scaleRangeFindingReticle, 1, 0);\r
165 setlistener("/controls/armament/gunsight/elevation", pipperVisibility, 1, 0);\r
166 setlistener("/controls/armament/gunsight/gunsightComputerInitialized", pipperVisibility, 1, 0);\r
167 setlistener("/controls/armament/gunsight/reticleSelectorPos", pipperVisibility, 1, 0);\r
168 setlistener("/controls/armament/gunsight/power-on", pipperVisibility, 1, 0);\r