選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

457 行
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. //Scale xp by source level so you can't just farm low level mobs (or get boosted on high level mobs).
  185. //Mobs that are farther then 10 levels from you, give no xp
  186. //We don't currently do this for quests/herb gathering
  187. var levelDelta = level - a.obj.stats.values.level;
  188. var amount = 0;
  189. if (Math.abs(levelDelta) <= 10)
  190. amount = ~~((get + (levelDelta * 10)) * Math.pow(1 - (Math.abs(levelDelta) / 10), 2));
  191. a.obj.stats.getXp(amount, this.obj);
  192. }
  193. a.obj.fireEvent('afterKillMob', target);
  194. }
  195. target.fireEvent('afterDeath');
  196. },
  197. die: function(source) {
  198. this.values.hp = this.values.hpMax;
  199. this.values.mana = this.values.manaMax;
  200. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  201. this.obj.syncer.setObject(false, 'stats', 'values', 'mana', this.values.mana);
  202. this.syncer.queue('onGetDamage', {
  203. id: this.obj.id,
  204. event: true,
  205. text: 'death'
  206. });
  207. this.syncer.queue('onDeath', {
  208. x: this.obj.x,
  209. y: this.obj.y,
  210. source: source.name
  211. }, [this.obj.serverId]);
  212. },
  213. takeDamage: function(damage, threatMult, source) {
  214. source.fireEvent('beforeDealDamage', damage, this.obj);
  215. this.obj.fireEvent('beforeTakeDamage', damage, source);
  216. //Maybe the attacker was stunned?
  217. if (damage.failed)
  218. return;
  219. //Maybe something else killed this mob already?
  220. if (this.obj.destroyed)
  221. return;
  222. var amount = damage.amount;
  223. if (amount > this.values.hp)
  224. amount = this.values.hp;
  225. this.values.hp -= amount;
  226. var recipients = [];
  227. if (this.obj.serverId != null)
  228. recipients.push(this.obj.serverId);
  229. if (source.serverId != null)
  230. recipients.push(source.serverId);
  231. if (recipients.length > 0) {
  232. this.syncer.queue('onGetDamage', {
  233. id: this.obj.id,
  234. source: source.id,
  235. crit: damage.crit,
  236. amount: amount
  237. }, recipients);
  238. }
  239. this.obj.aggro.tryEngage(source, amount, threatMult);
  240. var died = (this.values.hp <= 0);
  241. if (died) {
  242. var death = {
  243. success: true
  244. };
  245. this.obj.fireEvent('beforeDeath', death);
  246. if (death.success) {
  247. var deathEvent = {};
  248. if (source.player)
  249. source.stats.kill(this.obj);
  250. else
  251. this.obj.fireEvent('afterDeath', deathEvent);
  252. if (this.obj.player) {
  253. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  254. if (deathEvent.permadeath) {
  255. this.obj.auth.permadie();
  256. this.syncer.queue('onPermadeath', {
  257. source: source.name
  258. }, [this.obj.serverId]);
  259. } else
  260. this.values.hp = 0;
  261. this.obj.player.die(source, deathEvent.permadeath);
  262. } else {
  263. this.obj.effects.die();
  264. if (this.obj.spellbook)
  265. this.obj.spellbook.die();
  266. this.obj.destroyed = true;
  267. if (this.obj.inventory) {
  268. var aggroList = this.obj.aggro.list;
  269. var aLen = aggroList.length;
  270. var done = [];
  271. for (var i = 0; i < aLen; i++) {
  272. var a = aggroList[i].obj;
  273. if (done.some(d => d == a.serverId))
  274. continue;
  275. if ((a.social) && (a.social.party)) {
  276. a.social.party.forEach(function(p) {
  277. if (done.some(d => d == p))
  278. return;
  279. this.obj.inventory.dropBag(p, source);
  280. done.push(p);
  281. }, this);
  282. } else {
  283. if (a.serverId == null)
  284. continue;
  285. this.obj.inventory.dropBag(a.serverId, source);
  286. done.push(a.serverId);
  287. }
  288. }
  289. }
  290. }
  291. }
  292. } else {
  293. source.aggro.tryEngage(this.obj, 0);
  294. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', this.values.hp);
  295. }
  296. source.fireEvent('afterDealDamage', damage, this.obj);
  297. },
  298. getHp: function(heal, source) {
  299. var values = this.values;
  300. var hpMax = values.hpMax;
  301. if (values.hp >= hpMax)
  302. return;
  303. var amount = heal.amount;
  304. if (hpMax - values.hp < amount)
  305. amount = hpMax - values.hp;
  306. values.hp += amount;
  307. if (values.hp > hpMax)
  308. values.hp = hpMax;
  309. var recipients = [];
  310. if (this.obj.serverId != null)
  311. recipients.push(this.obj.serverId);
  312. if (source.serverId != null)
  313. recipients.push(source.serverId);
  314. if (recipients.length > 0) {
  315. this.syncer.queue('onGetDamage', {
  316. id: this.obj.id,
  317. source: source.id,
  318. heal: true,
  319. amount: amount,
  320. crit: heal.crit
  321. }, recipients);
  322. }
  323. //Add aggro to all our attackers
  324. var threat = amount * 0.4;
  325. var aggroList = this.obj.aggro.list;
  326. var aLen = aggroList.length;
  327. for (var i = 0; i < aLen; i++) {
  328. var a = aggroList[i].obj;
  329. a.aggro.tryEngage(source, threat);
  330. }
  331. this.obj.syncer.setObject(false, 'stats', 'values', 'hp', values.hp);
  332. },
  333. save: function() {
  334. if (this.sessionDuration) {
  335. this.stats.played = ~~(this.stats.played + this.sessionDuration);
  336. delete this.sessionDuration;
  337. }
  338. return {
  339. type: 'stats',
  340. values: this.values,
  341. stats: this.stats
  342. };
  343. },
  344. simplify: function(self) {
  345. var values = this.values;
  346. if (!self) {
  347. var result = {
  348. type: 'stats',
  349. values: {
  350. hp: values.hp,
  351. hpMax: values.hpMax,
  352. mana: values.mana,
  353. manaMax: values.manaMax,
  354. level: values.level
  355. }
  356. };
  357. return result
  358. }
  359. return {
  360. type: 'stats',
  361. values: values,
  362. stats: this.stats,
  363. vitScale: this.vitScale
  364. };
  365. }
  366. };
  367. });