Skip to content

Commit

Permalink
feat(village): Assign all free kittens in a single frame
Browse files Browse the repository at this point in the history
The flood of log messages isn't ideal, but this can be optimized later. Assigning only a single kitten per frame is exhausting to watch.
  • Loading branch information
oliversalzburg committed Mar 19, 2023
1 parent 5c6c34d commit 944bbf7
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions packages/kitten-scientists/source/VillageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,51 +62,53 @@ export class VillageManager implements Automation {
return;
}

// Find all jobs where we haven't assigned the maximum desired kittens yet.
const jobsNotCapped = new Array<{ job: JobInfo; count: number; toCap: number }>();
for (const job of this._host.gamePage.village.jobs) {
// Skip disabled jobs and those that haven't been unlocked;
const enabled = this.settings.jobs[job.name].enabled;
const unlocked = job.unlocked;
if (!enabled || !unlocked) {
continue;
for (let assignedKitten = 0; assignedKitten < freeKittens; ++assignedKitten) {
// Find all jobs where we haven't assigned the maximum desired kittens yet.
const jobsNotCapped = new Array<{ job: JobInfo; count: number; toCap: number }>();
for (const job of this._host.gamePage.village.jobs) {
// Skip disabled jobs and those that haven't been unlocked;
const enabled = this.settings.jobs[job.name].enabled;
const unlocked = job.unlocked;
if (!enabled || !unlocked) {
continue;
}

const maxKittensInJob = this._host.gamePage.village.getJobLimit(job.name);
const maxKittensToAssign =
this.settings.jobs[job.name].max === -1
? Number.POSITIVE_INFINITY
: this.settings.jobs[job.name].max;
const kittensInJob = job.value;
if (kittensInJob < maxKittensInJob && kittensInJob < maxKittensToAssign) {
jobsNotCapped.push({ job, count: kittensInJob, toCap: maxKittensInJob - kittensInJob });
}
}

const maxKittensInJob = this._host.gamePage.village.getJobLimit(job.name);
const maxKittensToAssign =
this.settings.jobs[job.name].max === -1
? Number.POSITIVE_INFINITY
: this.settings.jobs[job.name].max;
const kittensInJob = job.value;
if (kittensInJob < maxKittensInJob && kittensInJob < maxKittensToAssign) {
jobsNotCapped.push({ job, count: kittensInJob, toCap: maxKittensInJob - kittensInJob });
if (!jobsNotCapped.length) {
return;
}
}

if (!jobsNotCapped.length) {
return;
// Check if we _could_ assign farmers _and_ currently don't have any assigned.
// The idea here is, don't assign any kittens into jobs without having filled
// that single open farmer position first. This might prevent kitten death in
// certain scenarios.
const noFarmersAssigned = !isNil(
jobsNotCapped.find(job => job.job.name === "farmer" && job.count === 0)
);

// Find the job with the least kittens assigned and assign a kitten to that job.
jobsNotCapped.sort((a, b) => a.count - b.count);
const jobName = noFarmersAssigned ? "farmer" : jobsNotCapped[0].job.name;

this._host.gamePage.village.assignJob(this._host.gamePage.village.getJob(jobName), 1);
this.manager.render();
this._host.engine.iactivity(
"act.distribute",
[this._host.engine.i18n(`$village.job.${jobName}` as const)],
"ks-distribute"
);
}

// Check if we _could_ assign farmers _and_ currently don't have any assigned.
// The idea here is, don't assign any kittens into jobs without having filled
// that single open farmer position first. This might prevent kitten death in
// certain scenarios.
const noFarmersAssigned = !isNil(
jobsNotCapped.find(job => job.job.name === "farmer" && job.count === 0)
);

// Find the job with the least kittens assigned and assign a kitten to that job.
jobsNotCapped.sort((a, b) => a.count - b.count);
const jobName = noFarmersAssigned ? "farmer" : jobsNotCapped[0].job.name;

this._host.gamePage.village.assignJob(this._host.gamePage.village.getJob(jobName), 1);
this.manager.render();
this._host.engine.iactivity(
"act.distribute",
[this._host.engine.i18n(`$village.job.${jobName}` as const)],
"ks-distribute"
);
this._host.engine.storeForSummary("distribute", 1);
this._host.engine.storeForSummary("distribute", freeKittens);
}

autoElect(): void {
Expand Down

0 comments on commit 944bbf7

Please sign in to comment.