osanseviero HF staff commited on
Commit
3d380eb
1 Parent(s): f13687a

Upload combat.js

Browse files
Files changed (1) hide show
  1. combat.js +802 -0
combat.js ADDED
@@ -0,0 +1,802 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ var battleWIDTH=310;
3
+ var battleHEIGHT=150;
4
+ var battleGRID_WIDTH=31; //number of grid cells
5
+ var battleGRID_HEIGHT=15;
6
+ var battleINV_GRID_WIDTH = 1 / (battleWIDTH / battleGRID_WIDTH);
7
+ var battleINV_GRID_HEIGHT = 1 / (battleHEIGHT / battleGRID_HEIGHT);
8
+
9
+ var battleLEFTSHIPS=200;
10
+ var battleRIGHTSHIPS=200;
11
+
12
+ var battleMAXSPEED = 2;
13
+ var battleDEATH_THRESHOLD = 0.5; //used to calculate battles
14
+ var battleLEFTCOLOR = "#ffffff"
15
+ var battleRIGHTCOLOR = "#000000"
16
+ var battleEXPLODECOLOR = "#ffffff"
17
+
18
+ var ships = new Array();
19
+ var numShips = 0;
20
+ var grid = new Array(battleGRID_HEIGHT);
21
+ var numLeftShips = 0;
22
+ var numRightShips = 0;
23
+
24
+ var probeCombat = 0;
25
+ var probeCombatBaseRate = .15;
26
+ var attackSpeed = .2;
27
+ var battleSpeed = .2;
28
+ var attackSpeedFlag = 0;
29
+ var attackSpeedMod = .1;
30
+ var battles = [];
31
+ var battleID = 0;
32
+ var battleName = "foo";
33
+ var battleNameFlag = 0;
34
+ var maxBattles = 1;
35
+ var battleClock = 0;
36
+ var battleAlarm = 10;
37
+ var outcomeTimer = 150;
38
+ var drifterCombat = 1.75;
39
+ var warTrigger = 1000000;
40
+
41
+ var unitSize = 0;
42
+ var driftersKilled = 0;
43
+ var battleEndDelay = 0;
44
+ var battleEndTimer = 100;
45
+ var masterBattleClock = 0;
46
+
47
+ var honorCount = 0;
48
+ var threnodyTitle = "Durenstein 1";
49
+ var bonusHonor = 0;
50
+ var honorReward = 0;
51
+
52
+
53
+ //NON-CANVAS BATTLE LOGIC
54
+
55
+ function checkForBattles(){
56
+ if (drifterCount>warTrigger && probeCount > 0 && battles.length < maxBattles){
57
+ var r = (Math.random()*100);
58
+ if (r>=50){
59
+ if (battleFlag == 0){battleFlag = 1};
60
+ createBattle();
61
+ }
62
+ }
63
+ }
64
+
65
+ var battleNames = ["Aboukir", "Abensberg", "Acre", "Alba de Tormes", "la Albuera", "Algeciras Bay", "Amstetten", "Arcis-sur-Aube", "Aspern-Essling", "Jena-Auerstedt", "Arcole", "Austerlitz", "Badajoz", "Bailen", "la Barrosa", "Bassano", "Bautzen", "Berezina", "Bergisel", "Borodino", "Burgos", "Bucaco", "Cadiz", "Caldiero", "Castiglione", "Castlebar", "Champaubert", "Chateau-Thierry", "Copenhagen", "Corunna", "Craonne", "Dego", "Dennewitz", "Dresden", "Durenstein", "Eckmuhl", "Elchingen", "Espinosa de los Monteros", "Eylau", "Cape Finisterre", "Friedland", "Fuentes de Onoro", "Gevora River", "Gerona", "Hamburg", "Haslach-Jungingen", "Heilsberg", "Hohenlinden", "Jena-Auerstedt", "Kaihona", "Kolberg", "Landshut", "Leipzig", "Ligny", "Lodi", "Lubeck", "Lutzen", "Marengo", "Maria", "Medellin", "Medina de Rioseco", "Millesimo", "Mincio River", "Mondovi", "Montebello", "Montenotte", "Montmirail", "Mount Tabor", "The Nile", "Novi", "Ocana", "Cape Ortegal", "Orthez", "Pancorbo", "Piave River", "The Pyramids", "Quatre Bras", "Raab", "Raszyn", "Rivoli", "Rolica", "La Rothiere", "Rovereto", "Saalfeld", "Schongrabern", "Salamanca", "Smolensk", "Somosierra", "Talavera", "Tamames", "Trafalgar", "Trebbia", "Tudela", "Ulm", "Valls", "Valmaseda", "Valutino", "Vauchamps", "Vimeiro", "Vitoria", "Wagram", "Waterloo", "Wavre", "Wertingen", "Zaragoza"];
66
+
67
+ var battleNumbers = [];
68
+
69
+ for (i=0; i<battleNames.length; i++){
70
+ battleNumbers.push(1);
71
+ }
72
+
73
+ function generateBattleName(){
74
+ var x = Math.floor(Math.random()*battleNames.length);
75
+ var name = battleNames[x] + " " + battleNumbers[x];
76
+ battleNumbers[x] = battleNumbers[x] + 1;
77
+ return name
78
+ }
79
+
80
+
81
+ /*
82
+
83
+ function battleWrite(newBattle){
84
+
85
+ var element = document.getElementById("battleReportsDiv");
86
+ var reference = document.getElementById("battleListTop");
87
+
88
+ var newBattleReport = document.createElement("div");
89
+ newBattleReport.setAttribute("id", "battleReport"+newBattle.id);
90
+
91
+ var battleNameP = document.createElement("p");
92
+ battleNameP.setAttribute("class", "clean");
93
+
94
+ if (battleNameFlag == 0){
95
+ battleName = document.createTextNode("Battle "+newBattle.id);
96
+
97
+ }
98
+
99
+ if (battleNameFlag == 1){
100
+ battleName = document.createTextNode(generateBattleName());
101
+ }
102
+
103
+ battleNameP.appendChild(battleName);
104
+
105
+ newBattleReport.appendChild(battleNameP);
106
+
107
+ var battleDetailsP = document.createElement("p");
108
+ battleDetailsP.setAttribute("class", "clean");
109
+
110
+ var clipsLabelSpan = document.createElement("span");
111
+ clipsLabelSpan.style.fontWeight = "bold";
112
+
113
+ var clipsLabel = document.createTextNode("Clips: ");
114
+ clipsLabelSpan.appendChild(clipsLabel);
115
+
116
+ battleDetailsP.appendChild(clipsLabelSpan);
117
+
118
+ var clipProbeCountSpan = document.createElement("span");
119
+ clipProbeCountSpan.setAttribute("id", "battle"+newBattle.id+"clipCount")
120
+ var clipProbeCount = document.createTextNode(numberCruncher(newBattle.clipProbes));
121
+ clipProbeCountSpan.appendChild(clipProbeCount);
122
+ battleDetailsP.appendChild(clipProbeCountSpan);
123
+
124
+ var driftersLabelSpan = document.createElement("span");
125
+ driftersLabelSpan.style.fontWeight = "bold";
126
+
127
+ var driftersLabel = document.createTextNode(" Drifters: ");
128
+ driftersLabelSpan.appendChild(driftersLabel);
129
+
130
+ battleDetailsP.appendChild(driftersLabelSpan);
131
+
132
+ var drifterProbeCountSpan = document.createElement("span");
133
+ drifterProbeCountSpan.setAttribute("id", "battle"+newBattle.id+"drifterCount")
134
+ var drifterProbeCount = document.createTextNode(numberCruncher(newBattle.drifterProbes));
135
+ drifterProbeCountSpan.appendChild(drifterProbeCount);
136
+ battleDetailsP.appendChild(drifterProbeCountSpan);
137
+
138
+ newBattleReport.appendChild(battleDetailsP);
139
+
140
+ var territoryP = document.createElement("p");
141
+ territoryP.setAttribute("class", "clean");
142
+ var t = (newBattle.territory/availableMatter);
143
+ var territoryDisplay = document.createTextNode("Territory at stake: "+Math.ceil(t*100)+"% of available matter");
144
+ territoryP.appendChild(territoryDisplay);
145
+
146
+ newBattleReport.appendChild(territoryP);
147
+
148
+ var line = document.createElement("hr");
149
+ newBattleReport.appendChild(line);
150
+
151
+ reference.insertBefore(newBattleReport, reference.childNodes[0]);
152
+
153
+ }
154
+
155
+ function updateBattles(){
156
+
157
+ var combatEffectiveness = probeCombatBaseRate;
158
+
159
+ if (battleNameFlag == 1) {
160
+ combatEffectiveness = combatEffectiveness*2
161
+ }
162
+
163
+ if (attackSpeedFlag == 1){
164
+ battleSpeed = attackSpeed * .85;
165
+ if (battleSpeed > .99){
166
+ battleSpeed = .99;
167
+ }
168
+ }
169
+
170
+ for(var i = 0; i < battles.length; i++){
171
+ r = Math.random();
172
+ if (r>=battleSpeed) {
173
+ var clipCasualties = battles[i].drifterProbes * drifterCombat * (1-battleSpeed);
174
+ if (clipCasualties>battles[i].clipProbes){
175
+ clipCasualties=battles[i].clipProbes;
176
+ }
177
+
178
+ battles[i].clipProbes = battles[i].clipProbes - clipCasualties;
179
+ probeCount = probeCount - clipCasualties;
180
+ probesLostCombat = probesLostCombat + clipCasualties;
181
+ document.getElementById('probesLostCombatDisplay').innerHTML = numberCruncher(probesLostCombat);
182
+
183
+ // document.getElementById('battle'+battles[i].id+"clipCount").innerHTML = numberCruncher(battles[i].clipProbes);
184
+
185
+ } else {
186
+ var drifterCasualties = battles[i].clipProbes * Math.pow(probeCombat, 1.7) * combatEffectiveness;
187
+ if (drifterCasualties>battles[i].drifterProbes){
188
+ drifterCasualties=battles[i].drifterProbes;
189
+ }
190
+
191
+ battles[i].drifterProbes = battles[i].drifterProbes - drifterCasualties;
192
+ drifterCount = drifterCount - drifterCasualties;
193
+
194
+ // document.getElementById('battle'+battles[i].id+"drifterCount").innerHTML = numberCruncher(battles[i].drifterProbes);
195
+ }
196
+
197
+ if (battles[i].drifterProbes < 1){
198
+ battles[i].victory = true;
199
+ }
200
+
201
+ if (battles[i].clipProbes < 1 && battles[i].victory == false){
202
+ battles[i].loss = true;
203
+ }
204
+
205
+ if (battles[i].loss == true && battles[i].whiteFlag == 0){
206
+ availableMatter = availableMatter - battles[i].territory;
207
+ battles[i].whiteFlag = 1;
208
+ }
209
+
210
+ if (battles[i].loss == true){
211
+
212
+ // document.getElementById("battleReport"+battles[i].id).style.backgroundColor = "LightGrey";
213
+
214
+ battles[i].reportCount++;
215
+ if (battles[i].reportCount > outcomeTimer){
216
+ battles[i].garbageFlag = 1;
217
+ }
218
+ }
219
+
220
+ if (battles[i].victory == true){
221
+ battles[i].reportCount++;
222
+ if (battles[i].reportCount > outcomeTimer){
223
+ battles[i].garbageFlag = 1;
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ function battleCleanUp(){
230
+ for(var i = battles.length-1; i >= 0; i--){
231
+ if (battles[i].garbageFlag == 1){
232
+ var element = document.getElementById('battleReport'+battles[i].id);
233
+ element.parentNode.removeChild(element);
234
+ battles.splice(i,1);
235
+ }
236
+ }
237
+
238
+ }
239
+
240
+
241
+
242
+ function updateBattleDisplay(battle){
243
+
244
+
245
+
246
+ var element = document.getElementById("battleListTop");
247
+
248
+ var newBattle = document.createElement("div");
249
+ newBattle.setAttribute("id", battle.id);
250
+ element.appendChild(newBattle, element.firstChild);
251
+
252
+ var span = document.createElement("span");
253
+ span.setAttribute("class", "clean");
254
+ span.style.fontWeight = "bold";
255
+ newBattle.appendChild(span);
256
+
257
+ var hed = document.createTextNode("Combatants");
258
+ span.appendChild(hed);
259
+
260
+ var clipsCount = document.createElement("span");
261
+ clipsCount = battle.clipProbes;
262
+ element.appendChild(span);
263
+
264
+ }
265
+
266
+ */
267
+
268
+ //CANVAS BATTLE DISPLAY
269
+
270
+
271
+ function Battle(){
272
+
273
+ var canvas;
274
+ var context;
275
+ var interval;
276
+ var sign;
277
+
278
+ battleRestart();
279
+
280
+ this.initialize = function(){
281
+ canvas = document.getElementById("canvas");
282
+
283
+ context = canvas.getContext('2d');
284
+
285
+ canvas.width = battleWIDTH;
286
+ canvas.height = battleHEIGHT;
287
+
288
+ var interval = setInterval(Update, 16);
289
+
290
+ battleRestart();
291
+ }
292
+
293
+ var Update = function(){
294
+
295
+ ClearFrame();
296
+ UpdateGrid();
297
+ MoveShips();
298
+ DoCombat();
299
+
300
+ }
301
+
302
+ function checkForBattleEnd(){
303
+
304
+ if (battles.length>0){
305
+
306
+ if (numLeftShips == 0 || numRightShips == 0){
307
+
308
+ if (project121.flag == 1){
309
+ document.getElementById("victoryDiv").style.visibility = "visible";
310
+
311
+ if (numLeftShips == 0){
312
+ if (honorCount == 0){
313
+ bonusHonor = 0;
314
+ honor = honor - battleLEFTSHIPS;
315
+ honorCount = 1;
316
+ }
317
+ document.getElementById("battleResult").innerHTML = "DEFEAT";
318
+ document.getElementById("battleResultSign").innerHTML = "-";
319
+ document.getElementById("honorAmount").innerHTML = battleLEFTSHIPS;
320
+ document.getElementById("honorDisplay").innerHTML = Math.round(honor).toLocaleString();
321
+ threnodyTitle = battleName;
322
+ }
323
+
324
+ if (numRightShips == 0){
325
+ if (honorCount == 0){
326
+ honorReward = battleRIGHTSHIPS + bonusHonor;
327
+ document.getElementById("honorAmount").innerHTML = honorReward;
328
+ honor = honor + honorReward;
329
+
330
+ if (project134.flag == 1){
331
+ bonusHonor = bonusHonor + 10;
332
+ }
333
+
334
+ honorCount = 1;
335
+ }
336
+
337
+ document.getElementById("battleResult").innerHTML = "VICTORY";
338
+ document.getElementById("battleResultSign").innerHTML = "+";
339
+ document.getElementById("honorDisplay").innerHTML = Math.round(honor).toLocaleString();
340
+
341
+ }
342
+
343
+ }
344
+
345
+ battleEndDelay++;
346
+
347
+ } else if (numLeftShips <= 4 || numRightShips <= 4) {
348
+ battleClock = battleClock + 1;
349
+ if (battleClock>2000){
350
+ endBattle();
351
+ }
352
+ }
353
+
354
+ if (battleEndDelay >= battleEndTimer){
355
+ endBattle();
356
+ }
357
+
358
+ masterBattleClock++;
359
+ if (masterBattleClock >= 8000){
360
+ endBattle();
361
+ }
362
+
363
+ }
364
+ }
365
+
366
+ function endBattle(){
367
+ document.getElementById("victoryDiv").style.visibility = "hidden";
368
+ honorCount = 0;
369
+ battleClock = 0;
370
+ masterBattleClock = 0;
371
+ battleEndDelay = 0;
372
+ battles.splice(0,1);
373
+ }
374
+
375
+ function battleRestart(){
376
+
377
+ numLeftShips = 0;
378
+ numRightShips = 0;
379
+ numShips=0;
380
+ ships = new Array();
381
+ grid = new Array(battleGRID_HEIGHT);
382
+
383
+ //reset the grid
384
+
385
+ var row, col;
386
+ for (row = 0; row < battleGRID_HEIGHT; row++)
387
+ {
388
+ grid[row]= new Array();
389
+ for (col = 0; col < battleGRID_WIDTH; col++)
390
+ {
391
+ grid[row][col]= new Cell();
392
+ }
393
+ }
394
+
395
+
396
+ //create ships... alternate left team and right team so there's no advantage
397
+ //for array position
398
+
399
+ var leftShipTurn = false;
400
+ var i=0;
401
+ while ((numLeftShips<battleLEFTSHIPS)||(numRightShips<battleRIGHTSHIPS)){
402
+ if (leftShipTurn){
403
+ ships[i] = new Ship(0);
404
+ numLeftShips++;
405
+ numShips++;
406
+ if (numRightShips < battleRIGHTSHIPS) leftShipTurn = false;
407
+ }
408
+ else{
409
+ ships[i] = new Ship(1);
410
+ numRightShips++;
411
+ numShips++;
412
+ if (numLeftShips < battleLEFTSHIPS) leftShipTurn = true;
413
+ }
414
+ i++;
415
+ }
416
+ }
417
+
418
+
419
+ var UpdateGrid = function(){
420
+
421
+ //First clear grid out
422
+
423
+ var p;
424
+ var row,col;
425
+ for (row = 0; row < battleGRID_HEIGHT; row++){
426
+ for (col = 0; col < battleGRID_WIDTH; col++){
427
+ grid[row][col].ships.length=0;
428
+ grid[row][col].numShips = 0;
429
+ }
430
+ }
431
+
432
+ //Update Grid cells with ships in each cell
433
+
434
+ for (i = 0; i < numShips; i++){
435
+
436
+ p = ships[i];
437
+ if (!ships[i].alive) continue;
438
+
439
+ //figure out which grid cell the ship is in
440
+
441
+ p.gx = Math.floor(p.x * battleINV_GRID_WIDTH);
442
+ p.gy = Math.floor(p.y * battleINV_GRID_HEIGHT);
443
+
444
+ if (p.gx < 0){
445
+ p.gx = 0;
446
+ }
447
+ if (p.gy < 0){
448
+ p.gy = 0;
449
+ }
450
+ if (p.gx > battleGRID_WIDTH - 1){
451
+ p.gx = battleGRID_WIDTH - 1;
452
+ }
453
+ if (p.gy > battleGRID_HEIGHT - 1){
454
+ p.gy = battleGRID_HEIGHT - 1;
455
+ }
456
+
457
+ grid[p.gy][p.gx].add(p); //add ship to this grid cell
458
+ }
459
+ }
460
+
461
+
462
+ var DoCombat = function(){
463
+
464
+ var pX = probeCombat * probeCombatBaseRate;
465
+
466
+ // if (battleNameFlag == 1){
467
+ // pX = pX*2;
468
+ // }
469
+
470
+ var dX = drifterCombat;
471
+
472
+ var p;
473
+ var row,col,i;
474
+ var numLeftTeam, numRightTeam;
475
+ var diceRoll;
476
+ var ooda = 0;
477
+
478
+ if (attackSpeedFlag == 1){
479
+ ooda = probeSpeed * .2;
480
+ }
481
+
482
+ for (row = 0; row < battleGRID_HEIGHT; row++){
483
+
484
+ for (col = 0; col < battleGRID_WIDTH; col++){
485
+
486
+ //First Check if there are enough ships in this cell to do combat
487
+
488
+ if (grid[row][col].numShips < 2) continue;
489
+
490
+ numLeftTeam=0;
491
+ numRightTeam=0;
492
+
493
+ //Now count how many ships for each team in this cell;
494
+
495
+ for (i=0;i<grid[row][col].numShips;i++){
496
+ p = grid[row][col].ships[i];
497
+ if (!p.alive) continue;
498
+ if (p.team == 0) numLeftTeam++;
499
+ else numRightTeam++;
500
+ }
501
+
502
+ if ((numLeftTeam == 0)||(numRightTeam==0)) continue;
503
+
504
+ //now we have at least one ship of each team in this cell.
505
+ //roll a weighted die to see if each ship gets killed
506
+
507
+ for (i=0;i<grid[row][col].numShips;i++){
508
+ p = grid[row][col].ships[i];
509
+ if (p.team == 0) {
510
+ diceRoll = Math.random() * dX * ((numRightTeam/numLeftTeam)*.5);
511
+ battleDEATH_THRESHOLD = battleDEATH_THRESHOLD + ooda;
512
+ // console.log("Probe Death Check. dX = "+dX+". diceRoll = "+diceRoll+". deathThreshold = "+battleDEATH_THRESHOLD);
513
+ }
514
+ else {
515
+ diceRoll = ((Math.random() * pX) + (probeCombat * .1)) * ((numLeftTeam/numRightTeam)*.5);
516
+ // console.log("Drifter Death Check. pX = "+pX+". diceRoll = "+diceRoll+". deathThreshold = "+battleDEATH_THRESHOLD);
517
+ }
518
+ if (diceRoll > battleDEATH_THRESHOLD) {
519
+ p.alive = false;
520
+ if (p.team==0) {
521
+ numLeftShips--;
522
+ if (unitSize>probeCount){unitSize=probeCount;}
523
+ probeCount = probeCount - unitSize;
524
+ probesLostCombat = probesLostCombat + unitSize;
525
+ document.getElementById('probesLostCombatDisplay').innerHTML = numberCruncher(probesLostCombat);
526
+ } else {
527
+ numRightShips--;
528
+ if (unitSize>drifterCount){unitSize=drifterCount;}
529
+ drifterCount = drifterCount - unitSize;
530
+ driftersKilled = driftersKilled + unitSize;
531
+ document.getElementById('driftersKilled').innerHTML = numberCruncher(driftersKilled);
532
+ document.getElementById('drifterCount').innerHTML = numberCruncher(drifterCount);
533
+ }
534
+ }
535
+
536
+ battleDEATH_THRESHOLD = .5;
537
+
538
+ }
539
+ }
540
+ }
541
+
542
+ checkForBattleEnd();
543
+
544
+ }
545
+
546
+ var MoveShips = function(){
547
+
548
+ var i, p;
549
+ var centroid = FindCentroid();
550
+
551
+ for (i = 0; i < numShips; i++){
552
+ p = ships[i];
553
+ if (!p.alive) {
554
+ if (p.framesDead<10){
555
+ //draw explosion
556
+ context.fillStyle=battleEXPLODECOLOR;
557
+ if (p.framesDead<1){
558
+ context.fillRect(p.x -3, p.y -3,7,7); //big square for one frame
559
+ }
560
+ else if (p.framesDead<2){
561
+ context.fillRect(p.x -1, p.y -1,3,3); //little square for 1 frame
562
+ }
563
+ //4 little pixel squares moving out from the point of explosion
564
+ context.fillRect(p.x + p.framesDead, p.y + p.framesDead,1,1);
565
+ context.fillRect(p.x - p.framesDead, p.y + p.framesDead,1,1);
566
+ context.fillRect(p.x + p.framesDead, p.y - p.framesDead,1,1);
567
+ context.fillRect(p.x - p.framesDead, p.y - p.framesDead,1,1);
568
+ p.framesDead++;
569
+ }
570
+ }
571
+ else {
572
+ MoveSingleShip(p,centroid);
573
+ context.fillStyle = p.color;
574
+ context.fillRect(p.x - 1, p.y - 1, 2, 2);
575
+ }
576
+
577
+
578
+ }
579
+
580
+ }
581
+
582
+ var FindCentroid=function(){
583
+
584
+ //find the statistical center of all the ships
585
+
586
+ var i, p;
587
+ var centroid = { x: 0, y: 0};
588
+ var shipsAlive = 0;
589
+
590
+ for (i = 0; i < numShips; i++){
591
+ if (!ships[i].alive) continue;
592
+ centroid.x += ships[i].x;
593
+ centroid.y += ships[i].y;
594
+ shipsAlive++;
595
+ }
596
+
597
+ centroid.x /= shipsAlive;
598
+ centroid.y /= shipsAlive;
599
+
600
+ //give some tendency to center, so they bunch in the middle
601
+ centroid.x = (centroid.x * 0.8) + (battleWIDTH/2 * 0.2);
602
+ centroid.y = (centroid.y * 0.8) + (battleHEIGHT/2 * 0.2);
603
+ return centroid;
604
+ }
605
+
606
+ var MoveSingleShip = function(p, centroid){
607
+
608
+ //accelerate to group centroid
609
+ p.vx += (centroid.x-p.x)*0.001;
610
+ p.vy += (centroid.y-p.y)*0.001;
611
+
612
+ //accelerate to enemy ships in adjacent grid cells
613
+ var row,col,i;
614
+ var othership;
615
+ var teammatesConsidered=0;
616
+ for (row=Math.max(p.gy-1,0); row<Math.min(p.gy+2,battleGRID_HEIGHT);row++){
617
+ for(col=Math.max(p.gx-1,0);col<Math.min(p.gx+2,battleGRID_WIDTH);col++){
618
+ if (grid[row][col].ships.length<2) continue;
619
+ for (i=0;i<grid[row][col].ships.length; i++){
620
+ othership = grid[row][col].ships[i];
621
+ if (!othership.alive) continue; //ignore dead ships
622
+ if (othership.team == p.team) {
623
+ teammatesConsidered++;
624
+
625
+ if (teammatesConsidered>3) continue;//don't fixate on teammates
626
+
627
+ //mild acceleration to match teammates
628
+ p.vx += othership.vx * 0.01;
629
+ p.vy += othership.vy * 0.01;
630
+
631
+ //mild acceleration to get space from teammates
632
+ p.vx -= (othership.x - p.x) * .1;
633
+ p.vy -= (othership.y - p.y) * .1;
634
+ }
635
+ else {
636
+ p.vx += othership.vx * 0.2;
637
+ p.vy += othership.vy * 0.2;
638
+ p.vx += (othership.x - p.x) *0.2; // acceleration toward enemies
639
+ p.vy += (othership.y - p.y) * 0.2;
640
+ }
641
+ }
642
+ }
643
+ }
644
+
645
+ //limit speed to max
646
+ if (Math.abs(p.vx) > battleMAXSPEED) p.vx = p.vx < 0 ? -battleMAXSPEED : battleMAXSPEED;
647
+ if (Math.abs(p.vy) > battleMAXSPEED) p.vy = p.vy < 0 ? -battleMAXSPEED : battleMAXSPEED;
648
+
649
+ //move the ship
650
+ /*p.vx += Math.random() * .1;
651
+ p.vx += Math.random() * .1;
652
+ p.vx -= Math.random() * .1;
653
+ p.vx -= Math.random() * .1;*/
654
+
655
+ p.x += p.vx;
656
+ p.y += p.vy;
657
+
658
+ //bounce off edges
659
+ if (p.x > battleWIDTH) {
660
+ p.x = battleWIDTH;
661
+ p.vx = -battleMAXSPEED;
662
+ }
663
+ else if (p.x < 0){
664
+ p.x = 0;
665
+ p.vx = battleMAXSPEED;
666
+ }
667
+ if (p.y > battleHEIGHT) {
668
+ p.y = battleHEIGHT;
669
+ p.vy = -battleMAXSPEED;
670
+ }
671
+ else if (p.y < 0){
672
+ p.y = 0;
673
+ p.vy = battleMAXSPEED;
674
+ }
675
+ }
676
+
677
+ //Clear the screen,
678
+ // var MouseDown = function(e) {
679
+ // e.preventDefault();
680
+ // battleRestart();
681
+ // }
682
+
683
+ var ClearFrame = function(){
684
+ canvas.width = canvas.width
685
+
686
+ // var ctx = canvas.getContext("2d");
687
+ // ctx.font = "16px Times";
688
+ // ctx.fillStyle="white";
689
+ // ctx.fillText("Combat",10,20);
690
+
691
+
692
+ }
693
+ }
694
+
695
+ //Grid cells for quicker grouping
696
+ function Cell(){
697
+ this.ships = new Array();
698
+ this.numShips = 0;
699
+ this.add = function(ship){
700
+ this.ships[this.numShips++] = ship;
701
+ }
702
+ }
703
+
704
+ function Ship(team){
705
+
706
+ this.alive = true;
707
+ this.color;
708
+ this.team = team;
709
+ this.framesDead = 0;
710
+ //grid cell coord - we'll set this later
711
+ this.gx = 0;
712
+ this.gy = 0;
713
+
714
+ switch(team){
715
+
716
+ case 0:
717
+ this.x = (Math.random()*0.2) * battleWIDTH;
718
+ this.y = Math.random() * battleHEIGHT;
719
+ this.vx = Math.random() * battleMAXSPEED;
720
+ this.vy = Math.random()-0.5;
721
+ this.color = battleLEFTCOLOR;
722
+ break;
723
+ case 1:
724
+ this.x = (Math.random()*0.2+0.8) * battleWIDTH;
725
+ this.y = Math.random() * battleHEIGHT;
726
+ this.vx = -1* Math.random() * battleMAXSPEED;
727
+ this.vy =Math.random()-0.5;
728
+ this.color = battleRIGHTCOLOR;
729
+ break;
730
+ }
731
+
732
+ }
733
+
734
+ function createBattle(){
735
+
736
+ unitSize = 0;
737
+
738
+ if (drifterCount>=probeCount) {
739
+ unitSize = probeCount/100;
740
+ } else {
741
+ unitSize = drifterCount/100;
742
+ }
743
+
744
+ if (unitSize<1) {unitSize=1};
745
+ document.getElementById('battleScale').innerHTML = numberCruncher(unitSize,0);
746
+
747
+
748
+ var rr = Math.random()*drifterCount;
749
+ if (rr<1){rr=1};
750
+ var ss = Math.random()*probeCount;
751
+ if (ss<1){ss=1};
752
+ var tt = Math.random()*availableMatter;
753
+
754
+ battleID++;
755
+
756
+ var newBattle = {
757
+ id: battleID,
758
+ clipProbes: ss,
759
+ drifterProbes: rr,
760
+ victory: false,
761
+ loss: false,
762
+ whiteFlag: 0,
763
+ territory: tt,
764
+ reportCount: 0,
765
+ garbageFlag: 0,
766
+ }
767
+
768
+ battleLEFTSHIPS = Math.ceil(ss / 1000000);
769
+ if (battleLEFTSHIPS>200){
770
+ battleLEFTSHIPS=200;
771
+ } if (battleLEFTSHIPS == 200){
772
+ var hinder = Math.random();
773
+ if (hinder < .50) {
774
+ battleLEFTSHIPS = Math.ceil(Math.random()*175);
775
+ }
776
+ }
777
+
778
+ battleRIGHTSHIPS = Math.ceil(rr / 1000000);
779
+ if (battleRIGHTSHIPS>200){
780
+ battleRIGHTSHIPS=200;
781
+ }
782
+
783
+ Battle();
784
+
785
+ battleName = ("Drifter Attack "+newBattle.id);
786
+
787
+ if (battleNameFlag == 1){
788
+ battleName = generateBattleName();
789
+ }
790
+
791
+ document.getElementById('battleName').innerHTML = battleName;
792
+
793
+ battles.push(newBattle);
794
+
795
+ // battleWrite(newBattle);
796
+
797
+ }
798
+
799
+ var app = new Battle();
800
+ app.initialize();
801
+
802
+