You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

168 lines
3.7 KiB

  1. let generator = require('../../items/generator');
  2. let skins = require('../../config/skins');
  3. let factions = require('../../config/factions');
  4. module.exports = {
  5. baseItems: [],
  6. cdMax: 5130,
  7. blueprint: null,
  8. init: function (blueprint) {
  9. this.baseItems = this.items;
  10. this.items = {};
  11. this.faction = blueprint.faction;
  12. this.blueprint = blueprint;
  13. },
  14. update: function () {
  15. for (let name in this.items) {
  16. let list = this.items[name];
  17. list.cd--;
  18. if (!list.cd) {
  19. list.cd = this.cdMax;
  20. this.regenList(list);
  21. }
  22. }
  23. },
  24. getItems: function (requestedBy) {
  25. let name = requestedBy.name;
  26. let requestLevel = requestedBy.stats.values.level;
  27. let list = this.items[name];
  28. if (!list) {
  29. list = {
  30. items: [],
  31. level: requestLevel,
  32. cd: this.cdMax
  33. };
  34. this.items[name] = list;
  35. this.regenList(list);
  36. } else if (list.level !== requestLevel) {
  37. list.level = requestLevel;
  38. this.regenList(list);
  39. }
  40. let result = list.items.map(m => {
  41. let item = requestedBy.inventory.simplifyItem(m);
  42. if (item.stats)
  43. item.stats = { stats: '???' };
  44. if (item.implicitStats)
  45. item.implicitStats = [ { stat: 'stats', value: '???' } ];
  46. delete item.effects;
  47. return item;
  48. });
  49. return result;
  50. },
  51. regenList: function (list) {
  52. let blueprint = this.blueprint;
  53. list.items = null;
  54. list.items = [];
  55. let faction = factions.getFaction(blueprint.faction.id);
  56. let statGenerator = faction.uniqueStat;
  57. let itemCount = blueprint.items.min + ~~(Math.random() * (blueprint.items.max - blueprint.items.min));
  58. for (let i = 0; i < itemCount; i++) {
  59. let minLevel = blueprint.items.minLevel || Math.max(1, list.level * 0.75);
  60. let maxLevel = blueprint.items.maxLevel || (list.level * 1.25);
  61. let level = Math.min(20, ~~(minLevel + (Math.random() * (maxLevel - minLevel))));
  62. let item = generator.generate({
  63. noSpell: true,
  64. magicFind: 150,
  65. slot: blueprint.items.slot,
  66. level: level
  67. });
  68. let randomQuality = ~~(Math.random() * 5);
  69. item.worth = Math.pow(item.level, 1.5) + (Math.pow((randomQuality + 1), 2) * 10);
  70. let id = 0;
  71. list.items.forEach(function (checkItem) {
  72. if (checkItem.id >= id)
  73. id = checkItem.id + 1;
  74. });
  75. item.id = id;
  76. generator.removeStat(item);
  77. statGenerator.generate(item);
  78. item.factions = [{}];
  79. item.factions[0].id = blueprint.faction.id;
  80. item.factions[0].tier = blueprint.faction.tier;
  81. list.items.push(item);
  82. }
  83. let baseItems = this.baseItems;
  84. let bLen = baseItems.length;
  85. for (let i = 0; i < bLen; i++)
  86. list.items.push(baseItems[i]);
  87. let extra = blueprint.items.extra;
  88. if (!extra)
  89. return;
  90. let eLen = extra.length;
  91. for (let i = 0; i < eLen; i++) {
  92. let e = extra[i];
  93. let item = extend({}, e);
  94. if (item.type === 'skin') {
  95. let skinBlueprint = skins.getBlueprint(item.skinId);
  96. item.skinId = item.skinId;
  97. item.name = skinBlueprint.name;
  98. item.sprite = skinBlueprint.sprite;
  99. } else if (item.generate) {
  100. let generated = generator.generate(item);
  101. if (item.worth)
  102. generated.worth = item.worth;
  103. if (item.infinite)
  104. generated.infinite = true;
  105. if (item.factions)
  106. generated.factions = item.factions;
  107. item = generated;
  108. }
  109. let id = 0;
  110. list.items.forEach(function (checkItem) {
  111. if (checkItem.id >= id)
  112. id = checkItem.id + 1;
  113. });
  114. item.id = id;
  115. list.items.push(item);
  116. }
  117. },
  118. findItem: function (itemId, sourceName) {
  119. let list = this.items[sourceName];
  120. if (!list)
  121. return null;
  122. return list.items.find(i => i.id === itemId);
  123. },
  124. removeItem: function (itemId, sourceName) {
  125. let list = this.items[sourceName];
  126. if (!sourceName)
  127. return null;
  128. return list.items.spliceFirstWhere(i => i.id === itemId);
  129. }
  130. };