First iteration blazing fast sp engine

This commit is contained in:
hppeng 2022-07-17 20:44:00 -07:00
parent 3e9414ef1e
commit 1e4ebee975
3 changed files with 201 additions and 156 deletions

View file

@ -30,7 +30,6 @@ class Build{
this.level = level; this.level = level;
} else if (typeof level === "string") { } else if (typeof level === "string") {
this.level = level; this.level = level;
errors.push(new IncorrectInput(level, "a number", "level-choice"));
} else { } else {
errors.push("Level is not a string or number."); errors.push("Level is not a string or number.");
} }

View file

@ -407,7 +407,10 @@ class BuildAssembleNode extends ComputeNode {
input_map.get('guildTome1-input') input_map.get('guildTome1-input')
]; ];
let weapon = input_map.get('weapon-input'); let weapon = input_map.get('weapon-input');
let level = input_map.get('level-input'); let level = parseInt(input_map.get('level-input'));
if (isNaN(level)) {
level = 106;
}
let all_none = weapon.statMap.has('NONE'); let all_none = weapon.statMap.has('NONE');
for (const item of equipments) { for (const item of equipments) {

View file

@ -1,3 +1,82 @@
/**
* Apply skillpoint bonuses from an item.
* Also applies set deltas.
* Modifies the skillpoints array.
*/
function apply_skillpoints(skillpoints, item, activeSetCounts) {
for (let i = 0; i < 5; i++) {
skillpoints[i] += item.skillpoints[i];
}
const setName = item.set;
if (setName) { // undefined/null means no set.
let setCount = activeSetCounts.get(setName);
let old_bonus = {};
if (setCount) {
old_bonus = sets.get(setName).bonuses[setCount-1];
activeSetCounts.set(setName, setCount + 1);
}
else {
setCount = 0;
activeSetCounts.set(setName, 1);
}
const new_bonus = sets.get(setName).bonuses[setCount];
//let skp_order = ["str","dex","int","def","agi"];
for (const i in skp_order) {
const delta = (new_bonus[skp_order[i]] || 0) - (old_bonus[skp_order[i]] || 0);
skillpoints[i] += delta;
}
}
}
/**
* Apply skillpoints until this item can be worn.
* Also applies set deltas.
* Confusingly, does not modify the skillpoints array.
* Instead, return an array of deltas.
*/
function apply_to_fit(skillpoints, item, skillpoint_min, activeSetCounts) {
let applied = [0, 0, 0, 0, 0];
for (let i = 0; i < 5; i++) {
if (item.skillpoints[i] < 0 && skillpoint_min[i]) {
const unadjusted = skillpoints[i] + item.skillpoints[i];
const delta = skillpoint_min[i] - unadjusted;
if (delta > 0) {
applied[i] += delta;
}
}
if (item.reqs[i] == 0) continue;
skillpoint_min[i] = Math.max(skillpoint_min[i], item.reqs[i] + item.skillpoints[i]);
const req = item.reqs[i];
const cur = skillpoints[i];
if (req > cur) {
const diff = req - cur;
applied[i] += diff;
}
}
const setName = item.set;
if (setName) { // undefined/null means no set.
const setCount = activeSetCounts.get(setName);
if (setCount) {
const old_bonus = sets.get(setName).bonuses[setCount-1];
const new_bonus = sets.get(setName).bonuses[setCount];
//let skp_order = ["str","dex","int","def","agi"];
for (const i in skp_order) {
const set_delta = (new_bonus[skp_order[i]] || 0) - (old_bonus[skp_order[i]] || 0);
if (set_delta < 0 && skillpoint_min[i]) {
const unadjusted = skillpoints[i] + set_delta;
const delta = skillpoint_min[i] - unadjusted;
if (delta > 0) {
applied[i] += delta;
}
}
}
}
}
return applied;
}
function calculate_skillpoints(equipment, weapon) { function calculate_skillpoints(equipment, weapon) {
const start = performance.now(); const start = performance.now();
// Calculate equipment equipping order and required skillpoints. // Calculate equipment equipping order and required skillpoints.
@ -28,78 +107,7 @@ function calculate_skillpoints(equipment, weapon) {
consider.push(item); consider.push(item);
} }
} }
function apply_skillpoints(skillpoints, item, activeSetCounts) {
for (let i = 0; i < 5; i++) {
skillpoints[i] += item.skillpoints[i];
}
const setName = item.set;
if (setName) { // undefined/null means no set.
let setCount = activeSetCounts.get(setName);
let old_bonus = {};
if (setCount) {
old_bonus = sets.get(setName).bonuses[setCount-1];
activeSetCounts.set(setName, setCount + 1);
}
else {
setCount = 0;
activeSetCounts.set(setName, 1);
}
const new_bonus = sets.get(setName).bonuses[setCount];
//let skp_order = ["str","dex","int","def","agi"];
for (const i in skp_order) {
const delta = (new_bonus[skp_order[i]] || 0) - (old_bonus[skp_order[i]] || 0);
skillpoints[i] += delta;
}
}
}
function apply_to_fit(skillpoints, item, skillpoint_min, activeSetCounts) {
let applied = [0, 0, 0, 0, 0];
let total = 0;
for (let i = 0; i < 5; i++) {
if (item.skillpoints[i] < 0 && skillpoint_min[i]) {
const unadjusted = skillpoints[i] + item.skillpoints[i];
const delta = skillpoint_min[i] - unadjusted;
if (delta > 0) {
applied[i] += delta;
total += delta;
}
}
if (item.reqs[i] == 0) continue;
skillpoint_min[i] = Math.max(skillpoint_min[i], item.reqs[i] + item.skillpoints[i]);
const req = item.reqs[i];
const cur = skillpoints[i];
if (req > cur) {
const diff = req - cur;
applied[i] += diff;
total += diff;
}
}
const setName = item.set;
if (setName) { // undefined/null means no set.
const setCount = activeSetCounts.get(setName);
if (setCount) {
const old_bonus = sets.get(setName).bonuses[setCount-1];
const new_bonus = sets.get(setName).bonuses[setCount];
//let skp_order = ["str","dex","int","def","agi"];
for (const i in skp_order) {
const set_delta = (new_bonus[skp_order[i]] || 0) - (old_bonus[skp_order[i]] || 0);
if (set_delta < 0 && skillpoint_min[i]) {
const unadjusted = skillpoints[i] + set_delta;
const delta = skillpoint_min[i] - unadjusted;
if (delta > 0) {
applied[i] += delta;
total += delta;
}
}
}
}
}
return [applied, total];
}
// Separate out the no req items and add them to the static skillpoint base. // Separate out the no req items and add them to the static skillpoint base.
let static_skillpoints_base = [0, 0, 0, 0, 0] let static_skillpoints_base = [0, 0, 0, 0, 0]
@ -108,7 +116,7 @@ function calculate_skillpoints(equipment, weapon) {
apply_skillpoints(static_skillpoints_base, item, static_activeSetCounts); apply_skillpoints(static_skillpoints_base, item, static_activeSetCounts);
} }
let best = consider.concat(noboost); let best = consider;
let final_skillpoints = static_skillpoints_base.slice(); let final_skillpoints = static_skillpoints_base.slice();
let best_skillpoints = [0, 0, 0, 0, 0]; let best_skillpoints = [0, 0, 0, 0, 0];
let best_total = Infinity; let best_total = Infinity;
@ -117,97 +125,91 @@ function calculate_skillpoints(equipment, weapon) {
let allFalse = [0, 0, 0, 0, 0]; let allFalse = [0, 0, 0, 0, 0];
if (consider.length > 0 || noboost.length > 0 || crafted.length > 0) { if (consider.length > 0 || noboost.length > 0 || crafted.length > 0) {
// Try every combination and pick the best one. // Try every combination and pick the best one.
construct_scc_graph(consider); const [root, terminal, sccs] = construct_scc_graph(consider);
for (let permutation of perm(consider)) { console.log(sccs);
let activeSetCounts = new Map(static_activeSetCounts); const end_checks = crafted.concat(noboost);
let has_skillpoint = allFalse.slice(); end_checks.push(weapon);
permutation = permutation.concat(noboost); // naive way first.
let skillpoints_applied = [0, 0, 0, 0, 0]; function check_end(skillpoints_applied, skillpoints, activeSetCounts, total_applied) {
// Complete slice is a shallow copy.
let skillpoints = static_skillpoints_base.slice();
let total_applied = 0;
let result;
let needed_skillpoints;
let total_diff;
for (const item of permutation) {
result = apply_to_fit(skillpoints, item, has_skillpoint, activeSetCounts);
needed_skillpoints = result[0];
total_diff = result[1];
for (let i = 0; i < 5; ++i) {
skillpoints_applied[i] += needed_skillpoints[i];
skillpoints[i] += needed_skillpoints[i];
}
apply_skillpoints(skillpoints, item, activeSetCounts);
total_applied += total_diff;
if (total_applied >= best_total) {
break;
}
}
// Crafted skillpoint does not count initially. // Crafted skillpoint does not count initially.
for (const item of crafted) { for (const item of end_checks) {
//console.log(item) const needed_skillpoints = apply_to_fit(skillpoints, item,
result = apply_to_fit(skillpoints, item, allFalse.slice(), activeSetCounts); [false, false, false, false, false], activeSetCounts);
//console.log(result)
needed_skillpoints = result[0];
total_diff = result[1];
for (let i = 0; i < 5; ++i) { for (let i = 0; i < 5; ++i) {
skillpoints_applied[i] += needed_skillpoints[i]; const skp = needed_skillpoints[i]
skillpoints[i] += needed_skillpoints[i]; skillpoints_applied[i] += skp;
skillpoints[i] += skp;
total_applied += skp;
} }
total_applied += total_diff; if (best_total < total_applied) { return -1; }
}
if (total_applied >= best_total) {
continue;
}
let pre = skillpoints.slice();
result = apply_to_fit(skillpoints, weapon, allFalse.slice(), activeSetCounts);
needed_skillpoints = result[0];
total_diff = result[1];
for (let i = 0; i < 5; ++i) {
skillpoints_applied[i] += needed_skillpoints[i];
skillpoints[i] += needed_skillpoints[i];
}
apply_skillpoints(skillpoints, weapon, activeSetCounts);
total_applied += total_diff;
// Applying crafted item skill points last.
for (const item of crafted) {
apply_skillpoints(skillpoints, item, activeSetCounts);
//total_applied += total_diff;
}
if (total_applied < best_total) {
best = permutation;
final_skillpoints = skillpoints;
best_skillpoints = skillpoints_applied;
best_total = total_applied;
best_activeSetCounts = activeSetCounts;
} }
return total_applied;
} }
function permute_check(idx, _applied, _skillpoints, _sets, _has, _total_applied, order) {
const {nodes, children} = sccs[idx];
if (nodes[0] === terminal) {
const total = check_end(_applied, _skillpoints, _sets, _total_applied);
if (total < best_total) {
final_skillpoints = _skillpoints;
best_skillpoints = _applied;
best_total = total;
best_activeSetCounts = _sets;
best = order;
console.log('new best');
console.log(order);
}
return;
}
for (let permutation of perm(nodes)) {
const skillpoints_applied = _applied.slice();
const skillpoints = _skillpoints.slice();
const activeSetCounts = new Map(_sets);
const has_skillpoint = _has.slice();
let total_applied = _total_applied;
let short_circuit = false;
for (const {item} of permutation) {
needed_skillpoints = apply_to_fit(skillpoints, item, has_skillpoint, activeSetCounts);
for (let i = 0; i < 5; ++i) {
skp = needed_skillpoints[i];
skillpoints_applied[i] += skp;
skillpoints[i] += skp;
total_applied += skp;
}
if (total_applied >= best_total) {
short_circuit = true;
break; // short circuit failure
}
apply_skillpoints(skillpoints, item, activeSetCounts);
}
if (short_circuit) { continue; }
permute_check(idx+1, skillpoints_applied, skillpoints, activeSetCounts, has_skillpoint, total_applied, order.concat(permutation.map(x => x.item)));
}
}
// skip root.
permute_check(1, final_skillpoints, best_skillpoints, best_activeSetCounts, allFalse.slice(), 0, []);
// add extra sp bonus
apply_skillpoints(final_skillpoints, weapon, best_activeSetCounts);
// Applying crafted item skill points last.
for (const item of crafted) {
apply_skillpoints(final_skillpoints, item, best_activeSetCounts);
}
} }
else { else {
best_total = 0; best_total = 0;
result = apply_to_fit(final_skillpoints, weapon, allFalse.slice(), best_activeSetCounts); needed_skillpoints = apply_to_fit(final_skillpoints, weapon, allFalse.slice(), best_activeSetCounts);
needed_skillpoints = result[0];
total_diff = result[1];
for (let i = 0; i < 5; ++i) { for (let i = 0; i < 5; ++i) {
best_skillpoints[i] += needed_skillpoints[i]; const skp = needed_skillpoints[i];
final_skillpoints[i] += needed_skillpoints[i]; best_skillpoints[i] += skp;
final_skillpoints[i] += skp;
best_skillpoints += skp;
} }
apply_skillpoints(final_skillpoints, weapon, best_activeSetCounts); apply_skillpoints(final_skillpoints, weapon, best_activeSetCounts);
best_total += total_diff;
} }
let equip_order = fixed.concat(best).concat(crafted); let equip_order = fixed.concat(best).concat(noboost).concat(crafted);
// best_skillpoints: manually assigned (before any gear) // best_skillpoints: manually assigned (before any gear)
// final_skillpoints: final totals (5 individ) // final_skillpoints: final totals (5 individ)
// best_total: total skillpoints assigned (number) // best_total: total skillpoints assigned (number)
@ -220,21 +222,32 @@ function calculate_skillpoints(equipment, weapon) {
function construct_scc_graph(items_to_consider) { function construct_scc_graph(items_to_consider) {
let nodes = []; let nodes = [];
for (const item of items_to_consider) { let terminal_node = {
nodes.push({item: item, children: [], parents: [], visited: false}); item: null,
} children: [],
parents: nodes,
visited: false,
assigned: false,
scc: null
};
let root_node = { let root_node = {
item: null,
children: nodes, children: nodes,
parents: [], parents: [],
visited: false visited: false,
assigned: false,
scc: null
}; };
for (const item of items_to_consider) {
nodes.push({item: item, children: [terminal_node], parents: [root_node], visited: false, assigned: false, scc: null});
}
// Dependency graph construction. // Dependency graph construction.
for (const node_a of nodes) { for (const node_a of nodes) {
const {item: a, children: a_children} = node_a; const {item: a, children: a_children} = node_a;
for (const node_b of nodes) { for (const node_b of nodes) {
const {item: b, parents: b_parents} = node_b; const {item: b, parents: b_parents} = node_b;
for (let i = 0; i < 5; ++i) { for (let i = 0; i < 5; ++i) {
if (b.reqs[i] < a.reqs[i] && b.skillpoints[i]) { if (a.reqs[i] < b.reqs[i] && a.skillpoints[i]) {
a_children.push(node_b); a_children.push(node_b);
b_parents.push(node_a); b_parents.push(node_a);
break; break;
@ -257,5 +270,35 @@ function construct_scc_graph(items_to_consider) {
} }
visit(root_node, res); visit(root_node, res);
res.reverse(); res.reverse();
console.log(res); const sccs = [];
function assign(node, cur_scc) {
if (node.assigned) { return; }
cur_scc.nodes.push(node);
node.scc = cur_scc;
node.assigned = true;
for (const parent of node.parents) {
assign(parent, cur_scc);
}
}
for (const node of res) {
if (node.assigned) { continue; }
const cur_scc = {
nodes: [],
children: new Set(),
parents: new Set()
};
assign(node, cur_scc);
sccs.push(cur_scc);
}
for (const scc of sccs) {
for (const node of scc.nodes) {
for (const child of node.children) {
scc.children.add(child.scc);
}
for (const parent of node.parents) {
scc.parents.add(parent.scc);
}
}
}
return [root_node, terminal_node, sccs];
} }