您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

149 行
4.1 KiB

  1. let spells = require('../../config/spells');
  2. let spellsConfig = require('../../config/spellsConfig');
  3. let configTypes = require('../config/types');
  4. const qualityGenerator = require('./quality');
  5. const qualityCount = qualityGenerator.qualities.length;
  6. const buildRolls = (item, blueprint, { random: spellProperties, negativeStats = [] }, quality) => {
  7. //We randomise the order so a random property gets to 'pick first'
  8. // otherwise it's easier for earlier properties to use more of the valuePool
  9. const propKeys = Object
  10. .keys(spellProperties)
  11. .sort((a, b) => Math.random() - Math.random());
  12. const propCount = propKeys.length;
  13. const maxRoll = (quality + 1) / qualityCount;
  14. const minSum = (quality / qualityCount) * propCount;
  15. let runningTotal = 0;
  16. const result = {};
  17. for (let i = 0; i < propCount; i++) {
  18. const minRoll = Math.max(0, minSum - runningTotal - ((propCount - (i + 1)) * maxRoll));
  19. let roll = minRoll + (Math.random() * (maxRoll - minRoll));
  20. runningTotal += roll;
  21. const prop = propKeys[i];
  22. const isNegative = negativeStats.includes(prop);
  23. if (isNegative)
  24. roll = 1 - roll;
  25. result[prop] = roll;
  26. }
  27. return result;
  28. };
  29. module.exports = {
  30. generate: function (item, blueprint) {
  31. blueprint = blueprint || {};
  32. let spellQuality = blueprint ? blueprint.spellQuality : '';
  33. let spellName = blueprint.spellName;
  34. if (!spellName) {
  35. let spellList = Object.keys(spellsConfig.spells).filter(s => !spellsConfig.spells[s].auto && !spellsConfig.spells[s].noDrop);
  36. spellName = spellList[~~(Math.random() * spellList.length)];
  37. }
  38. let spell = extend({}, spellsConfig.spells[spellName], blueprint.spellConfig);
  39. let spellAesthetic = spells.spells.find(s => s.name.toLowerCase() === spellName) || {};
  40. if (!item.slot) {
  41. let sprite = [10, 0];
  42. let statType = spell.statType;
  43. if (statType === 'dex')
  44. sprite = [10, 1];
  45. else if (statType === 'str')
  46. sprite = [10, 2];
  47. else if (statType instanceof Array) {
  48. if ((statType.indexOf('dex') > -1) && (statType.indexOf('int') > -1))
  49. sprite = [10, 3];
  50. else if ((statType.indexOf('str') > -1) && (statType.indexOf('int') > -1))
  51. sprite = [10, 4];
  52. }
  53. item.name = 'Rune of ' + spellAesthetic.name;
  54. item.ability = true;
  55. item.sprite = sprite;
  56. } else if (spellQuality === 'basic')
  57. item.stats = {};
  58. if (blueprint.spellConfig)
  59. spellAesthetic = extend({}, spellAesthetic, blueprint.spellConfig);
  60. item.spell = {
  61. name: spellAesthetic.name || 'Weapon Damage',
  62. type: spellAesthetic.type || spellName,
  63. rolls: {},
  64. values: {}
  65. };
  66. if (blueprint.spellConfig)
  67. extend(item.spell, blueprint.spellConfig);
  68. if (item.type) {
  69. let typeConfig = configTypes.types[item.slot][item.type];
  70. if (typeConfig)
  71. extend(spell, typeConfig.spellConfig);
  72. }
  73. //If the item has a slot, we need to generate a new quality for the rune
  74. let quality = item.quality;
  75. if (item.slot) {
  76. const tempItem = {};
  77. const tempBlueprint = extend(blueprint);
  78. delete tempBlueprint.quality;
  79. tempBlueprint.quality = blueprint.spellQuality;
  80. qualityGenerator.generate(tempItem, tempBlueprint);
  81. quality = tempItem.quality;
  82. item.spell.quality = quality;
  83. }
  84. const rolls = buildRolls(item, blueprint, spell, quality);
  85. Object.entries(spell.random || {}).forEach(entry => {
  86. const [ property, range ] = entry;
  87. const roll = rolls[property];
  88. item.spell.rolls[property] = roll;
  89. const isInt = property.indexOf('i_') === 0;
  90. let useProperty = property;
  91. const minRange = range[0];
  92. const maxRange = range[1] * (item.level / consts.maxLevel);
  93. let val = minRange + ((maxRange - minRange) * roll);
  94. if (isInt) {
  95. useProperty = property.substr(2);
  96. val = Math.round(val);
  97. } else
  98. val = ~~(val * 100) / 100;
  99. val = Math.max(range[0], Math.min(range[1], val));
  100. item.spell.values[useProperty] = val;
  101. });
  102. if (blueprint.spellProperties) {
  103. item.spell.properties = {};
  104. for (let p in blueprint.spellProperties)
  105. item.spell.properties[p] = blueprint.spellProperties[p];
  106. }
  107. if (item.range) {
  108. item.spell.properties = item.spell.properties || {};
  109. item.spell.properties.range = item.range;
  110. }
  111. }
  112. };