-
Notifications
You must be signed in to change notification settings - Fork 1
/
MBC.js
284 lines (256 loc) · 10.7 KB
/
MBC.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
adjusting = false;
function process() {
//clearSolution();
calculateController.clearCanvas();
calculateController.clearResults();
var baseEndPos = calculateController.getTarget();
calculateController.adjust.setResultPos(baseEndPos);
var ret = processBty(baseEndPos, true);
addLog(ret);
isDangerClose(baseEndPos);
calculateController.updateAdjust(getCellValue('B5'), true);
}
function processBty(baseEndPos, print) {
calculateController.setMapFocus(baseEndPos);
var ret = [];
var sheaf = calculateController.sheaf
var sheafWidthMod = sheaf.getLength() / 2;
sheafDir = sheaf.getDir();
if (sheaf.getType() != 'special' && sheaf.getQuick() == 'true') {
sheafDir = calcDirection(calculateController.getBatteryPos().mgrs, baseEndPos.mgrs) / 360 * 6400;
}
console.log(calculateController.guns);
for (var i = 0; i < calculateController.guns.length; i++) {
var endPos = baseEndPos;
var gun = calculateController.guns[i];
var gunPos = calculateController.guns[i].getPos();
if (sheaf.getType() == 'parallel') {
gunPos = calculateController.getBatteryPos();
} else if (sheaf.getType() == 'special') {
var guns = calculateController.guns.length;
WidthPerGun = sheaf.getLength() / guns;
var halfSheaf = sheaf.getLength() / 2;
offset = (i+1) * WidthPerGun - (WidthPerGun / 2) - halfSheaf;
console.log(offset);
//offset = (calculateController.guns.length % 2 == 0) ? sheafWidthMod * (i-1) -0.5 : sheafWidthMod * (i-1) ;
endPos = adjustGridToGrid(endPos, sheafDir, 0, offset, 0, 0);
} else if (sheaf.getType() == 'linear') {
endPos = adjustGridToGrid(endPos, sheafDir, 0, 40 * (i-1), 0, 0);
} else if (sheaf.getType() == 'open') {
endPos = adjustGridToGrid(endPos, sheafDir, 0, 60 * (i-1), 0, 0);
} // else it's converged sheaf
calculateController.drawCross(gunPos, 'multiply');
var res = getSolutions(gunPos, endPos);
ret[i] = res;
if(print) {
gun.setResults(res);
}
}
return ret;
}
function startSweepZone(start, end, shape) {
calculateController.clearResults();
calculateController.clearCanvas();
var shifts = setSweepZone(start, end, shape);
calculateController.sweepZone.setDistance(shifts.targetLength);
for(var i = 0; i < shifts.solutions.length; i++)
{
calculateController.guns[i].setSweepZoneResults(shifts.base[i], shifts.solutions[i]);
}
addLog(shifts, true);
isDangerClose(end);
}
function isDangerClose(target) {
var keys = Object.keys(window.Controller.BCS.pointStores);
noDanger = true;
for (var i = keys.length - 1; i >= 0; i--) {
point = pointStores[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 < 600) {
console.log(keys[i]);
noDanger = false;
}
}
}
}
/*==============================================================================================================*/
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 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 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);
console.log(toFiveDigit(totalEast, true));
return toPos(toFiveDigit(totalEast, true) + '' + toFiveDigit(totalNorth,true), Math.floor(totalElev / total));
}
/*==============================================================================================================*/
function getSolutions(startPos, endPos) {
var distance = calcDistance(startPos.mgrs, endPos.mgrs);
var azimuth = calcDirection(startPos.mgrs, endPos.mgrs);
var solutions = calcQuadrants(distance, endPos.elev - startPos.elev);
calculateController.drawCross(endPos, 'add');
return {'azimuth' : azimuth / 360 * 6400, 'distance' : distance, 'quadrants' : solutions ,start: startPos, end: endPos};
}
function calcQuadrants(distance, elevDiff) {
var charges = mortarTable.mortar_82.charges;
var res = [];
for (var i = charges.length - 1; i >= 0; i--) {
var charge = charges[i];
console.log(calculateController.options.getRangeTable());
res[i] = QuadrantFromRange(mortarTable[calculateController.options.getRangeTable()][charge], distance, elevDiff);
}
return res;
}
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 {'qd' : null, 'tof' : null};
}
}
/*==============================================================================================================*/
function setSweepZone(start, end, shape) {
var result = {
solutions: [],
base: [],
error: ''
};
var sweepzone = calculateController.sweepZone;
var targetStartPos = (start != null) ? start : calculateController.adjust.getResultPos();
var startSolution = processBty(targetStartPos, false);
var startSheaf = calculateController.sheaf.toJSON();
if(calculateController.sweepZone.getSheaf() && !shape) {
calculateController.sheaf.setType('special');
calculateController.sheaf.setDir(calculateController.sweepZone.getSheafDir());
calculateController.sheaf.setLength(calculateController.sweepZone.getSheafLength());
}
var charge = sweepzone.getCharge();
var shifts = sweepzone.getShifts();
var targetEndPos = (end != null) ? end : sweepzone.getTarget();
var endSolution = processBty(targetEndPos, false);
calculateController.sheaf.fromJSON(startSheaf);
var targetLength = calcDistance(targetStartPos.mgrs, targetEndPos.mgrs);
result.targetLength = targetLength;
for (var i = 0; i < calculateController.guns.length; i++) {
var startQuadrant = startSolution[i].quadrants[charge].qd;
var quadrant = endSolution[i].quadrants[charge].qd;
if (quadrant == null) {
result['error'] = 'this charge does not cover the target area';
}
var startAzimuth = startSolution[i].azimuth;
var azimuth = endSolution[i].azimuth;
calculateController.drawLine(startSolution[i].end, endSolution[i].end);
var sweep = (azimuth - startAzimuth) / shifts;
var zone = (quadrant - startQuadrant) / shifts;
result['base'][i] = startSolution[i];
result['solutions'][i] = {'baseaz' : Math.round(startAzimuth), 'baseqd' : startQuadrant, 'sweep' : Math.round(sweep), 'zone' : zone, 'shifts' : shifts};
}
return result;
}
/*=========================================adjust===============================================================*/
function adjust() {
adjusting = true;
calculateController.clearResults();
var adjust = calculateController.adjust;
var res = adjustGridToGrid(adjust.getResultPos(), adjust.getOT(), adjust.getAD(), adjust.getLR(), adjust.getUD());
adjust.setResultPos(res);
calculateController.setTarget(res);
console.log(res);
var sl = processBty(res, true);
isDangerClose(res);
calculateController.updateAdjust(getCellValue('B5'));
adjusting = false;
}
function adjustGridToGrid(pos, ot, ad, lr, ud) {
console.log('ud', 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 = toFiveDigit(endEasting + '', true);
endNorthing = toFiveDigit(endNorthing + '', true);
var endGrid = endEasting + '' + endNorthing;
var endElev = parseInt(pos.elev) + parseInt(ud);
return toPos(endGrid, endElev);
}
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);
}
/*=============================preload=============================*/
function load()
{
var ref = calculateController.getLoadReference();
var row = storeController.getLoadRow(ref);
calculateController.sheaf.fromJSON(row.sheaf.toJSON());
calculateController.sweepZone.fromJSON(row.sweepZone.toJSON());
}
function save()
{
var ref = calculateController.getSaveReference();
var row = storeController.getSaveRow(ref);
row.setReference(ref);
row.setPosition(calculateController.adjust.getResultPos());
row.sheaf.fromJSON(calculateController.sheaf.toJSON());
row.sweepZone.fromJSON(calculateController.sweepZone.toJSON());
}