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.
 
 
 

737 line
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. }
  242. }
  243. },
  244. kill: function (target) {
  245. var level = target.stats.values.level;
  246. //Who should get xp?
  247. var aggroList = target.aggro.list;
  248. var hpMax = target.stats.values.hpMax;
  249. var aLen = aggroList.length;
  250. for (var i = 0; i < aLen; i++) {
  251. var a = aggroList[i];
  252. var dmg = a.damage;
  253. if (dmg <= 0)
  254. continue;
  255. var mult = 1;
  256. //How many party members contributed
  257. // Remember, maybe one of the aggro-ees might be a mob too
  258. var party = a.obj.social ? a.obj.social.party : null;
  259. if (party) {
  260. var partySize = aggroList.filter(function (f) {
  261. return ((a.damage > 0) && (party.indexOf(f.obj.serverId) > -1));
  262. }).length;
  263. partySize--;
  264. mult = (1 + (partySize * 0.1));
  265. }
  266. if ((a.obj.stats) && (!a.obj.follower)) {
  267. //Scale xp by source level so you can't just farm low level mobs (or get boosted on high level mobs).
  268. //Mobs that are farther then 10 levels from you, give no xp
  269. //We don't currently do this for quests/herb gathering
  270. var sourceLevel = a.obj.stats.values.level;
  271. var levelDelta = level - sourceLevel;
  272. var amount = level * 10 * mult;
  273. if (Math.abs(levelDelta) <= 10)
  274. amount = ~~(((sourceLevel + levelDelta) * 10) * Math.pow(1 - (Math.abs(levelDelta) / 10), 2) * mult);
  275. else
  276. amount = 0;
  277. a.obj.stats.getXp(amount, this.obj, target);
  278. }
  279. a.obj.fireEvent('afterKillMob', target);
  280. }
  281. },
  282. die: function (source) {
  283. this.values.hp = this.values.hpMax;
  284. this.values.mana = this.values.manaMax;
  285. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  286. this.obj.syncer.setObject(false, 'stats', 'values', 'mana', this.values.mana);
  287. this.syncer.queue('onGetDamage', {
  288. id: this.obj.id,
  289. event: true,
  290. text: 'death'
  291. });
  292. this.syncer.queue('onDeath', {
  293. x: this.obj.x,
  294. y: this.obj.y,
  295. source: source.name
  296. }, [this.obj.serverId]);
  297. },
  298. takeDamage: function (damage, threatMult, source) {
  299. source.fireEvent('beforeDealDamage', damage, this.obj);
  300. this.obj.fireEvent('beforeTakeDamage', damage, source);
  301. //Maybe the attacker was stunned?
  302. if (damage.failed)
  303. return;
  304. //Maybe something else killed this mob already?
  305. if (this.obj.destroyed)
  306. return;
  307. var amount = damage.amount;
  308. if (amount > this.values.hp)
  309. amount = this.values.hp;
  310. this.values.hp -= amount;
  311. var recipients = [];
  312. if (this.obj.serverId != null)
  313. recipients.push(this.obj.serverId);
  314. if (source.serverId != null)
  315. recipients.push(source.serverId);
  316. if ((source.follower) && (source.follower.master.serverId))
  317. recipients.push(source.follower.master.serverId);
  318. if ((this.obj.follower) && (this.obj.follower.master.serverId))
  319. recipients.push(this.obj.follower.master.serverId);
  320. if (recipients.length > 0) {
  321. if (!damage.blocked) {
  322. this.syncer.queue('onGetDamage', {
  323. id: this.obj.id,
  324. source: source.id,
  325. crit: damage.crit,
  326. amount: amount
  327. }, recipients);
  328. } else {
  329. this.syncer.queue('onGetDamage', {
  330. id: this.obj.id,
  331. source: source.id,
  332. event: true,
  333. text: 'blocked'
  334. }, recipients);
  335. }
  336. }
  337. this.obj.aggro.tryEngage(source, amount, threatMult);
  338. var died = (this.values.hp <= 0);
  339. if (died) {
  340. var death = {
  341. success: true
  342. };
  343. this.obj.fireEvent('beforeDeath', death);
  344. if (death.success) {
  345. var deathEvent = {};
  346. var killSource = source;
  347. if (source.follower)
  348. killSource = source.follower.master;
  349. if (killSource.player)
  350. killSource.stats.kill(this.obj);
  351. this.obj.fireEvent('afterDeath', deathEvent);
  352. if (this.obj.player) {
  353. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  354. if (deathEvent.permadeath) {
  355. this.obj.auth.permadie();
  356. this.obj.instance.syncer.queue('onGetMessages', {
  357. messages: {
  358. class: 'color-red',
  359. message: `(level ${this.values.level}) ${this.obj.name} has forever left the shores of the living.`
  360. }
  361. });
  362. this.syncer.queue('onPermadeath', {
  363. source: killSource.name
  364. }, [this.obj.serverId]);
  365. } else
  366. this.values.hp = 0;
  367. this.obj.player.die(killSource, deathEvent.permadeath);
  368. } else {
  369. this.obj.effects.die();
  370. if (this.obj.spellbook)
  371. this.obj.spellbook.die();
  372. this.obj.destroyed = true;
  373. var deathAnimation = _.getDeepProperty(animations, ['mobs', this.obj.sheetName, this.obj.cell, 'death']);
  374. if (deathAnimation) {
  375. this.obj.instance.syncer.queue('onGetObject', {
  376. x: this.obj.x,
  377. y: this.obj.y,
  378. components: [deathAnimation]
  379. });
  380. }
  381. if (this.obj.inventory) {
  382. var aggroList = this.obj.aggro.list;
  383. var aLen = aggroList.length;
  384. for (var i = 0; i < aLen; i++) {
  385. var a = aggroList[i].obj;
  386. if (a.serverId == null)
  387. continue;
  388. this.obj.inventory.dropBag(a.serverId, killSource);
  389. }
  390. }
  391. }
  392. }
  393. } else {
  394. source.aggro.tryEngage(this.obj, 0);
  395. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  396. }
  397. if (!damage.noEvents)
  398. source.fireEvent('afterDealDamage', damage, this.obj);
  399. },
  400. getHp: function (heal, source) {
  401. var amount = heal.amount;
  402. if (amount == 0)
  403. return;
  404. var threatMult = heal.threatMult;
  405. if (!heal.hasOwnProperty('threatMult'))
  406. threatMult = 1;
  407. var values = this.values;
  408. var hpMax = values.hpMax;
  409. if (values.hp >= hpMax)
  410. return;
  411. if (hpMax - values.hp < amount)
  412. amount = hpMax - values.hp;
  413. values.hp += amount;
  414. if (values.hp > hpMax)
  415. values.hp = hpMax;
  416. var recipients = [];
  417. if (this.obj.serverId != null)
  418. recipients.push(this.obj.serverId);
  419. if (source.serverId != null)
  420. recipients.push(source.serverId);
  421. if (recipients.length > 0) {
  422. this.syncer.queue('onGetDamage', {
  423. id: this.obj.id,
  424. source: source.id,
  425. heal: true,
  426. amount: amount,
  427. crit: heal.crit
  428. }, recipients);
  429. }
  430. //Add aggro to all our attackers
  431. var threat = amount * 0.4 * threatMult;
  432. var aggroList = this.obj.aggro.list;
  433. var aLen = aggroList.length;
  434. for (var i = 0; i < aLen; i++) {
  435. var a = aggroList[i].obj;
  436. a.aggro.tryEngage(source, threat);
  437. }
  438. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  439. },
  440. save: function () {
  441. if (this.sessionDuration) {
  442. this.stats.played = ~~(this.stats.played + this.sessionDuration);
  443. delete this.sessionDuration;
  444. }
  445. return {
  446. type: 'stats',
  447. values: this.originalValues || this.values,
  448. stats: this.stats
  449. };
  450. },
  451. simplify: function (self) {
  452. var values = this.values;
  453. if (!self) {
  454. var result = {
  455. type: 'stats',
  456. values: {
  457. hp: values.hp,
  458. hpMax: values.hpMax,
  459. mana: values.mana,
  460. manaMax: values.manaMax,
  461. level: values.level
  462. }
  463. };
  464. return result
  465. }
  466. return {
  467. type: 'stats',
  468. values: values,
  469. stats: this.stats,
  470. vitScale: this.vitScale
  471. };
  472. },
  473. onLogin: function () {
  474. var stats = this.stats;
  475. var scheduler = require('misc/scheduler');
  476. var time = scheduler.getTime();
  477. var lastLogin = stats.lastLogin;
  478. if ((!lastLogin) || (lastLogin.day != time.day)) {
  479. var daysSkipped = 1;
  480. if (lastLogin) {
  481. if (time.day > lastLogin.day)
  482. daysSkipped = time.day - lastLogin.day;
  483. else {
  484. var daysInMonth = scheduler.daysInMonth(lastLogin.month);
  485. daysSkipped = (daysInMonth - lastLogin.day) + time.day;
  486. for (var i = lastLogin.month + 1; i < time.month - 1; i++) {
  487. daysSkipped += scheduler.daysInMonth(i);
  488. }
  489. }
  490. }
  491. if (daysSkipped == 1) {
  492. stats.loginStreak++;
  493. if (stats.loginStreak > 21)
  494. stats.loginStreak = 21;
  495. } else {
  496. stats.loginStreak -= (daysSkipped - 1);
  497. if (stats.loginStreak < 1)
  498. stats.loginStreak = 1;
  499. }
  500. var mail = this.obj.instance.mail;
  501. var rewards = loginRewards.generate(stats.loginStreak);
  502. mail.sendMail(this.obj.name, rewards);
  503. } else
  504. this.obj.instance.mail.getMail(this.obj.name);
  505. stats.lastLogin = time;
  506. },
  507. rescale: function (level, isMob) {
  508. if (level >= (this.originalValues || this.values).level)
  509. return;
  510. var sync = this.obj.syncer.setObject.bind(this.obj.syncer);
  511. var oldHp = this.values.hp;
  512. var oldXp = this.values.xp;
  513. var oldXpTotal = this.values.xpTotal;
  514. var oldXpMax = this.values.xpMax;
  515. if (!this.originalValues)
  516. this.originalValues = extend(true, {}, this.values);
  517. var oldValues = this.values;
  518. var newValues = extend(true, {}, baseStats);
  519. this.values = newValues;
  520. var gainStats = classes.stats[this.obj.class].gainStats;
  521. for (var s in gainStats) {
  522. newValues[s] += (gainStats[s] * level);
  523. }
  524. newValues.level = level;
  525. newValues.originalLevel = (this.originalValues || oldValues).level;
  526. newValues.hpMax = level * 32.7;
  527. if (isMob)
  528. newValues.hpMax = ~~(newValues.hpMax * (level / 10));
  529. newValues.hp = oldHp;
  530. var resetHp = false;
  531. if (newValues.hp > newValues.hpMax) {
  532. resetHp = true;
  533. newValues.hp = newValues.hpMax;
  534. }
  535. newValues.xp = oldXp;
  536. newValues.xpMax = oldXpMax;
  537. newValues.xpTotal = oldXpTotal;
  538. var addStats = this.obj.equipment.rescale(level);
  539. for (var p in addStats) {
  540. var statName = p;
  541. this.addStat(statName, addStats[p]);
  542. }
  543. if (resetHp)
  544. newValues.hp = newValues.hpMax;
  545. this.obj.spellbook.calcDps();
  546. for (var p in newValues) {
  547. sync(true, 'stats', 'values', p, newValues[p]);
  548. }
  549. },
  550. getKillStreakCoefficient: function (mobName) {
  551. var killStreak = this.stats.mobKillStreaks[mobName];
  552. if (!killStreak)
  553. return 1;
  554. else
  555. return Math.max(0, (10000 - Math.pow(killStreak, 2)) / 10000);
  556. },
  557. events: {
  558. afterKillMob: function (mob) {
  559. var mobKillStreaks = this.stats.mobKillStreaks;
  560. var mobName = mob.name;
  561. if (!mobKillStreaks[mobName])
  562. mobKillStreaks.mobName = 0;
  563. if (mobKillStreaks[mobName] < 100)
  564. mobKillStreaks[mobName]++;
  565. for (var p in mobKillStreaks) {
  566. if (p == mobName)
  567. continue;
  568. mobKillStreaks[p]--;
  569. if (mobKillStreaks[p] <= 0)
  570. delete mobKillStreaks[p];
  571. }
  572. },
  573. beforeGetXp: function (event) {
  574. if (!event.target.mob)
  575. return;
  576. event.amount *= this.getKillStreakCoefficient(event.target.name);
  577. },
  578. beforeGenerateLoot: function (event) {
  579. if (!event.source.mob)
  580. return;
  581. event.chanceMultiplier *= this.getKillStreakCoefficient(event.source.name);
  582. },
  583. afterMove: function (event) {
  584. var mobKillStreaks = this.stats.mobKillStreaks;
  585. for (var p in mobKillStreaks) {
  586. mobKillStreaks[p] -= 0.08;
  587. if (mobKillStreaks[p] <= 0)
  588. delete mobKillStreaks[p];
  589. }
  590. }
  591. }
  592. };
  593. });