Skip to content

Commit

Permalink
feat(village): Promote kittens
Browse files Browse the repository at this point in the history
When the trigger value for gold has been reached, the game's "Promote
Kittens" action is activated, if we have any engineers that would
benefit from being promoted.

Closes #13
  • Loading branch information
oliversalzburg committed Dec 17, 2022
1 parent 67df5d5 commit 9df17e8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
43 changes: 40 additions & 3 deletions packages/userscript/source/VillageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { VillageSettings } from "./settings/VillageSettings";
import { TabManager } from "./TabManager";
import { objectEntries } from "./tools/Entries";
import { cdebug } from "./tools/Log";
import { isNil } from "./tools/Maybe";
import { isNil, mustExist } from "./tools/Maybe";
import { Resource } from "./types";
import { JobInfo, VillageTab } from "./types/village";
import { UserScript } from "./UserScript";
Expand Down Expand Up @@ -49,7 +49,11 @@ export class VillageManager implements Automation {
}

if (this.settings.promoteLeader.enabled) {
this.autoPromote();
this.autoPromoteLeader();
}

if (this.settings.promoteKittens.enabled) {
this.autoPromoteKittens();
}
}

Expand Down Expand Up @@ -133,7 +137,40 @@ export class VillageManager implements Automation {
this._host.engine.iactivity("act.elect");
}

autoPromote(): void {
autoPromoteKittens(): void {
const gold = this._workshopManager.getResource("gold");
if (this.settings.promoteKittens.trigger < gold.value / gold.maxValue) {
return;
}

for (
let kittenIndex = 0;
kittenIndex < this._host.gamePage.village.sim.kittens.length;
kittenIndex++
) {
let tier = -1;
const engineerSpeciality =
this._host.gamePage.village.sim.kittens[kittenIndex].engineerSpeciality;
// If this kitten has no engineer speciality, skip it.
if (isNil(engineerSpeciality)) {
continue;
}

// Check which rank would be ideal for their craft.
tier = mustExist(this._host.gamePage.workshop.getCraft(engineerSpeciality)).tier;
// If the rank has already been reached, check next kitten.
if (tier <= this._host.gamePage.village.sim.kittens[kittenIndex].rank) {
continue;
}

// We have found an engineer that isn't at their ideal rank.
// No need to look further.
this._host.gamePage.village.promoteKittens();
return;
}
}

autoPromoteLeader(): void {
// If we have Civil Service unlocked and a leader elected.
if (
this._host.gamePage.science.get("civil").researched &&
Expand Down
2 changes: 2 additions & 0 deletions packages/userscript/source/i18n/i18nData.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"option.observe": "Observe Astro Events",
"option.praise": "Auto Praise",
"option.promote": "Promote leader",
"option.promotekittens": "Promote kittens",
"option.shipOverride": "Force Ships to 243",
"option.steamworks": "Turn on Steamworks",
"option.time.reset": "Reset Timeline (Danger!)",
Expand Down Expand Up @@ -250,6 +251,7 @@
"option.observe": "观测天文事件",
"option.praise": "赞美太阳",
"option.promote": "提拔领袖",
"option.promotekittens": "Promote kittens",
"option.shipOverride": "强制243船",
"option.steamworks": "启动蒸汽工房",
"option.time.reset": "重启时间线 (危险!)",
Expand Down
3 changes: 3 additions & 0 deletions packages/userscript/source/settings/VillageSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class VillageSettings extends Setting {

holdFestivals: Setting;
hunt: SettingTrigger;
promoteKittens: SettingTrigger;
promoteLeader: Setting;
electLeader: ElectLeaderSettings;

Expand All @@ -29,13 +30,15 @@ export class VillageSettings extends Setting {
},
holdFestivals = new Setting(true),
hunt = new SettingTrigger(true, 0.98),
promoteKittens = new SettingTrigger(true, 1),
promoteLeader = new Setting(true),
electLeader = new ElectLeaderSettings()
) {
super(enabled);
this.jobs = jobs;
this.holdFestivals = holdFestivals;
this.hunt = hunt;
this.promoteKittens = promoteKittens;
this.promoteLeader = promoteLeader;
this.electLeader = electLeader;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/userscript/source/types/craft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export type ResourceInfo = {
value: number;
};

export type CraftableInfo = { name: ResourceCraftable; unlocked: boolean };
export type CraftableInfo = { name: ResourceCraftable; tier: number; unlocked: boolean };
1 change: 1 addition & 0 deletions packages/userscript/source/types/gamePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ export type GamePage = {
getExplorationPrice: (x: number, y: number) => number;
villageData: Record<string, unknown>;
};
promoteKittens(): void;
sim: {
goldToPromote: (rank: number, value0: number, value1: number) => Array<unknown>;
kittens: Array<Kitten>;
Expand Down
2 changes: 1 addition & 1 deletion packages/userscript/source/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export type GameTab = {
export type Kitten = {
age: number;
color: number;
engineerSpecialtity: null;
engineerSpeciality: ResourceCraftable | null;
exp: number;
isAdopted: boolean;
isLeader: boolean;
Expand Down
18 changes: 18 additions & 0 deletions packages/userscript/source/ui/VillageSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class VillageSettingsUi extends SettingsSectionUi<VillageSettings> {
private readonly _jobs: Array<SettingListItem>;
private readonly _hunt: SettingTriggerListItem;
private readonly _festivals: SettingListItem;
private readonly _promoteKittens: SettingTriggerListItem;
private readonly _promoteLeader: SettingListItem;
private readonly _electLeader: SettingListItem;

Expand Down Expand Up @@ -101,6 +102,23 @@ export class VillageSettingsUi extends SettingsSectionUi<VillageSettings> {
);
this.addChild(this._festivals);

this._promoteKittens = new SettingTriggerListItem(
this._host,
this._host.engine.i18n("option.promotekittens"),
this.setting.promoteKittens,
{
onCheck: () =>
this._host.engine.imessage("status.sub.enable", [
this._host.engine.i18n("option.promotekittens"),
]),
onUnCheck: () =>
this._host.engine.imessage("status.sub.disable", [
this._host.engine.i18n("option.promotekittens"),
]),
}
);
this.addChild(this._promoteKittens);

this._promoteLeader = new SettingListItem(
this._host,
this._host.engine.i18n("option.promote"),
Expand Down

0 comments on commit 9df17e8

Please sign in to comment.