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.
 
 
 

749 lines
18 KiB

  1. define([
  2. 'config/animations',
  3. 'config/loginRewards',
  4. 'config/classes'
  5. ], function (
  6. animations,
  7. loginRewards,
  8. classes
  9. ) {
  10. var baseStats = {
  11. mana: 20,
  12. manaMax: 20,
  13. manaReservePercent: 0,
  14. hp: 5,
  15. hpMax: 5,
  16. xpTotal: 0,
  17. xp: 0,
  18. xpMax: 0,
  19. level: 1,
  20. str: 0,
  21. int: 0,
  22. dex: 0,
  23. magicFind: 0,
  24. itemQuantity: 0,
  25. regenHp: 0,
  26. regenMana: 5,
  27. addCritChance: 0,
  28. addCritMultiplier: 0,
  29. critChance: 5,
  30. critMultiplier: 150,
  31. armor: 0,
  32. dmgPercent: 0,
  33. vit: 0,
  34. blockAttackChance: 0,
  35. blockSpellChance: 0,
  36. attackSpeed: 0,
  37. castSpeed: 0,
  38. elementArcanePercent: 0,
  39. elementFrostPercent: 0,
  40. elementFirePercent: 0,
  41. elementHolyPercent: 0,
  42. elementPoisonPercent: 0,
  43. elementArcaneResist: 0,
  44. elementFrostResist: 0,
  45. elementFireResist: 0,
  46. elementHolyResist: 0,
  47. elementPoisonResist: 0,
  48. elementAllResist: 0,
  49. sprintChance: 0,
  50. xpIncrease: 0,
  51. //Fishing stats
  52. catchChance: 0,
  53. catchSpeed: 0,
  54. fishRarity: 0,
  55. fishWeight: 0,
  56. fishItems: 0
  57. };
  58. return {
  59. type: 'stats',
  60. values: baseStats,
  61. originalValues: null,
  62. vitScale: 10,
  63. syncer: null,
  64. stats: {
  65. logins: 0,
  66. played: 0,
  67. lastLogin: null,
  68. loginStreak: 0,
  69. mobKillStreaks: {}
  70. },
  71. dead: false,
  72. init: function (blueprint, isTransfer) {
  73. this.syncer = this.obj.instance.syncer;
  74. var values = (blueprint || {}).values || {};
  75. for (var v in values) {
  76. this.values[v] = values[v];
  77. }
  78. var stats = (blueprint || {}).stats || {};
  79. for (var v in stats) {
  80. this.stats[v] = stats[v];
  81. }
  82. this.calcXpMax();
  83. if (blueprint)
  84. delete blueprint.stats;
  85. },
  86. resetHp: function () {
  87. var values = this.values;
  88. values.hp = values.hpMax;
  89. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  90. },
  91. update: function () {
  92. if (((this.obj.mob) && (!this.obj.follower)) || (this.dead))
  93. return;
  94. var values = this.values;
  95. var manaMax = values.manaMax;
  96. manaMax -= (manaMax * values.manaReservePercent);
  97. var regen = {
  98. success: true
  99. };
  100. this.obj.fireEvent('beforeRegen', regen);
  101. if (!regen.success)
  102. return;
  103. var isInCombat = (this.obj.aggro.list.length > 0);
  104. if (this.obj.follower) {
  105. isInCombat = (this.obj.follower.master.aggro.list.length > 0);
  106. if (isInCombat)
  107. return;
  108. }
  109. var regenHp = 0;
  110. var regenMana = 0;
  111. regenMana = values.regenMana / 50;
  112. if (!isInCombat)
  113. regenHp = Math.max(values.hpMax / 112, values.regenHp * 0.2);
  114. else
  115. regenHp = values.regenHp * 0.2;
  116. if (values.hp < values.hpMax) {
  117. values.hp += regenHp;
  118. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  119. }
  120. if (values.hp > values.hpMax) {
  121. values.hp = values.hpMax;
  122. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  123. }
  124. if (values.mana < manaMax) {
  125. values.mana += regenMana;
  126. //Show others what mana is?
  127. var onlySelf = true;
  128. if (this.obj.player)
  129. onlySelf = false;
  130. this.obj.syncer.setObject(onlySelf, 'stats', 'values', 'mana', values.mana);
  131. }
  132. if (values.mana > manaMax) {
  133. values.mana = manaMax;
  134. if (this.obj.player)
  135. onlySelf = false;
  136. this.obj.syncer.setObject(onlySelf, 'stats', 'values', 'mana', values.mana);
  137. }
  138. },
  139. addStat: function (stat, value) {
  140. if (['lvlRequire', 'allAttributes'].indexOf(stat) == -1)
  141. this.values[stat] += value;
  142. var sendOnlyToSelf = (['hp', 'hpMax', 'mana', 'manaMax', 'vit'].indexOf(stat) == -1);
  143. this.obj.syncer.setObject(sendOnlyToSelf, 'stats', 'values', stat, this.values[stat]);
  144. if (stat == 'addCritChance') {
  145. this.values.critChance += (0.05 * value);
  146. this.obj.syncer.setObject(true, 'stats', 'values', 'critChance', this.values.critChance);
  147. } else if (stat == 'addCritMultiplier') {
  148. this.values.critMultiplier += value;
  149. this.obj.syncer.setObject(true, 'stats', 'values', 'critMultiplier', this.values.critMultiplier);
  150. } else if (stat == 'vit') {
  151. this.values.hpMax += (value * this.vitScale);
  152. this.obj.syncer.setObject(true, 'stats', 'values', 'hpMax', this.values.hpMax);
  153. this.obj.syncer.setObject(false, 'stats', 'values', 'hpMax', this.values.hpMax);
  154. } else if (stat == 'allAttributes') {
  155. ['int', 'str', 'dex'].forEach(function (s) {
  156. this.values[s] += value;
  157. this.obj.syncer.setObject(true, 'stats', 'values', s, this.values[s]);
  158. }, this);
  159. } else if (stat == 'elementAllResist') {
  160. ['arcane', 'frost', 'fire', 'holy', 'poison'].forEach(function (s) {
  161. var element = 'element' + (s[0].toUpperCase() + s.substr(1)) + 'Resist';
  162. this.values[element] += value;
  163. this.obj.syncer.setObject(true, 'stats', 'values', element, this.values[element]);
  164. }, this);
  165. }
  166. },
  167. calcXpMax: function () {
  168. var level = (this.originalValues || this.values).level;
  169. this.values.xpMax = (level * 5) + ~~(level * 10 * Math.pow(level, 2.2));
  170. this.obj.syncer.setObject(true, 'stats', 'values', 'xpMax', this.values.xpMax);
  171. },
  172. //Source is the object that caused you to gain xp (mostly yourself)
  173. //Target is the source of the xp (a mob or quest)
  174. getXp: function (amount, source, target) {
  175. var obj = this.obj;
  176. var values = this.values;
  177. if ((this.originalValues || this.values).level == 20)
  178. return;
  179. var xpEvent = {
  180. source: source,
  181. target: target,
  182. amount: amount
  183. };
  184. this.obj.fireEvent('beforeGetXp', xpEvent);
  185. if (xpEvent.amount == 0)
  186. return;
  187. amount = ~~(xpEvent.amount * (1 + (values.xpIncrease / 100)));
  188. values.xpTotal = ~~(values.xpTotal + amount);
  189. values.xp = ~~(values.xp + amount);
  190. this.obj.syncer.setObject(true, 'stats', 'values', 'xp', values.xp);
  191. this.syncer.queue('onGetDamage', {
  192. id: obj.id,
  193. event: true,
  194. text: '+' + amount + ' xp'
  195. });
  196. var syncO = {};
  197. var didLevelUp = false;
  198. while (values.xp >= values.xpMax) {
  199. didLevelUp = true;
  200. values.xp -= values.xpMax;
  201. this.obj.syncer.setObject(true, 'stats', 'values', 'xp', values.xp);
  202. if (this.originalValues)
  203. this.originalValues.level++;
  204. else
  205. values.level++;
  206. if ((this.originalValues || this.values).level == 20)
  207. values.xp = 0;
  208. values.hpMax = values.level * 32.7;
  209. var gainStats = classes.stats[this.obj.class].gainStats;
  210. for (var s in gainStats) {
  211. values[s] += gainStats[s];
  212. }
  213. this.obj.spellbook.calcDps();
  214. this.syncer.queue('onGetDamage', {
  215. id: obj.id,
  216. event: true,
  217. text: 'level up'
  218. });
  219. syncO.level = (this.originalValues || this.values).level;
  220. this.calcXpMax();
  221. }
  222. if (didLevelUp) {
  223. var cellContents = obj.instance.physics.getCell(obj.x, obj.y);
  224. cellContents.forEach(function (c) {
  225. c.fireEvent('onCellPlayerLevelUp', obj);
  226. });
  227. obj.auth.doSave();
  228. }
  229. process.send({
  230. method: 'object',
  231. serverId: this.obj.serverId,
  232. obj: syncO
  233. });
  234. if (didLevelUp) {
  235. var maxLevel = this.obj.instance.zone.level[1]
  236. if (maxLevel < (this.originalValues || values).level)
  237. this.rescale(maxLevel, false);
  238. else {
  239. this.obj.syncer.setObject(true, 'stats', 'values', 'hpMax', values.hpMax);
  240. this.obj.syncer.setObject(true, 'stats', 'values', 'level', values.level);
  241. this.obj.syncer.setObject(false, 'stats', 'values', 'hpMax', values.hpMax);
  242. this.obj.syncer.setObject(false, 'stats', 'values', 'level', values.level);
  243. }
  244. }
  245. },
  246. kill: function (target) {
  247. var level = target.stats.values.level;
  248. //Who should get xp?
  249. var aggroList = target.aggro.list;
  250. var hpMax = target.stats.values.hpMax;
  251. var aLen = aggroList.length;
  252. for (var i = 0; i < aLen; i++) {
  253. var a = aggroList[i];
  254. var dmg = a.damage;
  255. if (dmg <= 0)
  256. continue;
  257. var mult = 1;
  258. //How many party members contributed
  259. // Remember, maybe one of the aggro-ees might be a mob too
  260. var party = a.obj.social ? a.obj.social.party : null;
  261. if (party) {
  262. var partySize = aggroList.filter(function (f) {
  263. return ((a.damage > 0) && (party.indexOf(f.obj.serverId) > -1));
  264. }).length;
  265. partySize--;
  266. mult = (1 + (partySize * 0.1));
  267. }
  268. if ((a.obj.stats) && (!a.obj.follower)) {
  269. //Scale xp by source level so you can't just farm low level mobs (or get boosted on high level mobs).
  270. //Mobs that are farther then 10 levels from you, give no xp
  271. //We don't currently do this for quests/herb gathering
  272. var sourceLevel = a.obj.stats.values.level;
  273. var levelDelta = level - sourceLevel;
  274. var amount = level * 10 * mult;
  275. if (Math.abs(levelDelta) <= 10)
  276. amount = ~~(((sourceLevel + levelDelta) * 10) * Math.pow(1 - (Math.abs(levelDelta) / 10), 2) * mult);
  277. else
  278. amount = 0;
  279. a.obj.stats.getXp(amount, this.obj, target);
  280. }
  281. a.obj.fireEvent('afterKillMob', target);
  282. }
  283. },
  284. die: function (source) {
  285. this.values.hp = this.values.hpMax;
  286. this.values.mana = this.values.manaMax;
  287. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  288. this.obj.syncer.setObject(false, 'stats', 'values', 'mana', this.values.mana);
  289. this.syncer.queue('onGetDamage', {
  290. id: this.obj.id,
  291. event: true,
  292. text: 'death'
  293. });
  294. this.syncer.queue('onDeath', {
  295. x: this.obj.x,
  296. y: this.obj.y,
  297. source: source.name
  298. }, [this.obj.serverId]);
  299. },
  300. takeDamage: function (damage, threatMult, source) {
  301. source.fireEvent('beforeDealDamage', damage, this.obj);
  302. this.obj.fireEvent('beforeTakeDamage', damage, source);
  303. //Maybe the attacker was stunned?
  304. if (damage.failed)
  305. return;
  306. //Maybe something else killed this mob already?
  307. if (this.obj.destroyed)
  308. return;
  309. var amount = damage.amount;
  310. if (amount > this.values.hp)
  311. amount = this.values.hp;
  312. this.values.hp -= amount;
  313. var recipients = [];
  314. if (this.obj.serverId != null)
  315. recipients.push(this.obj.serverId);
  316. if (source.serverId != null)
  317. recipients.push(source.serverId);
  318. if ((source.follower) && (source.follower.master.serverId))
  319. recipients.push(source.follower.master.serverId);
  320. if ((this.obj.follower) && (this.obj.follower.master.serverId))
  321. recipients.push(this.obj.follower.master.serverId);
  322. if (recipients.length > 0) {
  323. if (!damage.blocked) {
  324. this.syncer.queue('onGetDamage', {
  325. id: this.obj.id,
  326. source: source.id,
  327. crit: damage.crit,
  328. amount: amount
  329. }, recipients);
  330. } else {
  331. this.syncer.queue('onGetDamage', {
  332. id: this.obj.id,
  333. source: source.id,
  334. event: true,
  335. text: 'blocked'
  336. }, recipients);
  337. }
  338. }
  339. this.obj.aggro.tryEngage(source, amount, threatMult);
  340. var died = (this.values.hp <= 0);
  341. if (died) {
  342. var death = {
  343. success: true
  344. };
  345. this.obj.fireEvent('beforeDeath', death);
  346. if (death.success) {
  347. var deathEvent = {};
  348. var killSource = source;
  349. if (source.follower)
  350. killSource = source.follower.master;
  351. if (killSource.player)
  352. killSource.stats.kill(this.obj);
  353. this.obj.fireEvent('afterDeath', deathEvent);
  354. if (this.obj.player) {
  355. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  356. if (deathEvent.permadeath) {
  357. this.obj.auth.permadie();
  358. this.obj.instance.syncer.queue('onGetMessages', {
  359. messages: {
  360. class: 'color-red',
  361. message: `(level ${this.values.level}) ${this.obj.name} has forever left the shores of the living.`
  362. }
  363. });
  364. this.syncer.queue('onPermadeath', {
  365. source: killSource.name
  366. }, [this.obj.serverId]);
  367. } else
  368. this.values.hp = 0;
  369. this.obj.player.die(killSource, deathEvent.permadeath);
  370. } else {
  371. this.obj.effects.die();
  372. if (this.obj.spellbook)
  373. this.obj.spellbook.die();
  374. this.obj.destroyed = true;
  375. var deathAnimation = _.getDeepProperty(animations, ['mobs', this.obj.sheetName, this.obj.cell, 'death']);
  376. if (deathAnimation) {
  377. this.obj.instance.syncer.queue('onGetObject', {
  378. x: this.obj.x,
  379. y: this.obj.y,
  380. components: [deathAnimation]
  381. });
  382. }
  383. if (this.obj.inventory) {
  384. var aggroList = this.obj.aggro.list;
  385. var aLen = aggroList.length;
  386. for (var i = 0; i < aLen; i++) {
  387. var a = aggroList[i].obj;
  388. if (a.serverId == null)
  389. continue;
  390. this.obj.inventory.dropBag(a.serverId, killSource);
  391. }
  392. }
  393. }
  394. }
  395. } else {
  396. source.aggro.tryEngage(this.obj, 0);
  397. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  398. }
  399. if (!damage.noEvents)
  400. source.fireEvent('afterDealDamage', damage, this.obj);
  401. },
  402. getHp: function (heal, source) {
  403. var amount = heal.amount;
  404. if (amount == 0)
  405. return;
  406. var threatMult = heal.threatMult;
  407. if (!heal.hasOwnProperty('threatMult'))
  408. threatMult = 1;
  409. var values = this.values;
  410. var hpMax = values.hpMax;
  411. if (values.hp >= hpMax)
  412. return;
  413. if (hpMax - values.hp < amount)
  414. amount = hpMax - values.hp;
  415. values.hp += amount;
  416. if (values.hp > hpMax)
  417. values.hp = hpMax;
  418. var recipients = [];
  419. if (this.obj.serverId != null)
  420. recipients.push(this.obj.serverId);
  421. if (source.serverId != null)
  422. recipients.push(source.serverId);
  423. if (recipients.length > 0) {
  424. this.syncer.queue('onGetDamage', {
  425. id: this.obj.id,
  426. source: source.id,
  427. heal: true,
  428. amount: amount,
  429. crit: heal.crit
  430. }, recipients);
  431. }
  432. //Add aggro to all our attackers
  433. var threat = amount * 0.4 * threatMult;
  434. var aggroList = this.obj.aggro.list;
  435. var aLen = aggroList.length;
  436. for (var i = 0; i < aLen; i++) {
  437. var a = aggroList[i].obj;
  438. a.aggro.tryEngage(source, threat);
  439. }
  440. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  441. },
  442. save: function () {
  443. if (this.sessionDuration) {
  444. this.stats.played = ~~(this.stats.played + this.sessionDuration);
  445. delete this.sessionDuration;
  446. }
  447. return {
  448. type: 'stats',
  449. values: this.originalValues || this.values,
  450. stats: this.stats
  451. };
  452. },
  453. simplify: function (self) {
  454. var values = this.values;
  455. if (!self) {
  456. var result = {
  457. type: 'stats',
  458. values: {
  459. hp: values.hp,
  460. hpMax: values.hpMax,
  461. mana: values.mana,
  462. manaMax: values.manaMax,
  463. level: values.level
  464. }
  465. };
  466. return result
  467. }
  468. return {
  469. type: 'stats',
  470. values: values,
  471. stats: this.stats,
  472. vitScale: this.vitScale
  473. };
  474. },
  475. onLogin: function () {
  476. var stats = this.stats;
  477. var scheduler = require('misc/scheduler');
  478. var time = scheduler.getTime();
  479. var lastLogin = stats.lastLogin;
  480. if ((!lastLogin) || (lastLogin.day != time.day)) {
  481. var daysSkipped = 1;
  482. if (lastLogin) {
  483. if (time.day > lastLogin.day)
  484. daysSkipped = time.day - lastLogin.day;
  485. else {
  486. var daysInMonth = scheduler.daysInMonth(lastLogin.month);
  487. daysSkipped = (daysInMonth - lastLogin.day) + time.day;
  488. for (var i = lastLogin.month + 1; i < time.month - 1; i++) {
  489. daysSkipped += scheduler.daysInMonth(i);
  490. }
  491. }
  492. }
  493. if (daysSkipped == 1) {
  494. stats.loginStreak++;
  495. if (stats.loginStreak > 21)
  496. stats.loginStreak = 21;
  497. } else {
  498. stats.loginStreak -= (daysSkipped - 1);
  499. if (stats.loginStreak < 1)
  500. stats.loginStreak = 1;
  501. }
  502. var mail = this.obj.instance.mail;
  503. var rewards = loginRewards.generate(stats.loginStreak);
  504. mail.sendMail(this.obj.name, rewards);
  505. } else
  506. this.obj.instance.mail.getMail(this.obj.name);
  507. stats.lastLogin = time;
  508. },
  509. rescale: function (level, isMob) {
  510. if (level >= (this.originalValues || this.values).level)
  511. return;
  512. var sync = this.obj.syncer.setObject.bind(this.obj.syncer);
  513. var oldHp = this.values.hp;
  514. var oldXp = this.values.xp;
  515. var oldXpTotal = this.values.xpTotal;
  516. var oldXpMax = this.values.xpMax;
  517. if (!this.originalValues)
  518. this.originalValues = extend(true, {}, this.values);
  519. var oldValues = this.values;
  520. var newValues = extend(true, {}, baseStats);
  521. this.values = newValues;
  522. var gainStats = classes.stats[this.obj.class].gainStats;
  523. for (var s in gainStats) {
  524. newValues[s] += (gainStats[s] * level);
  525. }
  526. newValues.level = level;
  527. newValues.originalLevel = (this.originalValues || oldValues).level;
  528. newValues.hpMax = level * 32.7;
  529. if (isMob)
  530. newValues.hpMax = ~~(newValues.hpMax * (level / 10));
  531. newValues.hp = oldHp;
  532. var resetHp = false;
  533. if (newValues.hp > newValues.hpMax) {
  534. resetHp = true;
  535. newValues.hp = newValues.hpMax;
  536. }
  537. newValues.xp = oldXp;
  538. newValues.xpMax = oldXpMax;
  539. newValues.xpTotal = oldXpTotal;
  540. var addStats = this.obj.equipment.rescale(level);
  541. for (var p in addStats) {
  542. var statName = p;
  543. this.addStat(statName, addStats[p]);
  544. }
  545. if (resetHp)
  546. newValues.hp = newValues.hpMax;
  547. this.obj.spellbook.calcDps();
  548. var publicStats = [
  549. 'hp',
  550. 'hpMax',
  551. 'mana',
  552. 'manaMax',
  553. 'level'
  554. ];
  555. for (var p in newValues) {
  556. sync(true, 'stats', 'values', p, newValues[p]);
  557. if (publicStats.indexOf(p) > -1)
  558. sync(false, 'stats', 'values', p, newValues[p]);
  559. }
  560. },
  561. getKillStreakCoefficient: function (mobName) {
  562. var killStreak = this.stats.mobKillStreaks[mobName];
  563. if (!killStreak)
  564. return 1;
  565. else
  566. return Math.max(0, (10000 - Math.pow(killStreak, 2)) / 10000);
  567. },
  568. events: {
  569. afterKillMob: function (mob) {
  570. var mobKillStreaks = this.stats.mobKillStreaks;
  571. var mobName = mob.name;
  572. if (!mobKillStreaks[mobName])
  573. mobKillStreaks.mobName = 0;
  574. if (mobKillStreaks[mobName] < 100)
  575. mobKillStreaks[mobName]++;
  576. for (var p in mobKillStreaks) {
  577. if (p == mobName)
  578. continue;
  579. mobKillStreaks[p]--;
  580. if (mobKillStreaks[p] <= 0)
  581. delete mobKillStreaks[p];
  582. }
  583. },
  584. beforeGetXp: function (event) {
  585. if (!event.target.mob)
  586. return;
  587. event.amount *= this.getKillStreakCoefficient(event.target.name);
  588. },
  589. beforeGenerateLoot: function (event) {
  590. if (!event.source.mob)
  591. return;
  592. event.chanceMultiplier *= this.getKillStreakCoefficient(event.source.name);
  593. },
  594. afterMove: function (event) {
  595. var mobKillStreaks = this.stats.mobKillStreaks;
  596. for (var p in mobKillStreaks) {
  597. mobKillStreaks[p] -= 0.085;
  598. if (mobKillStreaks[p] <= 0)
  599. delete mobKillStreaks[p];
  600. }
  601. }
  602. }
  603. };
  604. });