Skip to content

Commit

Permalink
read modifiers for unique set runewords
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav committed Mar 15, 2024
1 parent 6bde86a commit 049f6d1
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 86 deletions.
127 changes: 72 additions & 55 deletions src/d2/attribute_enhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function enhanceItem(item: types.IItem, constants: types.IConstantData, l
const pt = constants.armor_items[parent.type] || constants.weapon_items[parent.type] || constants.other_items[item.type];
const t = constants.other_items[item.type];
if (t.m) {
item.magic_attributes = _compactAttributes(t.m[pt.gt], constants);
item.magic_attributes = compactAttributes(t.m[pt.gt], constants);
}
}
let details = null;
Expand Down Expand Up @@ -178,7 +178,7 @@ function _enhanceAttributeDescription(
const dgrpsVal = [0, 0, 0];
for (const property of magic_attributes) {
const prop = constants.magical_properties[property.id];
const v = property.values[property.values.length - 1];
const v = property.values[property.values?.length - 1];
if (prop.dg) {
if (dgrpsVal[prop.dg - 1] === 0) {
dgrpsVal[prop.dg - 1] = v;
Expand All @@ -193,7 +193,7 @@ function _enhanceAttributeDescription(
if (prop == null) {
throw new Error(`Cannot find Magical Property for id: ${property.id}`);
}
let v = property.values[property.values.length - 1];
let v = property.values[property.values?.length - 1];
if (prop.ob === "level") {
switch (prop.o) {
case 1: {
Expand Down Expand Up @@ -275,46 +275,65 @@ function _enhanceAttributeDescription(
return magic_attributes;
}

function _compactAttributes(mods: any[], constants: types.IConstantData): types.IMagicProperty[] {
export function compactAttributes(mods: any[], constants: types.IConstantData): types.IMagicProperty[] {
const magic_attributes = [] as types.IMagicProperty[];
for (const mod of mods) {
const properties = constants.properties[mod.m] || [];
for (let i = 0; i < properties.length; i++) {
const property = properties[i];
let stat = property.s;
switch (property.f) {
case 5: {
stat = "mindamage";
break;
}
case 6: {
stat = "maxdamage";
break;
}
case 7: {
stat = "item_maxdamage_percent";
break;
}
case 20: {
stat = "item_indesctructible";
break;
}
default: {
break;
for (const stat of constants.properties[mod.prop] || []) {
const statId = constants.magical_properties.findIndex((e) => e.s === stat.s)
const prop = constants.magical_properties[statId];
if (prop) {
if (prop.np) continue;
let v = [mod.min, mod.max];
switch (prop.dF) {
//item_addclassskills
case 13: {
v = [stat.val, mod.min];
break;
}
//item_addskill_tab
case 14: {
//TODO +skill to skilltab
//v = [mod.p & 7, (mod.p >> 3) * 3, mod.min];
v = [Math.abs(Math.round(mod.p / 3) - 3), Math.floor(mod.p / 3), mod.min];
break;
}
//item_skillon
case 15: {
v = [mod.max, mod.p, mod.min];
break;
}
//item_aura
case 16: {
v = [mod.p, mod.min];
break;
}
//charged_skill
case 24: {
v = [mod.max, mod.p, mod.min, mod.min];
break;
}
//item_singleskill
case 27: {
v = [mod.p, mod.min];
if (mod.prop == "skill-rand") {
const rnd = Math.floor(Math.random() * (mod.max - mod.min) + mod.min);
v = [constants.skills[rnd]?.id, mod.p];
}
break;
}
//item_nonclassskill
case 28: {
v = [mod.p, mod.min];
break;
}
default:
}
}
const id = _itemStatCostFromStat(stat, constants);
const prop = constants.magical_properties[id];
if (prop.np) i += prop.np;
const v = [mod.min, mod.max];
if (mod.p) {
v.push(mod.p);
}
magic_attributes.push({
id: id,
values: v,
name: prop.s,
} as types.IMagicProperty);
magic_attributes.push({
id: statId,
values: v,
name: prop.s,
} as types.IMagicProperty);
}
}
}
return magic_attributes;
Expand Down Expand Up @@ -377,19 +396,19 @@ function _descFunc(
}
case 14: {
const clazz = constants.classes[property.values[1]];
const skillTabStr = clazz.ts[property.values[0]];
const skillTabStr = clazz?.ts[property.values[0]];
descString = _sprintf(skillTabStr, v);
property.description = `${descString} ${clazz.co}`;
property.description = `${descString} ${clazz?.co}`;
break;
}
case 15: {
descString = _sprintf(descString, property.values[2], property.values[0], constants.skills[property.values[1]].s);
descString = _sprintf(descString, property.values[2], property.values[0], constants.skills[property.values[1]]?.n);
property.description = `${descString}`;
break;
}
case 16: {
property.description = descString.replace(/%d/, v.toString());
property.description = property.description.replace(/%s/, constants.skills[property.values[0]].s);
property.description = property.description.replace(/%s/, constants.skills[property.values[0]].n);
break;
}
case 17: {
Expand All @@ -403,7 +422,7 @@ function _descFunc(
break;
}
case 19: {
property.description = _sprintf(descString, v.toString());
property.description = _sprintf(descString, v?.toString());
break;
}
case 20: {
Expand Down Expand Up @@ -432,12 +451,12 @@ function _descFunc(
descString = descString.replace(/%d/gi, () => {
return property.values[2 + count++].toString();
});
property.description = `Level ${property.values[0]} ${constants.skills[property.values[1]].s} ${descString}`;
property.description = `Level ${property.values[0]} ${constants.skills[property.values[1]].n} ${descString}`;
} else {
property.description = _sprintf(
descString,
property.values[0],
constants.skills[property.values[1]].s,
constants.skills[property.values[1]]?.n,
property.values[2],
property.values[3]
);
Expand All @@ -446,17 +465,17 @@ function _descFunc(
}
case 27: {
const skill = constants.skills[property.values[0]];
const clazz = _classFromCode(skill.c, constants);
const clazz = _classFromCode(skill?.c, constants);
if (descString) {
property.description = _sprintf(descString, v, skill?.s, clazz?.co);
property.description = _sprintf(descString, v, skill?.n, clazz?.co);
} else {
property.description = `${sign}${v} to ${skill?.s} ${clazz?.co}`;
}
break;
}
case 28: {
const skill = constants.skills[property.values[0]];
property.description = `${sign}${v} to ${skill?.s}`;
property.description = `${sign}${v} to ${skill?.n}`;
break;
}
case 29: {
Expand Down Expand Up @@ -494,15 +513,13 @@ function _descFunc(

function _sprintf(str: string, ...args: any[]): string {
let i = 0;
return str
.replace(/%\+?d|%\+?s/gi, (m) => {
let v = args[i++].toString();
return str?.replace(/%\+?d|%\+?s/gi, (m) => {
let v = args[i++]?.toString();
if (m.indexOf("+") >= 0) {
v = "+" + v;
}
return v;
})
.replace("%%", "%");
}).replace("%%", "%");
}

function _itemStatCostFromStat(stat: string, constants: types.IConstantData): number {
Expand Down
2 changes: 1 addition & 1 deletion src/d2/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ export function _readMagicProperties(reader: BitReader, constants: types.IConsta
if (prop.sP) {
let param = reader.ReadUInt16(prop.sP);
switch (prop.dF) {
case 14: //+skill to skilltab
case 14: //TODO +skill to skilltab
values.push(param & 0x7);
param = (param >> 3) & 0x1fff;
break;
Expand Down
Loading

0 comments on commit 049f6d1

Please sign in to comment.