2022-07-18 03:44:00 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2022-05-21 04:34:24 +00:00
|
|
|
function calculate_skillpoints(equipment, weapon) {
|
2022-07-18 06:33:39 +00:00
|
|
|
// const start = performance.now();
|
2021-01-07 06:41:41 +00:00
|
|
|
// Calculate equipment equipping order and required skillpoints.
|
|
|
|
// Return value: [equip_order, best_skillpoints, final_skillpoints, best_total];
|
|
|
|
let fixed = [];
|
|
|
|
let consider = [];
|
|
|
|
let noboost = [];
|
2021-02-13 00:43:21 +00:00
|
|
|
let crafted = [];
|
2022-07-17 08:38:12 +00:00
|
|
|
weapon.skillpoints = weapon.get('skillpoints');
|
|
|
|
weapon.reqs = weapon.get('reqs');
|
|
|
|
weapon.set = weapon.get('set');
|
2021-01-07 06:41:41 +00:00
|
|
|
for (const item of equipment) {
|
2022-07-17 08:38:12 +00:00
|
|
|
item.skillpoints = item.get('skillpoints');
|
|
|
|
item.reqs = item.get('reqs');
|
|
|
|
item.set = item.get('set');
|
2021-02-14 23:33:37 +00:00
|
|
|
if (item.get("crafted")) {
|
2021-02-13 00:43:21 +00:00
|
|
|
crafted.push(item);
|
|
|
|
}
|
2022-07-18 04:13:20 +00:00
|
|
|
// TODO hack: We will treat ALL set items as unsafe :(
|
|
|
|
else if (item.set !== null) {
|
|
|
|
consider.push(item);
|
|
|
|
}
|
2022-07-17 08:38:12 +00:00
|
|
|
else if (item.get("reqs").every(x => x === 0) && item.skillpoints.every(x => x >= 0)) {
|
2021-03-21 04:00:34 +00:00
|
|
|
// All reqless item without -skillpoints.
|
2021-02-14 23:33:37 +00:00
|
|
|
fixed.push(item);
|
|
|
|
}
|
2022-07-18 04:13:20 +00:00
|
|
|
else if (item.skillpoints.every(x => x <= 0)) {
|
2021-01-07 06:41:41 +00:00
|
|
|
noboost.push(item);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
consider.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Separate out the no req items and add them to the static skillpoint base.
|
|
|
|
let static_skillpoints_base = [0, 0, 0, 0, 0]
|
2021-01-10 04:29:07 +00:00
|
|
|
let static_activeSetCounts = new Map()
|
2021-01-07 06:41:41 +00:00
|
|
|
for (const item of fixed) {
|
2021-01-10 04:29:07 +00:00
|
|
|
apply_skillpoints(static_skillpoints_base, item, static_activeSetCounts);
|
2021-01-07 06:41:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:44:00 +00:00
|
|
|
let best = consider;
|
2021-01-08 20:36:03 +00:00
|
|
|
let final_skillpoints = static_skillpoints_base.slice();
|
2021-01-07 06:41:41 +00:00
|
|
|
let best_skillpoints = [0, 0, 0, 0, 0];
|
|
|
|
let best_total = Infinity;
|
2021-01-10 04:29:07 +00:00
|
|
|
let best_activeSetCounts = static_activeSetCounts;
|
2021-01-07 06:41:41 +00:00
|
|
|
|
2021-02-13 00:43:21 +00:00
|
|
|
let allFalse = [0, 0, 0, 0, 0];
|
|
|
|
if (consider.length > 0 || noboost.length > 0 || crafted.length > 0) {
|
2021-01-07 06:41:41 +00:00
|
|
|
// Try every combination and pick the best one.
|
2022-07-18 03:44:00 +00:00
|
|
|
const [root, terminal, sccs] = construct_scc_graph(consider);
|
|
|
|
const end_checks = crafted.concat(noboost);
|
|
|
|
end_checks.push(weapon);
|
2021-01-07 06:41:41 +00:00
|
|
|
|
2022-07-18 03:44:00 +00:00
|
|
|
function check_end(skillpoints_applied, skillpoints, activeSetCounts, total_applied) {
|
2021-02-13 00:43:21 +00:00
|
|
|
// Crafted skillpoint does not count initially.
|
2022-07-18 03:44:00 +00:00
|
|
|
for (const item of end_checks) {
|
|
|
|
const needed_skillpoints = apply_to_fit(skillpoints, item,
|
|
|
|
[false, false, false, false, false], activeSetCounts);
|
2021-02-13 00:43:21 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < 5; ++i) {
|
2022-07-18 03:44:00 +00:00
|
|
|
const skp = needed_skillpoints[i]
|
|
|
|
skillpoints_applied[i] += skp;
|
|
|
|
skillpoints[i] += skp;
|
|
|
|
total_applied += skp;
|
2021-02-13 00:43:21 +00:00
|
|
|
}
|
2022-07-18 03:44:00 +00:00
|
|
|
if (best_total < total_applied) { return -1; }
|
2021-02-13 00:43:21 +00:00
|
|
|
}
|
2022-07-18 03:44:00 +00:00
|
|
|
return total_applied;
|
|
|
|
}
|
2021-02-13 00:43:21 +00:00
|
|
|
|
2022-07-18 03:44:00 +00:00
|
|
|
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);
|
2022-07-18 06:10:16 +00:00
|
|
|
if (total !== -1 && total < best_total) {
|
2022-07-18 03:44:00 +00:00
|
|
|
final_skillpoints = _skillpoints;
|
|
|
|
best_skillpoints = _applied;
|
|
|
|
best_total = total;
|
|
|
|
best_activeSetCounts = _sets;
|
|
|
|
best = order;
|
|
|
|
}
|
|
|
|
return;
|
2021-02-13 00:43:21 +00:00
|
|
|
}
|
2022-07-18 03:44:00 +00:00
|
|
|
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)));
|
2021-01-07 06:41:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-02 13:47:25 +00:00
|
|
|
if (sccs.length === 1) {
|
|
|
|
// Only crafteds. Just do end check (check req first, then apply sp after)
|
|
|
|
const total = check_end(best_skillpoints, final_skillpoints, best_activeSetCounts, allFalse.slice());
|
|
|
|
final_skillpoints = best_skillpoints;
|
|
|
|
best_total = total;
|
|
|
|
best_activeSetCounts = best_activeSetCounts;
|
|
|
|
best = [];
|
|
|
|
} else {
|
|
|
|
// skip root.
|
|
|
|
permute_check(1, best_skillpoints, final_skillpoints, best_activeSetCounts, allFalse.slice(), 0, []);
|
|
|
|
}
|
2021-01-07 06:41:41 +00:00
|
|
|
|
2022-07-18 03:44:00 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2021-01-07 06:41:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-01-08 20:36:03 +00:00
|
|
|
best_total = 0;
|
2022-07-18 03:44:00 +00:00
|
|
|
needed_skillpoints = apply_to_fit(final_skillpoints, weapon, allFalse.slice(), best_activeSetCounts);
|
2021-01-08 20:14:23 +00:00
|
|
|
for (let i = 0; i < 5; ++i) {
|
2022-07-18 03:44:00 +00:00
|
|
|
const skp = needed_skillpoints[i];
|
|
|
|
best_skillpoints[i] += skp;
|
|
|
|
final_skillpoints[i] += skp;
|
2022-07-18 06:48:42 +00:00
|
|
|
best_total += skp;
|
2021-01-08 20:14:23 +00:00
|
|
|
}
|
2021-01-10 07:48:40 +00:00
|
|
|
apply_skillpoints(final_skillpoints, weapon, best_activeSetCounts);
|
2021-01-07 06:41:41 +00:00
|
|
|
}
|
2022-07-18 03:44:00 +00:00
|
|
|
let equip_order = fixed.concat(best).concat(noboost).concat(crafted);
|
2021-07-27 10:04:12 +00:00
|
|
|
// best_skillpoints: manually assigned (before any gear)
|
|
|
|
// final_skillpoints: final totals (5 individ)
|
|
|
|
// best_total: total skillpoints assigned (number)
|
2022-07-18 06:33:39 +00:00
|
|
|
// const end = performance.now();
|
|
|
|
// const output_msg = `skillpoint calculation took ${(end-start)/ 1000} seconds.`;
|
|
|
|
// console.log(output_msg);
|
2021-01-10 04:29:07 +00:00
|
|
|
return [equip_order, best_skillpoints, final_skillpoints, best_total, best_activeSetCounts];
|
2021-01-07 06:41:41 +00:00
|
|
|
}
|
2022-07-17 19:40:37 +00:00
|
|
|
|
|
|
|
function construct_scc_graph(items_to_consider) {
|
|
|
|
let nodes = [];
|
2022-07-18 03:44:00 +00:00
|
|
|
let terminal_node = {
|
|
|
|
item: null,
|
|
|
|
children: [],
|
2022-07-20 16:21:13 +00:00
|
|
|
parents: nodes
|
2022-07-18 03:44:00 +00:00
|
|
|
};
|
2022-07-17 19:40:37 +00:00
|
|
|
let root_node = {
|
2022-07-18 03:44:00 +00:00
|
|
|
item: null,
|
2022-07-17 19:40:37 +00:00
|
|
|
children: nodes,
|
|
|
|
parents: [],
|
|
|
|
};
|
2022-07-18 03:44:00 +00:00
|
|
|
for (const item of items_to_consider) {
|
2022-07-20 16:21:13 +00:00
|
|
|
nodes.push({item: item, children: [terminal_node], parents: [root_node]});
|
2022-07-18 03:44:00 +00:00
|
|
|
}
|
2022-07-17 19:40:37 +00:00
|
|
|
// Dependency graph construction.
|
|
|
|
for (const node_a of nodes) {
|
|
|
|
const {item: a, children: a_children} = node_a;
|
|
|
|
for (const node_b of nodes) {
|
|
|
|
const {item: b, parents: b_parents} = node_b;
|
|
|
|
for (let i = 0; i < 5; ++i) {
|
2022-07-18 04:00:27 +00:00
|
|
|
if (a.skillpoints[i] > 0 && (a.reqs[i] < b.reqs[i] || b.skillpoints[i] < 0)) {
|
2022-07-17 19:40:37 +00:00
|
|
|
a_children.push(node_b);
|
|
|
|
b_parents.push(node_a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 16:21:13 +00:00
|
|
|
const sccs = make_SCC_graph(root_node, nodes);
|
2022-07-18 03:44:00 +00:00
|
|
|
return [root_node, terminal_node, sccs];
|
2022-07-17 19:40:37 +00:00
|
|
|
}
|