forked from cameroncondry/cbc-kitten-scientists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitten-scientists.user.js
1817 lines (1493 loc) · 70.2 KB
/
kitten-scientists.user.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name Kitten Scientists
// @namespace http://www.reddit.com/r/kittensgame/comments/34gb2u/kitten_scientists_automation_script/
// @description Launch Kitten Scientists
// @include *bloodrizer.ru/games/kittens/*
// @include file:///*kitten-game*
// @version 1.3.2
// @grant none
// @copyright 2015, cameroncondry
// ==/UserScript==
// ==========================================
// Begin Kitten Scientist's Automation Engine
// ==========================================
var version = 'Kitten Scientists version 1.3.2';
var address = '1AQ1AC9W5CEAPgG5739XGXC5vXqyafhoLp';
// Game will be referenced in loadTest function
var game = null;
var run = function() {
var options = {
// When debug is enabled, messages that go to the game log are also logged using window.console.
debug: true,
// The interval at which the internal processing loop is run, in milliseconds.
interval: 2000,
// The default color for KS messages in the game log (like enabling and disabling items).
msgcolor: '#aa50fe', // dark purple
// The color for activity summaries.
summarycolor: '#009933', // light green
// The color for log messages that are about activities (like festivals and star observations).
activitycolor: '#E65C00', // orange
// Should activity be logged to the game log?
showactivity: true,
// The default consume rate.
consume: 0.6,
// How many messages to keep in the game log.
logMessages: 100,
// The default settings for game automation.
auto: {
// Settings related to KS itself.
engine: {
// Should any automation run at all?
enabled: false
},
faith: {
// Should praising be automated?
enabled: true,
// At what percentage of the faith storage capacity should KS praise the sun?
trigger: 0.99
},
festival: {
// Should festivals be held automatically?
enabled: true
},
hunt: {
// Should hunters be sent on hunts automatically?
enabled: true,
// At what percentage of the catpower storage capacity should KS send hunters on hunts?
trigger: 0.6
},
build: {
// Should buildings be built automatically?
enabled: true,
// When a building requires a certain resource (this is what their *require* property refers to), then
// this is the percentage of the storage capacity of that resource, that has to be met for the building
// to be built.
trigger: 0.75,
// The items that be automatically built.
// Every item can define a required resource. This resource has to be available at a certain capacity for
// the building to be built. The capacity requirement is defined by the trigger value set for the section.
//
// Additionally, for upgradeable buildings, the item can define which upgrade stage it refers to.
// For upgraded buildings, the ID (or internal name) of the building can be controlled through the *name*
// property. For other buildings, the key of the item itself is used.
items: {
// housing
hut: {require: 'wood', enabled: false},
logHouse: {require: 'minerals', enabled: false},
mansion: {require: 'titanium', enabled: false},
// craft bonuses
workshop: {require: 'minerals', enabled: true},
factory: {require: 'titanium', enabled: true},
// production
field: {require: 'catnip', enabled: true},
pasture: {require: 'catnip', enabled: true, stage: 0},
solarFarm: {require: 'titanium', enabled: true, stage: 1, name: 'pasture'},
mine: {require: 'wood', enabled: true},
lumberMill: {require: 'minerals', enabled: true},
aqueduct: {require: 'minerals', enabled: true, stage: 0},
hydroPlant: {require: 'titanium', enabled: true, stage: 1, name: 'aqueduct'},
oilWell: {require: 'coal', enabled: true},
quarry: {require: 'coal', enabled: true},
// conversion
smelter: {require: 'minerals', enabled: true},
biolab: {require: 'science', enabled: false},
calciner: {require: 'titanium', enabled: false},
reactor: {require: 'titanium', enabled: false},
accelerator: {require: 'titanium', enabled: false},
steamworks: {require: false, enabled: false},
magneto: {require: false, enabled: false},
// science
library: {require: 'wood', enabled: true},
academy: {require: 'wood', enabled: true},
observatory: {require: 'iron', enabled: true},
// other
amphitheatre: {require: 'minerals', enabled: true, stage: 0},
broadcastTower: {require: 'titanium', enabled: true, stage: 1, name: 'amphitheatre'},
tradepost: {require: 'gold', enabled: true},
chapel: {require: 'minerals', enabled: true},
temple: {require: 'gold', enabled: true},
mint: {require: false, enabled: false},
unicornPasture: {require: false, enabled: true},
ziggurat: {require: false, enabled: true},
chronosphere: {require: 'unobtainium', enabled: true},
// storage
barn: {require: 'wood', enabled: true},
harbor: {require: false, enabled: false},
warehouse: {require: false, enabled: false}
}
},
space: {
// Should space buildings be built automatically?
enabled: false,
// The functionality of the space section is identical to the build section. It just needs to be treated
// seperately, because the game internals are slightly different.
trigger: 0.95,
items: {
// Cath
spaceElevator: {require: 'unobtainium', enabled: false},
sattelite: {require: 'titanium', enabled: false},
spaceStation: {require: 'oil', enabled: false},
// Moon
moonOutpost: {require: 'uranium', enabled: false},
moonBase: {require: 'unobtainium', enabled: false},
// Dune
planetCracker: {require: 'science', enabled: false},
hydrofracturer: {require: 'science', enabled: false},
spiceRefinery: {require: 'science', enabled: false},
// Piscine
researchVessel: {require: 'titanium', enabled: false},
orbitalArray: {require: 'eludium', enabled: false},
// Helios
sunlifter: {require: 'eludium', enabled: false},
containmentChamber: {require: 'science', enabled: false},
// T-Minus
cryostation: {require: 'eludium', enabled: false},
// Kairo
spaceBeacon: {require: 'antimatter', enabled: false},
// Yarn
terraformingStation: {require: 'antimatter', enabled: false},
hydroponics: {require: 'kerosene', enabled: false},
// Centaurus
tectonic: {require: 'antimatter', enabled: false}
}
},
craft: {
// Should resources be crafted automatically?
enabled: true,
// Every item can define a required resource with the *require* property.
// At what percentage of the storage capacity of that required resource should the listed resource be crafted?
trigger: 0.95,
// The items that can be crafted.
// In addition to the *require* property, which is explained above, items can also define a *max*. If they
// do, no more than that resource will be automatically produced. This feature can not be controlled through
// the UI and is not used for any resource by default.
// The *limited* property tells KS to only craft the resource once per season.
items: {
wood: {require: 'catnip', max: 0, limited: false, enabled: true},
beam: {require: 'wood', max: 0, limited: false, enabled: true},
slab: {require: 'minerals', max: 0, limited: false, enabled: true},
steel: {require: 'coal', max: 0, limited: false, enabled: true},
plate: {require: 'iron', max: 0, limited: false, enabled: true},
alloy: {require: 'titanium', max: 0, limited: true, enabled: false},
concrete: {require: false, max: 0, limited: true, enabled: false},
gear: {require: false, max: 0, limited: true, enabled: false},
scaffold: {require: false, max: 0, limited: true, enabled: false},
ship: {require: false, max: 0, limited: true, enabled: false},
tanker: {require: false, max: 0, limited: true, enabled: false},
parchment: {require: false, max: 0, limited: true, enabled: true},
manuscript: {require: 'culture', max: 0, limited: true, enabled: true},
compendium: {require: 'science', max: 0, limited: true, enabled: true},
blueprint: {require: 'science', max: 0, limited: true, enabled: false},
kerosene: {require: 'oil', max: 0, limited: true, enabled: false},
megalith: {require: false, max: 0, limited: true, enabled: false},
eludium: {require: 'unobtainium', max: 0, limited: true, enabled: false},
thorium: {require: 'uranium', max: 0, limited: true, enabled: false}
}
},
trade: {
// Should KS automatically trade?
enabled: true,
// Every trade can define a required resource with the *require* property.
// At what percentage of the storage capacity of that required resource should the trade happen?
trigger: 0.95,
// Trades can be limited to only happen during specific seasons. This is because trades with certain races
// are more effective during specific seasons.
// The *allowcapped* property allows us to trade even if the sold resources are at their cap.
items: {
dragons: {enabled: false, require: 'titanium', allowcapped: false,
summer: true, autumn: true, winter: true, spring: true},
zebras: {enabled: true, require: false, allowcapped: false,
summer: true, autumn: true, winter: true, spring: true},
lizards: {enabled: false, require: 'minerals', allowcapped: false,
summer: true, autumn: false, winter: false, spring: false},
sharks: {enabled: false, require: 'iron', allowcapped: false,
summer: false, autumn: false, winter: true, spring: false},
griffins: {enabled: false, require: 'wood', allowcapped: false,
summer: false, autumn: true, winter: false, spring: false},
nagas: {enabled: false, require: false, allowcapped: false,
summer: false, autumn: false, winter: false, spring: true},
spiders: {enabled: false, require: false, allowcapped: false,
summer: false, autumn: true, winter: false, spring: false},
leviathans: {enabled: false, require: 'unobtainium', allowcapped: true,
summer: true, autumn: true, winter: true, spring: true}
}
},
resources: {
furs: {stock: 1000},
unobtainium: {consume: 1.0}
}
}
};
// GameLog Modification
// ====================
// Add a message filter for trades
if (!game.console.filters.trade){
game.console.filters.trade = {
title: "Trades",
enabled: true,
unlocked: true
};
game.ui.renderFilters();
}
// Increase messages displayed in log
game.console.maxMessages = 1000;
var printoutput = function (args) {
var color = args.pop();
args[1] = args[1] || 'ks-default';
// update the color of the message immediately after adding
var msg = game.msg.apply(game, args);
$(msg.span).css('color', color);
// if (options.debug && console) console.log(args);
};
// Used for option change messages and other special notifications
var message = function () {
var args = Array.prototype.slice.call(arguments);
args.push('ks-default');
args.push(options.msgcolor);
printoutput(args);
};
var activity = function () {
if (options.showactivity) {
var args = Array.prototype.slice.call(arguments);
var activityClass = args.length > 1 ? ' type_' + args.pop() : '';
args.push('ks-activity' + activityClass);
args.push(options.activitycolor);
printoutput(args);
}
};
var summary = function () {
var args = Array.prototype.slice.call(arguments);
args.push('ks-summary');
args.push(options.summarycolor);
printoutput(args);
};
var warning = function () {
var args = Array.prototype.slice.call(arguments);
args.unshift('Warning!');
if (console) console.log(args);
};
// Core Engine for Kitten Scientists
// =================================
var Engine = function () {
this.buildManager = new BuildManager();
this.spaceManager = new SpaceManager();
this.craftManager = new CraftManager();
this.tradeManager = new TradeManager();
this.villageManager = new TabManager('Village');
};
Engine.prototype = {
buildManager: undefined,
spaceManager: undefined,
craftManager: undefined,
tradeManager: undefined,
villageManager: undefined,
loop: undefined,
start: function () {
if (this.loop) return;
this.loop = setInterval(this.iterate.bind(this), options.interval);
message('Enabling the kitten scientists!');
},
stop: function () {
if (!this.loop) return;
clearInterval(this.loop);
this.loop = undefined;
message('Disabling the kitten scientists!');
},
iterate: function () {
this.observeStars();
if (options.auto.faith.enabled) this.praiseSun();
if (options.auto.festival.enabled) this.holdFestival();
if (options.auto.build.enabled) this.build();
if (options.auto.space.enabled) this.space();
if (options.auto.craft.enabled) this.craft();
if (options.auto.hunt.enabled) this.hunt();
if (options.auto.trade.enabled) this.trade();
},
build: function () {
var builds = options.auto.build.items;
var buildManager = this.buildManager;
var craftManager = this.craftManager;
var trigger = options.auto.build.trigger;
// Render the tab to make sure that the buttons actually exist in the DOM. Otherwise we can't click them.
buildManager.manager.render();
for (var name in builds) {
if (!builds[name].enabled) continue;
var build = builds[name];
var require = !build.require ? false : craftManager.getResource(build.require);
if (!require || trigger <= require.value / require.maxValue) {
// If the build overrides the name, use that name instead.
// This is usually true for buildings that can be upgraded.
buildManager.build(build.name || name, build.stage);
}
}
},
space: function () {
var builds = options.auto.space.items;
var buildManager = this.spaceManager;
var craftManager = this.craftManager;
var trigger = options.auto.space.trigger;
// Render the tab to make sure that the buttons actually exist in the DOM. Otherwise we can't click them.
buildManager.manager.render();
for (var name in builds) {
var build = builds[name];
var require = !build.require ? false : craftManager.getResource(build.require);
if (!require || trigger <= require.value / require.maxValue) {
buildManager.build(name);
}
}
},
craft: function () {
var crafts = options.auto.craft.items;
var manager = this.craftManager;
var trigger = options.auto.craft.trigger;
for (var name in crafts) {
var craft = crafts[name];
var current = !craft.max ? false : manager.getResource(name);
var require = !craft.require ? false : manager.getResource(craft.require);
var season = game.calendar.getCurSeason().name;
// Ensure that we have reached our cap
if (current && current.value > craft.max) continue;
// Enforce season limited on specific crafts
if (craft.limited && craft.lastSeason === season) continue;
// Craft the resource if we meet the trigger requirement
if (!require || trigger <= require.value / require.maxValue) {
var amount = Math.floor(manager.getLowestCraftAmount(name));
// Only update season if we actually craft anything.
if (amount > 0) {
manager.craft(name, manager.getLowestCraftAmount(name));
// Store the season for future reference
craft.lastSeason = season;
}
}
}
},
holdFestival: function () {
// if (game.science.get('drama').researched && game.calendar.festivalDays === 0 && game.villageTab.festivalBtn.model.enabled) {
if (game.science.get('drama').researched && game.calendar.festivalDays === 0 && this.villageManager.tab.festivalBtn.model.enabled) {
this.villageManager.tab.festivalBtn.onClick();
if (game.calendar.festivalDays !== 0) {
storeForSummary('festival');
activity('Kittens begin holding a festival', 'ks-festival');
}
}
},
observeStars: function () {
if (game.calendar.observeBtn != null){
game.calendar.observeHandler();
activity('Kitten Scientists have observed a star', 'ks-star');
storeForSummary('stars', 1);
}
},
praiseSun: function () {
var faith = this.craftManager.getResource('faith');
if (options.auto.faith.trigger <= faith.value / faith.maxValue) {
storeForSummary('faith', faith.value);
activity('Praised the sun!', 'ks-praise');
game.religion.praise();
}
},
hunt: function () {
var catpower = this.craftManager.getResource('catpower');
if (options.auto.hunt.trigger <= catpower.value / catpower.maxValue && catpower.value >= 100) {
// No way to send only some hunters. Thus, we hunt with everything
var hunters = game.village.getJob('hunter').value;
storeForSummary('hunt', hunters);
activity('Sent ' + game.getDisplayValueExt(hunters) + ' kitten' + (hunters == 1 ? '' : 's') + ' on the hunt', 'ks-hunt');
game.village.huntAll();
}
},
trade: function () {
var craftManager = this.craftManager;
var tradeManager = this.tradeManager;
var gold = craftManager.getResource('gold');
var trades = [];
// Only trade if it's enabled
if (!options.auto.trade.enabled) return;
// Trade when we have enough gold. Don't worry about catpower.
if (options.auto.trade.trigger >= gold.value / gold.maxValue) return;
// Determine how many races we will trade this cycle
for (var name in options.auto.trade.items) {
var trade = options.auto.trade.items[name];
var season = game.calendar.getCurSeason().name;
// Only check if we are in season and enabled
if (!trade.enabled) continue;
if (!trade[season]) continue;
var require = !trade.require ? false : craftManager.getResource(trade.require);
var requireTrigger = options.auto.trade.trigger;
// If we have enough to trigger the check, then attempt to trade
if (!require || requireTrigger <= require.value / require.maxValue) {
trades.push(name);
}
}
// Figure out how much we can currently trade
var maxTrades = tradeManager.getLowestTradeAmount(undefined);
// Try our best not to starve any single race
maxTrades = (trades.length > 0) ? Math.floor(maxTrades / trades.length) : 0;
if (maxTrades < 1) return;
for (var i in trades) {
var name = trades[i];
tradeManager.trade(name, Math.min(tradeManager.getLowestTradeAmount(name), maxTrades));
}
}
};
// Tab Manager
// ===========
var TabManager = function (name) {
this.setTab(name);
};
TabManager.prototype = {
tab: undefined,
render: function () {
if (this.tab && game.activeTabId !== this.tab.tabId) this.tab.render();
return this;
},
setTab: function (name) {
for (var tab in game.tabs) {
if (game.tabs[tab].tabId === name) {
this.tab = game.tabs[tab];
break;
}
}
this.tab ? this.render() : warning('unable to find tab ' + name);
}
};
// Building manager
// ================
var BuildManager = function () {
this.manager = new TabManager('Bonfire');
this.crafts = new CraftManager();
};
BuildManager.prototype = {
manager: undefined,
crafts: undefined,
build: function (name, stage) {
var build = this.getBuild(name);
var button = this.getBuildButton(name, stage);
if (!button || !button.model.enabled) return;
//need to simulate a click so the game updates everything properly
button.domNode.click(build);
storeForSummary(name, 1, 'build');
var label = build.meta.label ? build.meta.label : build.meta.stages[0].label;
activity('Kittens have built a new ' + label, 'ks-build');
},
getBuild: function (name) {
return game.bld.getBuildingExt(name);
},
getBuildButton: function (name, stage) {
var buttons = this.manager.tab.buttons;
var build = this.getBuild(name);
var label = typeof stage !== 'undefined' ? build.meta.stages[stage].label : build.meta.label;
for (var i in buttons) {
var haystack = buttons[i].buttonContent.innerText;
if(haystack.indexOf(label) !== -1){
return buttons[i];
}
}
}
};
// Space manager
// ================
var SpaceManager = function () {
this.manager = new TabManager('Space');
this.crafts = new CraftManager();
};
SpaceManager.prototype = {
manager: undefined,
crafts: undefined,
build: function (name) {
var build = this.getBuild(name);
var button = this.getBuildButton(name);
if (!build.unlocked || !button || !button.model.enabled || !options.auto.space.items[name].enabled) return;
//need to simulate a click so the game updates everything properly
button.domNode.click(build);
storeForSummary(name, 1, 'build');
var label = build.label;
activity('Kittens have built a new ' + label, 'ks-build');
},
getBuild: function (name) {
return game.space.getProgram(name);
},
getBuildButton: function (name) {
var panels = this.manager.tab.planetPanels;
for (var panel in panels) {
for (var child in panels[panel].children) {
if (panels[panel].children[child].id === name) return panels[panel].children[child];
}
}
}
};
// Crafting Manager
// ================
var CraftManager = function () {};
CraftManager.prototype = {
craft: function (name, amount) {
amount = Math.floor(amount);
if (!name || 1 > amount) return;
if (!this.canCraft(name, amount)) return;
var craft = this.getCraft(name);
var ratio = ('wood' === name) ? 'refineRatio' : 'craftRatio';
game.craft(craft.name, amount);
// determine actual amount after crafting upgrades
amount = (amount * (game.getEffect(ratio) + 1)).toFixed(2);
storeForSummary(name, amount, 'craft');
activity('Kittens have crafted ' + game.getDisplayValueExt(amount) + ' ' + ucfirst(name), 'ks-craft');
},
canCraft: function (name, amount) {
var craft = this.getCraft(name);
var enabled = options.auto.craft.items[name].enabled;
var result = false;
if (craft.unlocked && enabled) {
result = true;
for (var i in craft.prices) {
var price = craft.prices[i];
var value = this.getValueAvailable(price.name);
if (value < price.val * amount) {
result = false;
}
}
}
return result;
},
getCraft: function (name) {
return game.workshop.getCraft(this.getName(name));
},
getLowestCraftAmount: function (name) {
var amount = undefined;
var materials = this.getMaterials(name);
// Safeguard if materials for craft cannot be determined.
if (!materials) return 0;
var res = this.getResource(name);
for (var i in materials) {
var total = this.getValueAvailable(i) / materials[i];
amount = (amount === undefined || total < amount) ? total : amount;
}
// If we have a maximum value, ensure that we don't produce more than
// this value. This should currently only impact wood crafting, but is
// written generically to ensure it works for any craft that produces a
// good with a maximum value.
if (res.maxValue > 0 && amount > (res.maxValue - res.value))
amount = res.maxValue - res.value;
return amount;
},
getMaterials: function (name) {
var materials = {};
var craft = this.getCraft(name);
// Safeguard against craft items that aren't actually available yet.
if (!craft) return;
var prices = craft.prices;
for (var i in prices) {
var price = prices[i];
materials[price.name] = price.val;
}
return materials;
},
getName: function (name) {
// adjust for spelling discrepancies in core game logic
if ('catpower' === name) name = 'manpower';
if ('compendium' === name) name = 'compedium';
if ('concrete' === name) name = 'concrate';
return name;
},
getResource: function (name) {
for (var i in game.resPool.resources) {
var res = game.resPool.resources[i];
if (res.name === this.getName(name)) return res;
}
warning('unable to find resource ' + name);
return null;
},
getValue: function (name) {
return this.getResource(name).value;
},
getStock: function (name) {
var res = options.auto.resources[this.getName(name)];
var stock = res ? res.stock : 0;
return !stock ? 0 : stock;
},
getValueAvailable: function (name, all) {
var value = this.getValue(name);
var stock = this.getStock(name);
if ('catnip' === name) {
var resPerTick = game.getResourcePerTick(name, false, {
modifiers: {
'catnip': 0.10 - game.calendar.getWeatherMod()
}});
if (resPerTick < 0) stock -= resPerTick * 202 * 5;
}
value = Math.max(value - stock, 0);
// If we have a maxValue, and user hasn't requested all, check
// consumption rate
if (!all && this.getResource(name).maxValue > 0) {
var res = options.auto.resources[name];
var consume = res && (res.consume != undefined) ? res.consume : options.consume;
value *= consume;
}
return value;
}
};
// Trading Manager
// ===============
var TradeManager = function () {
this.craftManager = new CraftManager();
this.manager = new TabManager('Trade');
this.manager.render();
};
TradeManager.prototype = {
craftManager: undefined,
manager: undefined,
trade: function (name, amount) {
if (!name || 1 > amount) return;
var race = this.getRace(name);
if (!race.unlocked) return;
var button = this.getTradeButton(race.title);
if (!button.model.enabled || !options.auto.trade.items[name].enabled) return;
game.diplomacy.tradeMultiple(race, amount);
storeForSummary(name, amount, 'trade');
activity('Kittens have traded ' + amount + 'x with ' + ucfirst(name), 'ks-trade');
},
getLowestTradeAmount: function (name) {
var amount = undefined;
var highestCapacity = undefined;
var materials = this.getMaterials(name);
var race = this.getRace(name);
for (var i in materials) {
var total = this.craftManager.getValueAvailable(i) / materials[i];
amount = (amount === undefined || total < amount) ? total : amount;
}
if (race === null || options.auto.trade.items[name].allowcapped) return Math.floor(amount);
// Loop through the items obtained by the race, and determine
// which good has the most space left. Once we've determined this,
// reduce the amount by this capacity. This ensures that we continue to trade
// as long as at least one resource has capacity, and we never over-trade.
for (var s in race.sells) {
var item = race.sells[s];
var resource = this.craftManager.getResource(item.name);
var max = 0;
// No need to process resources that don't cap
if (!resource.maxValue) continue;
// Zebras special cased titanium taken directly from game code
if (race.name == "zebras" && item.name == "titanium") {
var val = 1.5 + (1.5 * game.resPool.get("ship").value / 100 * 2);
max = Math.ceil(val);
} else {
var sratio = item.seasons[game.calendar.getCurSeason().name];
var tratio = game.getEffect("tradeRatio");
var val = item.value + item.value * tratio;
max = val * sratio * (1 + item.delta/2);
}
capacity = (resource.maxValue - resource.value) / max;
highestCapacity = (capacity < highestCapacity) ? highestCapacity : capacity;
}
// We must take the ceiling of capacity so that we will trade as long
// as there is any room, even if it doesn't have exact space. Otherwise
// we seem to starve trading altogether.
highestCapacity = Math.ceil(highestCapacity);
// Now that we know the most we *should* trade for, check to ensure that
// we trade for our max cost, or our max capacity, whichever is lower.
// This helps us prevent trading for resources we can't store. Note that we
// essentially ignore blueprints here. In addition, if highestCapacity was never set,
// then we just
amount = (highestCapacity < amount) ? highestCapacity : amount;
return Math.floor(amount);
},
getMaterials: function (name) {
var materials = {catpower: 50, gold: 15};
if (name === undefined)
return materials;
var prices = this.getRace(name).buys;
for (var i in prices) {
var price = prices[i];
materials[price.name] = price.val;
}
return materials;
},
getRace: function (name) {
if (name === undefined)
return null;
else
return game.diplomacy.get(name);
},
getTradeButton: function (race) {
for (var i in this.manager.tab.racePanels) {
var panel = this.manager.tab.racePanels[i];
if (panel.name.indexOf(race) > -1) return panel.tradeBtn;
}
warning('unable to find trade button for ' + name);
}
};
// ==============================
// Configure overall page display
// ==============================
var container = $('#game');
var column = $('.column');
var body = $('body');
var button = $('.btn.modern');
var left = $('#leftColumn');
var middle = $('#midColumn');
var right = $('#rightColumn');
var addRule = function (rule) {
var sheets = document.styleSheets;
sheets[0].insertRule(rule, 0);
};
if (game.colorScheme !== 'sleek') {
container.css({
fontFamily: 'monospace',
fontSize: '12px',
minWidth: '1300px',
top: '32px'
});
body.css({
fontFamily: 'monospace',
fontSize: '12px'
});
button.css({
fontFamily: 'monospace',
fontSize: '12px',
width: '290px'
});
column.css({
minHeight: 'inherit',
maxWidth: 'inherit',
padding: '1%',
margin: 0,
overflowY: 'auto'
});
left.css({
height: '92%',
width: '26%'
});
middle.css({
marginTop: '1%',
height: '90%',
width: '48%'
});
right.css({
overflowY: 'scroll',
height: '92%',
width: '19%'
});
addRule('#gameLog .msg {'
+ 'display: block;'
+ '}');
addRule('#gameLog {'
+ 'overflow-y: hidden !important;'
+ 'width: 100% !important;'
+ 'padding-top: 5px !important;'
+ '}');
addRule('#resContainer .maxRes {'
+ 'color: #676766;'
+ '}');
addRule('#game .btn {'
+ 'border-radius: 0px;'
+ 'font-family: monospace;'
+ 'font-size: 12px !important;'
+ 'margin: 0 5px 7px 0;'
+ 'width: 290px;'
+ '}');
}
addRule('#ks-options ul {'
+ 'list-style: none;'
+ 'margin: 0 0 5px;'
+ 'padding: 0;'
+ '}');
addRule('#ks-options ul:after {'
+ 'clear: both;'
+ 'content: " ";'
+ 'display: block;'
+ 'height: 0;'
+ '}');
addRule('#ks-options ul li {'
+ 'display: block;'
+ 'float: left;'
+ 'width: 100%;'
+ '}');
// Local Storage
// =============
var kittenStorageVersion = 1;
var kittenStorage = {
version: kittenStorageVersion,
items: {},
resources: {},
triggers: {}
};
var initializeKittenStorage = function () {
$("#items-list-build, #items-list-craft, #items-list-trade").find("input[id^='toggle-']").each(function () {
kittenStorage.items[$(this).attr("id")] = $(this).prop("checked");
});
saveToKittenStorage();
};
var saveToKittenStorage = function () {
kittenStorage.resources = options.auto.resources;
kittenStorage.triggers = {
faith: options.auto.faith.trigger,
hunt: options.auto.hunt.trigger,
build: options.auto.build.trigger,
space: options.auto.space.trigger,