forked from eli-schwartz/cookieclicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascendCalibrator.js
613 lines (563 loc) · 15.6 KB
/
ascendCalibrator.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
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
/*
05/08/2022: wrote a system that lets me edit heavenly upgrade positioning on the ascend screen more easily
*/
(function(){
let l=function(what){return document.getElementById(what);}
Dist=function(x1,y1,x2,y2)
{
var a=x1-x2;var b=y1-y2;
return Math.sqrt(a*a+b*b);
}
Angle=function(x1,y1,x2,y2)
{
return Math.atan2(y2-y1,x2-x1);
}
let isOn=false;
let nodes=[];
let nodeEls=[];
let nodesByEl={};
let nodesById={};
//custom interface
let isOnCondition=()=>(Game.DebuggingPrestige && Game.OnAscend==1);
let getNodes=()=>
{
let list=[];
let els=l('ascendUpgrades').querySelector('.crateBox').querySelectorAll('.crate.upgrade.heavenly');
for (var i=0;i<els.length;i++)
{
let el=els[i];
let obj=Game.UpgradesById[parseInt(el.dataset.id)];
let node={id:list.length,el:el,obj:obj,x:obj.posX,y:obj.posY,parents:[],children:[]};
node.prevx=node.x;node.prevy=node.y;
list.push(node);
nodeEls.push(el);
nodesByEl[el.id]=node;
nodesById[node.id]=node;
}
for (var i=0;i<list.length;i++)
{
let me=list[i];
me.el.onclick='';
me.el.onmouseover='';
for (var ii=0;ii<me.obj.parents.length;ii++)
{
let parent=list.find(it=>it.obj.id==me.obj.parents[ii].id);
me.parents.push(parent);
parent.children.push(me);
}
}
return list;
}
let updateNode=(node)=>
{
let me=node.obj;
me.posX=Math.floor(node.x);
me.posY=Math.floor(node.y);
node.el.style.left=Math.floor(me.posX)+'px';
node.el.style.top=Math.floor(me.posY)+'px';
for (var i in me.parents)
{
var origX=0;
var origY=0;
var targX=me.posX+28;
var targY=me.posY+28;
if (me.parents[i]!=-1) {origX=me.parents[i].posX+28;origY=me.parents[i].posY+28;}
var rot=-(Math.atan((targY-origY)/(origX-targX))/Math.PI)*180;
if (targX<=origX) rot+=180;
var dist=Math.floor(Math.sqrt((targX-origX)*(targX-origX)+(targY-origY)*(targY-origY)));
l('heavenlyLink'+me.id+'-'+i).style='width:'+dist+'px;transform:rotate('+rot+'deg);left:'+(origX)+'px;top:'+(origY)+'px;';
}
}
let onMove=()=>
{
Game.tooltip.hide();
}
let transformMouseCoords=(x,y)=>
{
x=x*(1/Game.AscendZoomT);
y=y*(1/Game.AscendZoomT);
return [x,y];
}
let transformPivotCoords=(x,y)=>
{
let box=l('ascendZoomable').getBoundingClientRect();
x=(x+Game.AscendOffX)*(Game.AscendZoom)+box.left;
y=(y+Game.AscendOffY)*(Game.AscendZoom)+box.top;
return [x,y];
}
let untransformPivotCoords=(x,y)=>
{
let box=l('ascendZoomable').getBoundingClientRect();
x-=box.left;
y-=box.top;
x/=Game.AscendZoom;
y/=Game.AscendZoom;
x-=Game.AscendOffX;
y-=Game.AscendOffY;
return [x,y];
}
let nodeOffset=(x,y)=>
{
return [x+30,y+30];
}
let nodeUnOffset=(x,y)=>
{
return [x-30,y-30];
}
let exportData=(nodes)=>
{
var str='';
for (var i=0;i<nodes.length;i++)
{
let node=nodes[i];
let me=node.obj;
if (me.placedByCode) continue;
str+=me.id+':['+Math.floor(me.posX)+','+Math.floor(me.posY)+'],';
}
return 'Game.UpgradePositions={'+str+'};';
}
//end custom interface
let toStr=()=>
{
var o={};
for (var i=0;i<nodes.length;i++)
{
let node=nodes[i];
o[node.id]={x:node.x,y:node.y};
}
return JSON.stringify(o);
}
let fromStr=(str)=>
{
var o=JSON.parse(str);
for (var i=0;i<nodes.length;i++)
{
let node=nodes[i];
if (o[node.id])
{
node.x=o[node.id].x;
node.y=o[node.id].y;
node.prevx=node.x;node.prevy=node.y;
}
}
updatePositions();
}
let undoSteps=[];
let redoSteps=[];
let prevState=0;
let saveStep=()=>{
redoSteps=[];
undoSteps.unshift([prevState,toStr()]);
prevState=toStr();
}
let undo=()=>{
if (undoSteps.length==0) return;
let step=undoSteps[0];
undoSteps.shift();
redoSteps.unshift(step);
fromStr(step[0]);
prevState=toStr();
}
let redo=()=>{
if (redoSteps.length==0) return;
let step=redoSteps[0];
redoSteps.shift();
undoSteps.unshift(step);
fromStr(step[1]);
prevState=toStr();
}
let updatePositions=()=>
{
for (var i=0;i<nodes.length;i++)
{
let node=nodes[i];
updateNode(node);
}
for (var i=0;i<nodes.length;i++)//do it twice to take care of connection lines etc
{
let node=nodes[i];
updateNode(node);
}
l('calibratorExport').value=exportData(nodes);
}
let hasAddedListeners=false;
let selectedNodes=[];
let rootEl=0;
let pivotEl=0;
let pivot=0;
let getPivot=()=>
{
if (Array.isArray(pivot)) return pivot;
else if (typeof pivot==='object') return nodeOffset(pivot.prevx,pivot.prevy);
else return [0,0];
}
let setup=function()
{
let el=document.createElement('div');
el.id="calibrator";
el.style.cssText='z-index:100000000000;position:absolute;left:0px;top:0px;right:0px;bottom:0px;pointer-events:none;';
el.innerHTML=`
<div style="position:absolute;left:4px;bottom:44px;text-align:left;color:#fff;text-shadow:0px 1px 1px #000;line-height:125%;">
<button id="calibratorEvenFromPivotButton">Even from pivot</button><br>
<button id="calibratorEvenFromOthersButton">Even from each other</button><br>
Use:<br>
-select nodes by clicking; select multiple with Shift<br>
-move selection by dragging<br>
-set pivot by ctrl-clicking anywhere<br>
-move selection around pivot by right-click-dragging<br>
-undo/redo with ctrl-z and ctrl-y<br>
<input type="text" style="pointer-events:auto;display:inline-block;min-width:200px;max-width:100%;" id="calibratorExport"/></div>
</div>
<div id="calibratorPivot"></div>
`;
document.body.appendChild(el);
if (!l('calibratorStyle'))
{
var css=`
#calibrator button
{
pointer-events:auto;
border:1px solid #000;
box-shadow:1px 1px 0px #fff inset,-1px -1px 0px #999 inset;
background:#ccc;
color:#000;
cursor:pointer;
margin:0px 2px 2px 0px;
}
#calibrator button:hover{filter:brightness(150%);}
#calibratorPivot
{
width:64px;
height:64px;
background:rgba(255,255,255,0.1);
box-shadow:0px 0px 16px 4px #6cf;
border-radius:100%;
position:absolute;
left:0px;top:0px;
margin-left:-32px;
margin-top:-32px;
animation:calibratorPing 0.5s infinite;
}
@keyframes calibratorPing
{
from {transform:scale(1.3);}
to {transform:scale(1);}
}
`;
var style=document.createElement('style');
style.type='text/css';
style.appendChild(document.createTextNode(css));
style.id="calibratorStyle";
document.head.appendChild(style);
}
l('calibratorEvenFromPivotButton').addEventListener('click',()=>{
//sets all selected nodes at equal distance and angles from the pivot
//probably an easier way of doing this
if (selectedNodes.length<2) return;
let [fromX,fromY]=getPivot();
let dist=0;
let nodeAngles=[];
for (var i=0;i<selectedNodes.length;i++)
{
let node=selectedNodes[i];
let [prevx,prevy]=nodeOffset(node.prevx,node.prevy);
let nodeDist=Dist(prevx,prevy,fromX,fromY);
let nodeAngle=(-Angle(prevx,prevy,fromX,fromY)-Math.PI/2+Math.PI*2)%(Math.PI*2);
dist+=nodeDist/selectedNodes.length;
nodeAngles.push({node:node,angle:nodeAngle});
}
//find start angle
nodeAngles.sort((a,b)=>(b.angle-a.angle));
var startNode=0;
var maxAngle=0;
for (var i=0;i<nodeAngles.length;i++)
{
let prev=nodeAngles[i==0?(nodeAngles.length-1):(i-1)];
let a=(nodeAngles[i].angle-prev.angle+Math.PI*2)%(Math.PI*2)-Math.PI*2;
nodeAngles[i].angleD=a;
if (a<maxAngle){maxAngle=a;startNode=nodeAngles[i];}
}
nodeAngles.sort((a,b)=>(b.angleD-a.angleD));
let angleInc=0;
for (var i=0;i<nodeAngles.length-1;i++)
{angleInc+=nodeAngles[i].angleD;}
angleInc/=nodeAngles.length-1;
let angleMin=startNode.angle;
nodeAngles.sort((a,b)=>(b.angle-a.angle));
var ind=nodeAngles.indexOf(startNode);
nodeAngles=nodeAngles.slice(ind,nodeAngles.length).concat(nodeAngles.slice(0,ind));
for (var i=0;i<nodeAngles.length;i++)
{
let node=nodeAngles[i].node;
let [prevx,prevy]=nodeOffset(node.prevx,node.prevy);
let nodeDist=dist;
let nodeAngle=angleMin+angleInc*i;
let x=fromX+Math.sin(nodeAngle)*(nodeDist);
let y=fromY+Math.cos(nodeAngle)*(nodeDist);
[x,y]=nodeUnOffset(x,y);
node.x=x;node.y=y;
node.prevx=node.x;node.prevy=node.y;
}
updatePositions();
saveStep();
});
l('calibratorEvenFromOthersButton').addEventListener('click',()=>{
//equalizes all distances and angles between a chain of nodes
if (selectedNodes.length<3) return;
//find roots
let roots=[];
let chains={};
for (var i=0;i<selectedNodes.length;i++)
{
let isInChain=false;
let node=selectedNodes[i];
for (var ii=0;ii<node.parents.length;ii++)
{
if (selectedNodes.indexOf(node.parents[ii])!=-1) isInChain=true;
}
if (!isInChain) {roots.push(node);}
}
//make chains
for (var i=0;i<roots.length;i++)
{
let node=roots[i];
chains[node.id]=[node];
let addChildren=(me)=>{
for (var ii=0;ii<me.children.length;ii++)
{
if (selectedNodes.indexOf(me.children[ii])!=-1) {chains[node.id].push(me.children[ii]);addChildren(me.children[ii]);break;}
}
};
addChildren(node,chains[node.id]);
}
for (var i in chains)
{
if (chains[i].length<3) continue;
let dist=0;
let nodeAngles=[];
nodeAngles.push({node:chains[i][0],angle:0,angleD:0});
for (var ii=1;ii<chains[i].length;ii++)
{
let node=chains[i][ii];
let [fromX,fromY]=nodeOffset(chains[i][ii-1].prevx,chains[i][ii-1].prevy);
let [prevx,prevy]=nodeOffset(node.prevx,node.prevy);
let nodeDist=Dist(prevx,prevy,fromX,fromY);
let nodeAngle=(-Angle(prevx,prevy,fromX,fromY)-Math.PI/2+Math.PI*2)%(Math.PI*2);
dist+=nodeDist;
nodeAngles.push({node:node,angle:nodeAngle,angleD:0});
}
dist/=(chains[i].length-1);
for (var ii=1;ii<nodeAngles.length;ii++)
{
let prev=nodeAngles[ii-1];
let a=(nodeAngles[ii].angle-prev.angle+Math.PI*2)%(Math.PI*2);
if (a>=Math.PI) a-=Math.PI*2;
nodeAngles[ii].angleD=a;
}
var startNode=nodeAngles[1];
let angleInc=0;
for (var ii=2;ii<nodeAngles.length;ii++)
{angleInc+=nodeAngles[ii].angleD;}
angleInc/=nodeAngles.length-2;
for (var ii=1;ii<nodeAngles.length;ii++)
{
let node=nodeAngles[ii].node;
let [fromX,fromY]=nodeOffset(chains[i][ii-1].prevx,chains[i][ii-1].prevy);
let [prevx,prevy]=nodeOffset(node.prevx,node.prevy);
let nodeDist=dist;
let nodeAngle=startNode.angle+angleInc*(ii-1);
let x=fromX+Math.sin(nodeAngle)*(nodeDist);
let y=fromY+Math.cos(nodeAngle)*(nodeDist);
[x,y]=nodeUnOffset(x,y);
node.x=x;node.y=y;
node.prevx=node.x;node.prevy=node.y;
}
//todo: ideally the first and last nodes don't move
}
updatePositions();
saveStep();
});
nodes=getNodes();
prevState=toStr();
l('calibratorExport').value=exportData(nodes);
rootEl=el;
pivotEl=l('calibratorPivot');
let clickFromX=0,clickFromY=0,clickFromAngle=0,clickFromDist=0;
let clickOffX=0,clickOffY=0;
let isClicking=false;
let isRClicking=false;
let isDragging=false;
let isPivoting=false;
selectNode=(node)=>
{
node.el.style.filter='brightness(150%) drop-shadow(0px 0px 8px #6cf)';
}
deselectNode=(node)=>
{
node.el.style.filter='';
}
if (!hasAddedListeners)
{
document.addEventListener('contextmenu',e=>{
if (!e.ctrlKey) e.preventDefault();
});
document.addEventListener('mousedown',e=>{
if (!isOn) return;
clickFromX=e.clientX;
clickFromY=e.clientY;
clickOffX=0;clickOffY=0;
if (nodeEls.indexOf(e.target)!=-1 && nodesByEl[e.target.id])
{
let node=nodesByEl[e.target.id];
if (e.button==0 && !e.ctrlKey)
{
//select and deselect
if (e.shiftKey)
{
if (selectedNodes.indexOf(node)!=-1) selectedNodes.splice(selectedNodes.indexOf(node),1);
else selectedNodes.push(node);
}
else
{
if (selectedNodes.indexOf(node)!=-1){}
else
{
for (var i=0;i<selectedNodes.length;i++){deselectNode(selectedNodes[i]);}
selectedNodes=[node];
}
}
if (selectedNodes.indexOf(node)!=-1) selectNode(node);
else deselectNode(node);
isClicking=true;
}
else if (e.button==0 && e.ctrlKey)
{
//set pivot on node
pivot=node;
}
else if (e.button==2)
{
isRClicking=true;
}
e.preventDefault();
e.stopPropagation();
}
else
{
if (e.button==0 && e.ctrlKey)
{
//set pivot on point
pivot=untransformPivotCoords(clickFromX,clickFromY);
}
}
},true);
document.addEventListener('mousemove',e=>{
if (!isOn) return;
if (!isDragging && isClicking && Dist(clickFromX,clickFromY,e.clientX,e.clientY)>=5) isDragging=true;
if (!isPivoting && isRClicking && Dist(clickFromX,clickFromY,e.clientX,e.clientY)>=5)
{
//pivot
isPivoting=true;
let [fromX,fromY]=getPivot();
let [toX,toY]=untransformPivotCoords(clickFromX,clickFromY);
clickFromAngle=-Angle(fromX,fromY,toX,toY)+Math.PI/2;
clickFromDist=Dist(fromX,fromY,toX,toY);
}
if (isDragging && selectedNodes.length>0)
{
[clickOffX,clickOffY]=transformMouseCoords(e.clientX-clickFromX,e.clientY-clickFromY);
for (var i=0;i<selectedNodes.length;i++)
{
let node=selectedNodes[i];
node.x=node.prevx+clickOffX;
node.y=node.prevy+clickOffY;
}
updatePositions();
}
else if (isPivoting && selectedNodes.length>0)
{
[clickOffX,clickOffY]=transformMouseCoords(e.clientX-clickFromX,e.clientY-clickFromY);
let [fromX,fromY]=getPivot();
let [toX,toY]=untransformPivotCoords(e.clientX,e.clientY);
let angle=clickFromAngle-(-Angle(fromX,fromY,toX,toY)+Math.PI/2);
let dist=Dist(fromX,fromY,toX,toY)/clickFromDist;
for (var i=0;i<selectedNodes.length;i++)
{
let node=selectedNodes[i];
let [prevx,prevy]=nodeOffset(node.prevx,node.prevy);
let nodeDist=Dist(prevx,prevy,fromX,fromY);
let nodeAngle=-Angle(prevx,prevy,fromX,fromY)-Math.PI/2;
let x=fromX+Math.sin(nodeAngle-angle)*(nodeDist*dist);
let y=fromY+Math.cos(nodeAngle-angle)*(nodeDist*dist);
[x,y]=nodeUnOffset(x,y);
node.x=x;node.y=y;
}
updatePositions();
}
onMove();
});
document.addEventListener('mouseup',e=>{
if (!isOn) return;
if (selectedNodes.length>0)
{
for (var i=0;i<selectedNodes.length;i++)
{
let node=selectedNodes[i];
node.prevx=node.x;node.prevy=node.y;
}
e.preventDefault();
e.stopPropagation();
}
//for (var i=0;i<selectedNodes.length;i++){deselectNode(selectedNodes[i]);}
//selectedNodes=[];
if (isDragging || isPivoting) saveStep();
isClicking=false;
isRClicking=false;
isDragging=false;
isPivoting=false;
});
document.addEventListener('click',e=>{
if (!isOn) return;
if (selectedNodes.length>0)
{
e.preventDefault();
e.stopPropagation();
}
});
document.addEventListener('keydown',e=>{
if (!isOn) return;
if (e.ctrlKey && e.key=='z') undo();
else if (e.ctrlKey && e.key=='y') redo();
});
hasAddedListeners=true;
}
}
let reset=function()
{
if (rootEl) {rootEl.remove();rootEl=0;}
nodes=[];
nodeEls=[];
nodesByEl={};
}
let loop=function()
{
let oldIsOn=isOn;
isOn=isOnCondition();
if (oldIsOn!=isOn)
{
if (isOn) setup();
else reset();
}
setTimeout(loop,1000);
}
let draw=function()
{
let [x,y]=transformPivotCoords(...getPivot());
pivotEl.style.left=x+'px';
pivotEl.style.top=y+'px';
setTimeout(draw,1000/30);
}
reset();
loop();
draw();
})();