-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculate.js
349 lines (320 loc) · 11.8 KB
/
calculate.js
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
/*=========================================BTY========================================*/
function calculate(firemission) {
bty = window.BCS.battery;
for (var i = bty.guns.length - 1; i >= 0; i--) {
gun = bty.guns[i];
solution = {
piece: gun.name,
direction: gun.direction,
targetPos:null,
type: bty.type,
mof: firemission.engagement.mof,
moc: firemission.engagement.moc,
rounds: firemission.ffe.rounds,
shell: firemission.ffe.shell,
fuze: firemission.ffe.fuze,
charge: firemission.engagement.charge,
az:null,
qd:null,
tof:null
}
if(firemission.engagement.mof == "adj") {
if(firemission.adjust.guns == gun.name || firemission.adjust.guns == "All")
{
$.extend(solution, {
rounds: 1,
shell: firemission.adjust.shell,
fuze: firemission.adjust.fuze
});
} else {
$.extend(solution, {
moc: "dnl"
});
}
}
firemission.solutions[gun.name] = solution;
if(i == 1 || bty.guns.length == 1) {
firemission.solutions.bty = solution;
firemission.solutions.bty.piece = "bty"
firemission.solutions.bty.targetPos = firemission.target.position;
}
}
calcBatteryPosition(firemission);
setSheafTargets(firemission);
fillSolutions(firemission);
isDangerClose(firemission);
//console.log(firemission);
return firemission;
}
function calcBatteryPosition(firemission) {
positions = [];
bty = window.BCS.battery;
for (var i = bty.guns.length - 1; i >= 0; i--) {
gun = bty.guns[i];
positions.push(gun.position);
}
pos = calcAverage(positions);
if(firemission != null) {
firemission.solutions.bty.position = pos;
}
return pos;
}
function setSheafTargets(firemission) {
sheaf = firemission.engagement.sheaf;
sheafWidthMod = sheaf.length / 2;
sheafDir = sheaf.direction;
batteryPos = firemission.solutions.bty.position;
target = firemission.target.position;
if (sheaf.quick == 'true') {
sheafDir = calcDirection(batteryPos.mgrs, target.mgrs) / 360 * 6400;
//console.log(sheafDir);
}
bty = window.BCS.battery;
for (var i = bty.guns.length - 1; i >= 0; i--) {
gun = bty.guns[i];
var gunTarget = target;
var sourcePosition = gun.position;
switch(sheaf.type) {
case "parallel":
sourcePosition = batteryPos;
break;
case "special":
{
var guns = bty.guns.length;
WidthPerGun = sheaf.length / guns;
var halfSheaf = sheaf.length / 2;
offset = (i+1) * WidthPerGun - (WidthPerGun / 2) - halfSheaf;
gunTarget = adjustGridToGrid(gunTarget, sheafDir, 0, offset, 0, 0);
}
break;
case "linear":
gunTarget = adjustGridToGrid(gunTarget, sheafDir, 0, 40 * (i-1), 0, 0);
break;
case "open":
gunTarget = adjustGridToGrid(gunTarget, sheafDir, 0, 60 * (i-1), 0, 0);
break;
}
firemission.solutions[gun.name].sourcePos = sourcePosition;
firemission.solutions[gun.name].targetPos = gunTarget;
}
}
function fillSolutions(firemission) {
keys = Object.keys(firemission.solutions);
for (var i = 0; i < keys.length; i++) {
solution = firemission.solutions[keys[i]];
console.log("solution ",solution);
solution.quadrants = [];
results = getSolutions(solution);
quads = null;
solution.firstCharge = -1;
for (var j = 0; j < results.quadrants.length; j++) {
res = results.quadrants[j];
if(res != null && res.qd != null)
{
if(solution.firstCharge == -1) solution.firstCharge = j;
quads = res;
solution.az = Math.round(results.azimuth);
solution.df = Math.round(3200 - (solution.az - solution.direction));
console.log(quads);
sol = {};
sol.displayCharge = quads.ch;
sol.qd = Math.round(quads.qd);
sol.tof = Math.round(quads.tof);
sol.impAngle = Math.round(quads.impactAngle);
sol.maxOrd = Math.round(quads.maxOrd);
sol.targetDist = Math.round(quads.targetDist);
sol.heightDelta = Math.round(quads.heightDelta);
solution.quadrants.push(sol);
}
}
solution.az = Math.round(results.azimuth);
solution.qd = Math.round(quads.qd);
solution.tof = Math.round(quads.tof);
solution.impAngle = Math.round(quads.impactAngle);
solution.maxOrd = Math.round(quads.maxOrd);
//console.log('PROCESSED', solution);
}
}
/**
* Gets battery position
* parameter: guns key value json list
*/
function getBatteryPosition(guns) {
var positions = [];
var keys = Object.keys(positions);
for (var i = keys.length - 1; i >= 0; i--) {
positions.push(guns[keys[i]].getPos());
}
return calcAverage(positions);
}
/**
* Gets solution for a gun with a round between two positions
*/
function getSolutions(solutionBase) {
//console.log(solutionBase);
var distance = calcDistance(solutionBase.sourcePos.mgrs, solutionBase.targetPos.mgrs);
var azimuth = calcDirection(solutionBase.sourcePos.mgrs, solutionBase.targetPos.mgrs);
var solutions = CalcQuadrants(solutionBase.type, solutionBase.shell, distance, solutionBase.targetPos.elev - solutionBase.sourcePos.elev);
return {'azimuth' : azimuth / 360 * 6400, 'distance' : distance, 'quadrants' : solutions};
}
/*======================================Quadrant======================================*/
/**
* gun: which gun to take from the tables, example: mortar_82
* round: which table to take from the gun: all or all_nwr for the mortar_82, this will later be used for different shell types with different tables
* distance: distance in m
* elevDiff: elev Difference in m
*/
function CalcQuadrantsRTAB(gun, round, distance, elevDiff) {
var charges = weapons[gun].roundTypes[round].charges;
var res = [];
for (var i = charges.length - 1; i >= 0; i--) {
var charge = charges[i];
qds = QuadrantFromRange(weapons[gun], round, distance, elevDiff);
if(qds != null) res[i] = qds;
}
return res;
}
/**
* table: json table of charge
*
*/
function QuadrantFromRange(table, distance, elevDiff) {
var low = Math.floor(distance / 50) * 50;
var high = low + 50;
if(table[low] != null && table[high] != null)
{
var substep = high - distance;
var difference = high - low;
var lowqd = table[low].qd;
var highqd = table[high].qd;
var diffqd = lowqd - highqd;
var subqd = diffqd / difference * substep;
var qd = Math.round(highqd + subqd);
var lowElev = table[low].dqd;
var highElev = table[high].dqd;
var subElev = highElev + (lowElev - highElev) / difference * substep;
var adjElev = subElev * (elevDiff / 100) * -1;
qd = qd + adjElev;
//console.log('qd', elevDiff);
return {'qd' : Math.round(qd), 'tof' : table[low].tof};
}else{
return null;
}
}
/*======================================Adjust======================================*/
function adjustGridToGrid(pos, ot, ad, lr, ud) {
var de = deltaEasting(ot, ad, lr);
var dn = deltaNorthing(ot, ad, lr);
var startGrid = pos.mgrs;
de = Math.round(de); dn = Math.round(dn);
var startElev = pos.elev;
var endEasting = parseInt(startGrid.easting, 10) + de;
var endNorthing = parseInt(startGrid.northing, 10) + dn;
endEasting = Position.toFiveDigit(endEasting + '', true);
endNorthing = Position.toFiveDigit(endNorthing + '', true);
var endGrid = endEasting + '' + endNorthing;
var endElev = parseInt(pos.elev) + parseInt(ud);
return Position.generate(endGrid, endElev);
}
/*======================================General======================================*/
//here be all the math type shit used, don't touch unless broken
function deltaEasting(ot, ad, lr) {
var offAD = Math.sin((ot / 6400 * 360) * (Math.PI / 180)) * ad;
var offLR = Math.cos((ot / 6400 * 360) * (Math.PI / 180)) * lr;
return Math.round(offAD + offLR);
}
function deltaNorthing(ot, ad, lr) {
var offAD = Math.cos((ot / 6400 * 360) * (Math.PI / 180)) * ad;
var offLR = Math.sin((ot / 6400 * 360) * (Math.PI / 180)) * -lr;
return Math.round(offAD + offLR);
}
function calcAverage(positions) {
totalEast = 0;
totalNorth = 0;
totalElev = 0;
for (var i = positions.length - 1; i >= 0; i--) {
var pos = positions[i];
Math.floor(totalEast += parseInt(pos.mgrs.easting));
Math.floor(totalNorth += parseInt(pos.mgrs.northing));
Math.floor(totalElev += parseInt(pos.elev));
}
var total = positions.length;
totalEast = Math.floor(totalEast / total);
totalNorth = Math.floor(totalNorth / total);
return Position.generate(Position.toFiveDigit(totalEast, true) + '' + Position.toFiveDigit(totalNorth,true), Math.floor(totalElev / total));
}
function getGridDiff(start, end) {
var deltaX = getDiff(start.easting, end.easting);
var deltaY = getDiff(start.northing, end.northing);
return {'dx' : deltaX, 'dy' : deltaY};
}
function getDiff(i, j) {
return Math.abs(i - j);
}
function calcDistance(start, end) {
var diff = getGridDiff(start, end);
var dist = Math.round(Math.sqrt(Math.pow(diff.dx, 2) + Math.pow(diff.dy, 2)));
return dist;
}
function calcDirection(start, end) {
var diff = getGridDiff(start, end);
var angle = Math.atan(diff.dx / diff.dy) / (Math.PI / 180);
if(start.easting > end.easting && start.northing < end.northing)
return 360 - angle;
else if(start.easting > end.easting)
return 180 + angle;
else if(start.northing > end.northing)
return 180- angle;
else
return angle;
}
function isInRange(target, positions, distance) {
dangerCloseHide();
var keys = Object.keys(positions);
noDanger = true;
for (var i = keys.length - 1; i >= 0; i--) {
point = positions[keys[i]];
if(point.friendly == "true") {
//console.log('checking', dist, target, point);
var dist = calcDistance(target.mgrs, point.position.mgrs);
//console.log(dist);
if(dist < distance) {
dangerClose(keys[i]);
noDanger = false;
}
}
}
}
function pad(base, length, before) {
base = base + '';
if (base.length < length) {
for (var i = base.length; i < length; i++) {
if (before)
base = '0' + base;
else
base = base + '0';
}
}
return base;
}
function isDangerClose(firemission) {
$('.danger-close-units').html('');
$('.danger-close').hide();
//console.log("DC CHECK");
target = firemission.target.position;
pointStores = window.BCS.knownPoints;
var keys = Object.keys(pointStores);
noDanger = true;
for (var i = keys.length - 1; i >= 0; i--) {
////console.log(i, point.friendly);
point = pointStores[keys[i]];
if(point.friendly == true || point.friendly == "true") {
var dist = calcDistance(target.mgrs, point.position.mgrs);
if(dist < 600) {
$('.danger-close-units').html($('.danger-close-units').html() + ' ' + keys[i]);
$('.danger-close').show();
noDanger = false;
}
}
}
}