AbundantOre 0.5 (#1791)

This commit is contained in:
Jamis
2025-09-01 14:34:01 +08:00
committed by GitHub
parent cb37f59a3a
commit 42f54fccbb
3 changed files with 6184 additions and 3936 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -158,7 +158,14 @@ function is_ore_respawned(t) {
return respawn_time < Date.now() / 1000;
}
function get_some_tasks() {
function get_some_tasks(hints) {
hints.target_running_seconds = Math.min(7200, hints.target_running_seconds || Number.MAX_VALUE);
if (hints.target_yield) {
log.debug("Schedule with target yield {a}", hints.target_yield);
}
if (hints.target_running_seconds) {
log.debug("Schedule with target runnning seconds {a}", hints.target_running_seconds);
}
const exclude_tags = new Set(get_exclude_tags());
var filtered_statistics = [];
for (const [key, value] of Object.entries(statistics)) {
@@ -190,39 +197,52 @@ function get_some_tasks() {
filtered_statistics.sort((a, b) =>
b[1].statistics.avg_yield_per_min - a[1].statistics.avg_yield_per_min
);
var candidates = [];
var sum_yield = 0;
var sum_running_seconds = 0;
for (const [key, value] of filtered_statistics) {
candidates.push([key, value]);
sum_yield += value.statistics.avg_yield;
sum_running_seconds += value.statistics.avg_time_consumed;
if (hints.target_yield !== null && sum_yield >= hints.target_yield) {
break;
}
if (hints.target_running_seconds !== null && sum_running_seconds >= hints.target_running_seconds) {
break;
}
}
// We don't want to teleport around all the time. So add some spacial affinity here.
const look_ahead_num = 20;
var sorted_filtered_statistics = [];
for (var i = 0; i < filtered_statistics.length; ++i) {
if (!filtered_statistics[i]) {
continue;
}
const filename = filtered_statistics[i][0];
const value = filtered_statistics[i][1];
const candidate_groups = {};
for (const [key, value] of candidates) {
const group_name = value.group;
sorted_filtered_statistics.push([filename, value]);
filtered_statistics[i] = null;
for (var j = i + 1; j <= i + look_ahead_num && j < filtered_statistics.length; ++j) {
if (!filtered_statistics[j]) {
continue;
}
const filename2 = filtered_statistics[j][0];
const value2 = filtered_statistics[j][1];
const group_name2 = value2.group;
if (group_name2 === group_name) {
sorted_filtered_statistics.push([filename2, value2]);
filtered_statistics[j] = null;
}
if (!candidate_groups.hasOwnProperty(group_name)) {
candidate_groups[group_name] = {
sum_yield: 0,
sum_running_seconds: 0,
tasks: [],
};
}
candidate_groups[group_name].tasks.push(key);
candidate_groups[group_name].sum_yield += value.statistics.avg_yield;
candidate_groups[group_name].sum_running_seconds += value.statistics.avg_time_consumed;
}
if (sorted_filtered_statistics.length === 0) {
return [];
for (const i of Object.values(candidate_groups)) {
i.avg_yield_per_min = sum_yield / sum_running_seconds * 60;
i.tasks.sort();
}
const first_out_of_group_index = sorted_filtered_statistics.findIndex(i => i[1].group !== sorted_filtered_statistics[0][1].group);
const first_group = sorted_filtered_statistics.slice(0, first_out_of_group_index);
first_group.sort((a, b) => a[0].localeCompare(b[0]));
return first_group;
const tasks = Array.from(Object.values(candidate_groups)).sort((a, b) => b.avg_yield_per_min - a.avg_yield_per_min).map(i => i.tasks).flat();
var log_content = "";
sum_yield = 0;
sum_running_seconds = 0;
for (const i of tasks) {
const s = statistics[i]
log_content += ` ${s.statistics.avg_yield_per_min.toFixed(2)} ${i}\n`;
sum_yield += s.statistics.avg_yield;
sum_running_seconds += s.statistics.avg_time_consumed;
}
log.debug(log_content);
log.debug("Expected yield {a}, time {b} min", sum_yield, sum_running_seconds / 60);
return tasks.map(i => [i, statistics[i]]);
}
async function get_inventory() {
@@ -372,7 +392,6 @@ async function main() {
if (settings.time_range) {
const time_range = settings.time_range.replace("", "~").replace("", ":");
if (time_range.includes("~")) {
const [
[start_h, start_m],
[end_h, end_m]
@@ -403,7 +422,7 @@ async function main() {
const original_inventory = await get_inventory();
log.info("已有水晶块{a}个,紫晶块{b}个,萃凝晶{c}个", original_inventory.crystal_chunks, original_inventory.amethyst_lumps, original_inventory.condessence_crystals);
const target_yield = settings.target_amount ? Math.floor(Number(settings.target_amount)) : null;
const target_yield = settings.target_amount ? Math.ceil(Number(settings.target_amount)) : null;
if (target_yield && !run_until_unix_time) {
log.info("将挖矿{a}个", target_yield);
} else if (!target_yield && run_until_unix_time) {
@@ -419,12 +438,31 @@ async function main() {
const start_time = Date.now();
var accurate_yield = 0;
var estimated_yield = 0;
var cached_inventory_data = null;
var cached_inventory_data = original_inventory;
var finished = false;
const current_states = new Set();
while (!finished) {
const tasks = get_some_tasks();
const hints = {
target_yield: target_yield ? (target_yield - estimated_yield + 5) : null,
target_running_seconds: run_until_unix_time ? (run_until_unix_time - Date.now()) / 1000 : null,
};
{
const now = Math.floor(Date.now() / 1000);
var next_refresh_unix_time = Math.floor(now / 86400) * 86400 + 16 * 3600;
next_refresh_unix_time += 180; // to avoid the effect of clock skew
if (next_refresh_unix_time < now) {
next_refresh_unix_time += 86400;
}
if (!(hints.target_yield || hints.target_running_seconds)) {
hints.target_yield = 500;
}
const target_running_seconds2 = next_refresh_unix_time - now;
if (!hints.target_running_seconds || target_running_seconds2 < hints.target_running_seconds) {
hints.target_running_seconds = target_running_seconds2;
}
}
const tasks = get_some_tasks(hints);
if (tasks.length === 0) {
log.info("没有更多任务可运行,退出");
finished = true;

View File

@@ -2,7 +2,7 @@
"bgi_version": "0.49.0",
"manifest_version": 1,
"name": "矿产资源批发",
"version": "0.4",
"version": "0.5",
"description": "自动记录矿石刷新时间,优先选择效率最高的路线,支持按区域、种类、数量自动规划挖矿路线",
"authors": [
{
@@ -15,4 +15,4 @@
],
"settings_ui": "settings.json",
"main": "main.js"
}
}