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.
 
 
 

448 lines
10 KiB

  1. define([
  2. ], function(
  3. ) {
  4. return {
  5. type: 'stats',
  6. values: {
  7. mana: 10,
  8. manaMax: 10,
  9. hp: 5,
  10. hpMax: 5,
  11. xpTotal: 0,
  12. xp: 0,
  13. xpMax: 0,
  14. level: 1,
  15. str: 0,
  16. int: 0,
  17. dex: 0,
  18. magicFind: 0,
  19. regenHp: 0,
  20. regenMana: 10,
  21. addCritChance: 0,
  22. critChance: 5,
  23. armor: 0,
  24. dmgPercent: 0,
  25. elementArcanePercent: 0,
  26. elementFrostPercent: 0,
  27. elementFirePercent: 0,
  28. elementHolyPercent: 0,
  29. elementPhysicalPercent: 0,
  30. elementPoisonPercent: 0,
  31. elementArcaneResist: 0,
  32. elementFrostResist: 0,
  33. elementFireResist: 0,
  34. elementHolyResist: 0,
  35. elementPhysicalResist: 0,
  36. elementPoisonResist: 0,
  37. elementAllResist: 0,
  38. sprintChance: 0,
  39. xpIncrease: 0
  40. },
  41. vitScale: 10,
  42. syncer: null,
  43. stats: {
  44. logins: 0,
  45. played: 0
  46. },
  47. dead: false,
  48. init: function(blueprint) {
  49. this.syncer = this.obj.instance.syncer;
  50. var values = (blueprint || {}).values || {};
  51. for (var v in values) {
  52. this.values[v] = values[v];
  53. }
  54. this.calcXpMax();
  55. },
  56. resetHp: function() {
  57. var values = this.values;
  58. values.hp = values.hpMax;
  59. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  60. },
  61. update: function() {
  62. if ((this.obj.mob) || (this.dead))
  63. return;
  64. var regen = {
  65. success: true
  66. };
  67. this.obj.fireEvent('beforeRegen', regen);
  68. if (!regen.success)
  69. return;
  70. var values = this.values;
  71. var isInCombat = (this.obj.aggro.list.length > 0);
  72. var regenHp = 0;
  73. var regenMana = 0;
  74. regenMana = (values.manaMax / 200) + (values.regenMana / 200);
  75. if (!isInCombat)
  76. regenHp = values.hpMax / 100;
  77. else
  78. regenHp = values.regenHp * 0.3;
  79. if (values.hp < values.hpMax) {
  80. values.hp += regenHp;
  81. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  82. }
  83. if (values.hp > values.hpMax) {
  84. values.hp = values.hpMax;
  85. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  86. }
  87. if (values.mana < values.manaMax) {
  88. values.mana += regenMana;
  89. //Show others what mana is?
  90. var onlySelf = true;
  91. if (this.obj.player)
  92. onlySelf = false;
  93. this.obj.syncer.setObject(onlySelf, 'stats', 'values', 'mana', values.mana);
  94. }
  95. if (values.mana > values.manaMax) {
  96. values.mana = values.manaMax;
  97. if (this.obj.player)
  98. onlySelf = false;
  99. this.obj.syncer.setObject(onlySelf, 'stats', 'values', 'mana', values.mana);
  100. }
  101. },
  102. addStat: function(stat, value) {
  103. this.values[stat] += value;
  104. var sendOnlyToSelf = (['hp', 'hpMax', 'mana', 'manaMax'].indexOf(stat) == -1);
  105. this.obj.syncer.setObject(sendOnlyToSelf, 'stats', 'values', stat, this.values[stat]);
  106. if (stat == 'addCritChance') {
  107. this.values.critChance += (0.05 * value);
  108. this.obj.syncer.setObject(true, 'stats', 'values', 'critChance', this.values.critChance);
  109. } else if (stat == 'vit') {
  110. this.values.hpMax += (value * this.vitScale);
  111. this.obj.syncer.setObject(true, 'stats', 'values', 'hpMax', this.values.hpMax);
  112. } else if (stat == 'allAttributes') {
  113. ['int', 'str', 'dex'].forEach(function(s) {
  114. this.values[s] += value;
  115. this.obj.syncer.setObject(true, 'stats', 'values', s, this.values[s]);
  116. }, this);
  117. }
  118. },
  119. calcXpMax: function() {
  120. var level = this.values.level;
  121. this.values.xpMax = ~~(level * 10 * Math.pow(level, 1.75));
  122. this.obj.syncer.setObject(true, 'stats', 'values', 'xpMax', this.values.xpMax);
  123. },
  124. getXp: function(amount) {
  125. amount = ~~(amount * (1 + (this.values.xpIncrease / 100)));
  126. this.values.xpTotal = ~~(this.values.xpTotal + amount);
  127. this.values.xp = ~~(this.values.xp + amount);
  128. this.syncer.queue('onGetDamage', {
  129. id: this.obj.id,
  130. event: true,
  131. text: '+' + amount + ' xp'
  132. });
  133. var syncO = {};
  134. var didLevelUp = false;
  135. while (this.values.xp >= this.values.xpMax) {
  136. didLevelUp = true;
  137. this.values.xp -= this.values.xpMax;
  138. this.values.level++;
  139. this.values.hpMax += 40;
  140. this.syncer.queue('onGetDamage', {
  141. id: this.obj.id,
  142. event: true,
  143. text: 'level up'
  144. });
  145. this.obj.syncer.setObject(true, 'stats', 'values', 'level', this.values.level);
  146. this.obj.syncer.setObject(true, 'stats', 'values', 'hpMax', this.values.hpMax);
  147. syncO.level = this.values.level;
  148. this.calcXpMax();
  149. }
  150. if (didLevelUp)
  151. this.obj.auth.doSave();
  152. this.obj.syncer.setObject(true, 'stats', 'values', 'xp', this.values.xp);
  153. process.send({
  154. method: 'object',
  155. serverId: this.obj.serverId,
  156. obj: syncO
  157. });
  158. },
  159. kill: function(target) {
  160. var level = target.stats.values.level;
  161. var inc = level * 10;
  162. //Who should get xp?
  163. var aggroList = target.aggro.list;
  164. var hpMax = target.stats.values.hpMax;
  165. var aLen = aggroList.length;
  166. for (var i = 0; i < aLen; i++) {
  167. var a = aggroList[i];
  168. var dmg = a.damage;
  169. if (dmg <= 0)
  170. continue;
  171. var get = inc;
  172. //How many party members contributed
  173. // Remember, maybe one of the aggro-ees might be a mob too
  174. var party = a.obj.social ? a.obj.social.party : null;
  175. if (party) {
  176. var mult = aggroList.filter(function(f) {
  177. return ((a.damage > 0) && (party.indexOf(f.obj.serverId) > -1));
  178. }).length;
  179. mult--;
  180. get *= (1 + (mult * 0.1));
  181. get = ~~get;
  182. }
  183. if (a.obj.stats)
  184. a.obj.stats.getXp(inc);
  185. a.obj.fireEvent('afterKillMob', target);
  186. }
  187. target.fireEvent('afterDeath');
  188. },
  189. die: function(source) {
  190. this.values.hp = this.values.hpMax;
  191. this.values.mana = this.values.manaMax;
  192. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  193. this.obj.syncer.setObject(false, 'stats', 'values', 'mana', this.values.mana);
  194. this.syncer.queue('onGetDamage', {
  195. id: this.obj.id,
  196. event: true,
  197. text: 'death'
  198. });
  199. this.syncer.queue('onDeath', {
  200. x: this.obj.x,
  201. y: this.obj.y,
  202. source: source.name
  203. }, [this.obj.serverId]);
  204. },
  205. takeDamage: function(damage, threatMult, source) {
  206. source.fireEvent('beforeDealDamage', damage, this.obj);
  207. this.obj.fireEvent('beforeTakeDamage', damage, source);
  208. //Maybe the attacker was stunned?
  209. if (damage.failed)
  210. return;
  211. //Maybe something else killed this mob already?
  212. if (this.obj.destroyed)
  213. return;
  214. var amount = damage.amount;
  215. if (amount > this.values.hp)
  216. amount = this.values.hp;
  217. this.values.hp -= amount;
  218. var recipients = [];
  219. if (this.obj.serverId != null)
  220. recipients.push(this.obj.serverId);
  221. if (source.serverId != null)
  222. recipients.push(source.serverId);
  223. if (recipients.length > 0) {
  224. this.syncer.queue('onGetDamage', {
  225. id: this.obj.id,
  226. source: source.id,
  227. crit: damage.crit,
  228. amount: amount
  229. }, recipients);
  230. }
  231. this.obj.aggro.tryEngage(source, amount, threatMult);
  232. var died = (this.values.hp <= 0);
  233. if (died) {
  234. var death = {
  235. success: true
  236. };
  237. this.obj.fireEvent('beforeDeath', death);
  238. if (death.success) {
  239. var deathEvent = {};
  240. if (source.player)
  241. source.stats.kill(this.obj);
  242. else
  243. this.obj.fireEvent('afterDeath', deathEvent);
  244. if (this.obj.player) {
  245. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  246. if (deathEvent.permadeath) {
  247. this.obj.auth.permadie();
  248. this.syncer.queue('onPermadeath', {
  249. source: source.name
  250. }, [this.obj.serverId]);
  251. } else
  252. this.values.hp = 0;
  253. this.obj.player.die(source, deathEvent.permadeath);
  254. } else {
  255. this.obj.effects.die();
  256. if (this.obj.spellbook)
  257. this.obj.spellbook.die();
  258. this.obj.destroyed = true;
  259. if (this.obj.inventory) {
  260. var aggroList = this.obj.aggro.list;
  261. var aLen = aggroList.length;
  262. var done = [];
  263. for (var i = 0; i < aLen; i++) {
  264. var a = aggroList[i].obj;
  265. if (done.some(d => d == a.serverId))
  266. continue;
  267. if ((a.social) && (a.social.party)) {
  268. a.social.party.forEach(function(p) {
  269. if (done.some(d => d == p))
  270. return;
  271. this.obj.inventory.dropBag(p, source);
  272. done.push(p);
  273. }, this);
  274. } else {
  275. if (a.serverId == null)
  276. continue;
  277. this.obj.inventory.dropBag(a.serverId, source);
  278. done.push(a.serverId);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. } else {
  285. source.aggro.tryEngage(this.obj, 0);
  286. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  287. }
  288. source.fireEvent('afterDealDamage', damage, this.obj);
  289. },
  290. getHp: function(heal, source) {
  291. var values = this.values;
  292. var hpMax = values.hpMax;
  293. if (values.hp >= hpMax)
  294. return;
  295. var amount = heal.amount;
  296. if (hpMax - values.hp < amount)
  297. amount = hpMax - values.hp;
  298. values.hp += amount;
  299. if (values.hp > hpMax)
  300. values.hp = hpMax;
  301. var recipients = [];
  302. if (this.obj.serverId != null)
  303. recipients.push(this.obj.serverId);
  304. if (source.serverId != null)
  305. recipients.push(source.serverId);
  306. if (recipients.length > 0) {
  307. this.syncer.queue('onGetDamage', {
  308. id: this.obj.id,
  309. source: source.id,
  310. heal: true,
  311. amount: amount,
  312. crit: heal.crit
  313. }, recipients);
  314. }
  315. //Add aggro to all our attackers
  316. var threat = amount * 0.4;
  317. var aggroList = this.obj.aggro.list;
  318. var aLen = aggroList.length;
  319. for (var i = 0; i < aLen; i++) {
  320. var a = aggroList[i].obj;
  321. a.aggro.tryEngage(source, threat);
  322. }
  323. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  324. },
  325. save: function() {
  326. if (this.sessionDuration) {
  327. this.stats.played = ~~(this.stats.played + this.sessionDuration);
  328. delete this.sessionDuration;
  329. }
  330. return {
  331. type: 'stats',
  332. values: this.values,
  333. stats: this.stats
  334. };
  335. },
  336. simplify: function(self) {
  337. var values = this.values;
  338. if (!self) {
  339. var result = {
  340. type: 'stats',
  341. values: {
  342. hp: values.hp,
  343. hpMax: values.hpMax,
  344. mana: values.mana,
  345. manaMax: values.manaMax,
  346. level: values.level
  347. }
  348. };
  349. return result
  350. }
  351. return {
  352. type: 'stats',
  353. values: values,
  354. stats: this.stats,
  355. vitScale: this.vitScale
  356. };
  357. }
  358. };
  359. });