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.
 
 
 

323 lines
7.1 KiB

  1. let qualityGenerator = require('../items/generators/quality');
  2. module.exports = {
  3. type: 'gatherer',
  4. nodes: [],
  5. gathering: null,
  6. gatheringTtl: 0,
  7. gatheringTtlMax: 7,
  8. defaultTtlMax: 7,
  9. simplify: function () {
  10. return {
  11. type: 'gatherer'
  12. };
  13. },
  14. gather: function () {
  15. if (this.gathering !== null)
  16. return;
  17. let nodes = this.nodes;
  18. if (nodes.length === 0)
  19. return;
  20. let firstNode = nodes[0];
  21. this.gathering = firstNode;
  22. let ttlMax = firstNode.resourceNode.ttl || this.defaultTtlMax;
  23. if (firstNode.resourceNode.nodeType === 'fish') {
  24. let rod = this.obj.equipment.eq.tool;
  25. if (!rod) {
  26. process.send({
  27. method: 'events',
  28. data: {
  29. onGetAnnouncement: [{
  30. obj: {
  31. msg: 'You need a fishing rod to fish'
  32. },
  33. to: [this.obj.serverId]
  34. }]
  35. }
  36. });
  37. this.gathering = null;
  38. return;
  39. }
  40. rod = this.obj.inventory.findItem(rod);
  41. let statCatchSpeed = Math.min(150, this.obj.stats.values.catchSpeed);
  42. ttlMax *= (1 - (statCatchSpeed / 200));
  43. }
  44. this.gatheringTtlMax = ttlMax;
  45. this.gatheringTtl = this.gatheringTtlMax;
  46. },
  47. update: function () {
  48. let gathering = this.gathering;
  49. if (!gathering)
  50. return;
  51. if (gathering.destroyed) {
  52. this.gathering = null;
  53. this.gatheringTtl = 0;
  54. this.obj.syncer.set(false, 'gatherer', 'progress', 100);
  55. this.obj.syncer.set(true, 'gatherer', 'progress', 100);
  56. this.obj.syncer.set(true, 'gatherer', 'action', 'Fishing');
  57. return;
  58. }
  59. let isFish = (gathering.resourceNode.nodeType === 'fish');
  60. if (this.gatheringTtl > 0) {
  61. if ((this.gatheringTtl === this.gatheringTtlMax) && (gathering.width)) {
  62. ['x', 'y', 'width', 'height'].forEach(function (p) {
  63. this.obj.syncer.set(false, 'gatherer', p, gathering[p]);
  64. }, this);
  65. }
  66. this.gatheringTtl--;
  67. let progress = 100 - ~~((this.gatheringTtl / this.gatheringTtlMax) * 100);
  68. this.obj.syncer.set(true, 'gatherer', 'progress', progress);
  69. if (isFish)
  70. this.obj.syncer.set(true, 'gatherer', 'action', 'Fishing');
  71. return;
  72. }
  73. let resourceNode = gathering.resourceNode;
  74. let gatherResult = extend(true, {
  75. obj: gathering
  76. }, {
  77. nodeType: resourceNode.nodeType,
  78. blueprint: resourceNode.blueprint,
  79. xp: resourceNode.xp,
  80. items: gathering.inventory.items
  81. });
  82. this.obj.instance.eventEmitter.emitNoSticky('beforeGatherResource', gatherResult, this.obj);
  83. this.obj.fireEvent('beforeGatherResource', gatherResult, this.obj);
  84. this.obj.syncer.set(false, 'gatherer', 'progress', 100);
  85. if (isFish) {
  86. let rod = this.obj.equipment.eq.tool;
  87. rod = this.obj.inventory.findItem(rod);
  88. let catchChance = 40 + this.obj.stats.values.catchChance;
  89. if (~~(Math.random() * 100) >= catchChance) {
  90. process.send({
  91. method: 'events',
  92. data: {
  93. onGetAnnouncement: [{
  94. obj: {
  95. msg: 'The fish got away'
  96. },
  97. to: [this.obj.serverId]
  98. }]
  99. }
  100. });
  101. this.gathering = null;
  102. return;
  103. }
  104. gatherResult.items.forEach(function (g) {
  105. delete g.quantity;
  106. qualityGenerator.generate(g, {
  107. //100 x 2.86 = 2000 (chance for a common)
  108. bonusMagicFind: this.obj.stats.values.fishRarity * 2.82
  109. });
  110. g.name = {
  111. 0: '',
  112. 1: 'Big ',
  113. 2: 'Giant ',
  114. 3: 'Trophy ',
  115. 4: 'Fabled '
  116. }[g.quality] + g.name;
  117. let statFishWeight = 1 + (this.obj.stats.values.fishWeight / 100);
  118. let weight = ~~((gatherResult.blueprint.baseWeight + g.quality + (Math.random() * statFishWeight)) * 100) / 100;
  119. g.stats = {
  120. weight: weight
  121. };
  122. g.worth = ~~(weight * 10);
  123. }, this);
  124. }
  125. if (isFish) {
  126. let rod = this.obj.equipment.eq.tool;
  127. rod = this.obj.inventory.findItem(rod);
  128. let itemChance = 1 + this.obj.stats.values.fishItems;
  129. if (~~(Math.random() * 100) < itemChance) {
  130. gatherResult.items = [{
  131. name: 'Cerulean Pearl',
  132. material: true,
  133. quantity: 1,
  134. quality: 3,
  135. sprite: [11, 9]
  136. }];
  137. }
  138. }
  139. let blueprint = gatherResult.blueprint;
  140. gatherResult.items.forEach(function (item, i) {
  141. delete item.pos;
  142. if (i === 0) {
  143. if (blueprint.itemName)
  144. item.name = blueprint.itemName;
  145. if (blueprint.itemAmount)
  146. item.quantity = ~~(Math.random() * blueprint.itemAmount[1]) + blueprint.itemAmount[0];
  147. }
  148. this.obj.inventory.getItem(item);
  149. if (item.material)
  150. this.obj.fireEvent('afterGatherResource', gatherResult);
  151. }, this);
  152. if (!gatherResult.noChangeAmount)
  153. resourceNode.gather();
  154. this.obj.stats.getXp(gatherResult.xp, this.obj, gatherResult.obj);
  155. if (gathering.destroyed) {
  156. if (isFish) {
  157. process.send({
  158. method: 'events',
  159. data: {
  160. onGetAnnouncement: [{
  161. obj: {
  162. msg: 'The school has been depleted'
  163. },
  164. to: [this.obj.serverId]
  165. }]
  166. }
  167. });
  168. }
  169. this.nodes.spliceWhere(n => (n === gathering));
  170. }
  171. this.gathering = null;
  172. },
  173. enter: function (node) {
  174. let gatherResult = extend(true, {
  175. nodeName: node.name
  176. });
  177. this.obj.instance.eventEmitter.emitNoSticky('beforeEnterPool', gatherResult, this.obj);
  178. let nodeType = node.resourceNode.nodeType;
  179. let msg = `Press G to $ (${gatherResult.nodeName})`;
  180. msg = msg.replace('$', {
  181. herb: 'gather',
  182. fish: 'fish for'
  183. }[nodeType]);
  184. let success = true;
  185. if (nodeType === 'fish') {
  186. let rod = this.obj.equipment.eq.tool;
  187. if (rod === null) {
  188. success = false;
  189. msg = 'You need a fishing rod to fish';
  190. }
  191. }
  192. process.send({
  193. method: 'events',
  194. data: {
  195. onGetAnnouncement: [{
  196. obj: {
  197. msg: msg
  198. },
  199. to: [this.obj.serverId]
  200. }]
  201. }
  202. });
  203. this.nodes.spliceWhere(n => (n === node));
  204. this.nodes.push(node);
  205. },
  206. exit: function (node) {
  207. this.nodes.spliceWhere(n => (n === node));
  208. },
  209. events: {
  210. beforeRezone: function () {
  211. this.events.beforeMove.call(this);
  212. },
  213. beforeMove: function () {
  214. if (!this.gathering)
  215. return;
  216. ['x', 'y', 'width', 'height'].forEach(function (p) {
  217. this.obj.syncer.delete(false, 'gatherer', p);
  218. }, this);
  219. this.obj.syncer.set(true, 'gatherer', 'progress', 100);
  220. this.obj.syncer.set(false, 'gatherer', 'progress', 100);
  221. if (this.gathering.resourceNode.nodeType === 'fish')
  222. this.obj.syncer.set(true, 'gatherer', 'action', 'Fishing');
  223. this.gathering = null;
  224. },
  225. afterEquipItem: function (item) {
  226. let nodes = this.nodes;
  227. let nLen = nodes.length;
  228. for (let i = 0; i < nLen; i++) {
  229. let node = nodes[i];
  230. if (item.slot !== 'tool')
  231. continue;
  232. if (node.resourceNode.nodeType === 'fish') {
  233. let rod = this.obj.equipment.eq.tool;
  234. if (rod === null) {
  235. process.send({
  236. method: 'events',
  237. data: {
  238. onGetAnnouncement: [{
  239. obj: {
  240. msg: 'You need a fishing rod to fish'
  241. },
  242. to: [this.obj.serverId]
  243. }]
  244. }
  245. });
  246. if (this.gathering === node) {
  247. if (this.gathering.resourceNode.nodeType === 'fish')
  248. this.obj.syncer.set(true, 'gatherer', 'action', 'Fishing');
  249. this.gathering = null;
  250. this.obj.syncer.set(true, 'gatherer', 'progress', 100);
  251. this.obj.syncer.set(false, 'gatherer', 'progress', 100);
  252. }
  253. }
  254. }
  255. }
  256. },
  257. afterUnequipItem: function (item) {
  258. this.events.afterEquipItem.call(this, item);
  259. }
  260. }
  261. };