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.
 
 
 

400 lines
7.7 KiB

  1. let components = require('../components/components');
  2. module.exports = {
  3. components: [],
  4. actionQueue: [],
  5. addComponent: function (type, blueprint, isTransfer) {
  6. let cpn = this[type];
  7. if (!cpn) {
  8. let template = components.components[type];
  9. if (!template) {
  10. template = extend({
  11. type: type
  12. }, blueprint || {});
  13. }
  14. cpn = extend({}, template);
  15. cpn.obj = this;
  16. this.components.push(cpn);
  17. this[cpn.type] = cpn;
  18. }
  19. if (cpn.init && this.has('instance'))
  20. cpn.init(blueprint || {}, isTransfer);
  21. else {
  22. for (let p in blueprint)
  23. cpn[p] = blueprint[p];
  24. }
  25. return cpn;
  26. },
  27. removeComponent: function (type) {
  28. let cpn = this[type];
  29. if (!cpn)
  30. return;
  31. cpn.destroyed = true;
  32. },
  33. extendComponent: function (ext, type, blueprint) {
  34. let template = require('../components/extensions/' + type);
  35. let cpn = this[ext];
  36. extend(cpn, template);
  37. if (template.init)
  38. cpn.init(blueprint);
  39. return cpn;
  40. },
  41. update: function () {
  42. let usedTurn = false;
  43. let cpns = this.components;
  44. let len = cpns.length;
  45. for (let i = 0; i < len; i++) {
  46. let c = cpns[i];
  47. if (c.destroyed) {
  48. this.syncer.setSelfArray(false, 'removeComponents', c.type);
  49. cpns.spliceWhere(f => (f === c));
  50. delete this[c.type];
  51. len--;
  52. i--;
  53. } else if (c.update) {
  54. if (c.update())
  55. usedTurn = true;
  56. }
  57. }
  58. if (!usedTurn)
  59. this.performQueue();
  60. },
  61. getSimple: function (self, isSave, isTransfer) {
  62. let s = this.simplify(null, self, isSave, isTransfer);
  63. if (this.instance)
  64. s.zoneId = this.instance.zoneId;
  65. if (self && !isSave && this.syncer) {
  66. this.syncer.oSelf.components
  67. .forEach(c => {
  68. if (!this[c.type])
  69. s.components.push(c);
  70. });
  71. }
  72. return s;
  73. },
  74. simplify: function (o, self, isSave, isTransfer) {
  75. let result = {};
  76. if (!o) {
  77. result.components = [];
  78. o = this;
  79. }
  80. let syncTypes = ['portrait', 'area'];
  81. for (let p in o) {
  82. let value = o[p];
  83. if (value === null)
  84. continue;
  85. let type = typeof (value);
  86. if (type === 'function')
  87. continue;
  88. else if (type !== 'object')
  89. result[p] = value;
  90. else {
  91. if (value.type) {
  92. if (!value.simplify) {
  93. if (self) {
  94. result.components.push({
  95. type: value.type
  96. });
  97. }
  98. } else {
  99. let component = null;
  100. if (isSave && value.save)
  101. component = value.save();
  102. else if (isTransfer && value.simplifyTransfer)
  103. component = value.simplifyTransfer();
  104. else
  105. component = value.simplify(self);
  106. if (value.destroyed) {
  107. if (!component) {
  108. component = {
  109. type: value.type
  110. };
  111. }
  112. component.destroyed = true;
  113. }
  114. if (component)
  115. result.components.push(component);
  116. }
  117. } else if (syncTypes.includes(p))
  118. result[p] = value;
  119. continue;
  120. }
  121. }
  122. return result;
  123. },
  124. sendEvent: function (event, data) {
  125. process.send({
  126. method: 'event',
  127. id: this.serverId,
  128. data: {
  129. event: event,
  130. data: data
  131. }
  132. });
  133. },
  134. queue: function (action) {
  135. if (action.list) {
  136. let type = action.action;
  137. let data = action.data;
  138. let dLen = data.length;
  139. for (let i = 0; i < dLen; i++) {
  140. let d = data[i];
  141. this.actionQueue.push({
  142. action: type,
  143. data: d
  144. });
  145. }
  146. return;
  147. }
  148. if (action.priority)
  149. this.actionQueue.splice(this.actionQueue.firstIndex(a => !a.priority), 0, action);
  150. else
  151. this.actionQueue.push(action);
  152. },
  153. dequeue: function () {
  154. if (this.actionQueue.length === 0)
  155. return null;
  156. return this.actionQueue.splice(0, 1)[0];
  157. },
  158. clearQueue: function () {
  159. if (this.has('serverId')) {
  160. this.instance.syncer.queue('onClearQueue', {
  161. id: this.id
  162. }, [this.serverId]);
  163. }
  164. this.actionQueue = [];
  165. },
  166. performAction: function (action) {
  167. if (action.instanceModule)
  168. return;
  169. let cpn = this[action.cpn];
  170. if (!cpn)
  171. return;
  172. cpn[action.method](action.data);
  173. },
  174. performQueue: function () {
  175. let q = this.dequeue();
  176. if (!q)
  177. return;
  178. if (q.action === 'move') {
  179. let maxDistance = 1;
  180. if ((this.actionQueue[0]) && (this.actionQueue[0].action === 'move')) {
  181. let moveEvent = {
  182. sprintChance: this.stats.values.sprintChance || 0
  183. };
  184. this.fireEvent('onBeforeTryMove', moveEvent);
  185. let physics = this.instance.physics;
  186. let sprintChance = moveEvent.sprintChance;
  187. do {
  188. if ((~~(Math.random() * 100) < sprintChance) && (!physics.isTileBlocking(q.data.x, q.data.y))) {
  189. q = this.dequeue();
  190. maxDistance++;
  191. }
  192. sprintChance -= 100;
  193. } while (sprintChance > 0 && this.actionQueue.length > 0);
  194. }
  195. q.maxDistance = maxDistance;
  196. let success = this.performMove(q);
  197. if (!success)
  198. this.clearQueue();
  199. } else if (q.action === 'clearQueue')
  200. this.clearQueue();
  201. else if (q.action === 'spell') {
  202. let success = this.spellbook.cast(q);
  203. if (!success)
  204. this.performQueue();
  205. }
  206. },
  207. performMove: function (action) {
  208. let data = action.data;
  209. let physics = this.instance.physics;
  210. if (!action.force) {
  211. if (physics.isTileBlocking(data.x, data.y))
  212. return false;
  213. data.success = true;
  214. this.fireEvent('beforeMove', data);
  215. if (data.success === false) {
  216. action.priority = true;
  217. this.queue(action);
  218. return true;
  219. }
  220. let maxDistance = action.maxDistance || 1;
  221. let deltaX = Math.abs(this.x - data.x);
  222. let deltaY = Math.abs(this.y - data.y);
  223. if (
  224. (
  225. (deltaX > maxDistance) ||
  226. (deltaY > maxDistance)
  227. ) ||
  228. (
  229. (deltaX === 0) &&
  230. (deltaY === 0)
  231. )
  232. )
  233. return false;
  234. }
  235. //Don't allow mob overlap during combat
  236. if ((this.mob) && (this.mob.target)) {
  237. if (physics.addObject(this, data.x, data.y)) {
  238. physics.removeObject(this, this.x, this.y);
  239. this.x = data.x;
  240. this.y = data.y;
  241. } else
  242. return false;
  243. } else {
  244. physics.removeObject(this, this.x, this.y, data.x, data.y);
  245. physics.addObject(this, data.x, data.y, this.x, this.y);
  246. this.x = data.x;
  247. this.y = data.y;
  248. }
  249. let syncer = this.syncer;
  250. syncer.o.x = this.x;
  251. syncer.o.y = this.y;
  252. if (this.aggro)
  253. this.aggro.move();
  254. this.fireEvent('afterMove');
  255. return true;
  256. },
  257. collisionEnter: function (obj) {
  258. let cpns = this.components;
  259. let cLen = cpns.length;
  260. for (let i = 0; i < cLen; i++) {
  261. let c = cpns[i];
  262. if (c.collisionEnter) {
  263. if (c.collisionEnter(obj))
  264. return true;
  265. }
  266. }
  267. },
  268. collisionExit: function (obj) {
  269. let cpns = this.components;
  270. let cLen = cpns.length;
  271. for (let i = 0; i < cLen; i++) {
  272. let c = cpns[i];
  273. if (c.collisionExit)
  274. c.collisionExit(obj);
  275. }
  276. },
  277. fireEvent: function (event) {
  278. let args = [].slice.call(arguments, 1);
  279. let cpns = this.components;
  280. let cLen = cpns.length;
  281. for (let i = 0; i < cLen; i++) {
  282. let cpn = cpns[i];
  283. let events = cpn.events;
  284. if (!events)
  285. continue;
  286. let callback = events[event];
  287. if (!callback)
  288. continue;
  289. callback.apply(cpn, args);
  290. }
  291. if (this.effects)
  292. this.effects.fireEvent(event, args);
  293. if (this.quests)
  294. this.quests.fireEvent(event, args);
  295. if (this.prophecies)
  296. this.prophecies.fireEvent(event, args);
  297. if (this.inventory)
  298. this.inventory.fireEvent(event, args);
  299. if (this.spellbook)
  300. this.spellbook.fireEvent(event, args);
  301. },
  302. destroy: function () {
  303. let cpns = this.components;
  304. let len = cpns.length;
  305. for (let i = 0; i < len; i++) {
  306. let c = cpns[i];
  307. if (c.destroy)
  308. c.destroy();
  309. }
  310. },
  311. toString: function () {
  312. let res = {};
  313. for (let p in this) {
  314. if (['components', 'syncer'].includes(p))
  315. continue;
  316. let val = this[p];
  317. let stringVal = (val && val.toString) ? val.toString() : val;
  318. const type = typeof(val);
  319. if (
  320. type !== 'function' &&
  321. (
  322. type !== 'object' ||
  323. val.type
  324. )
  325. )
  326. res[p] = stringVal;
  327. }
  328. return JSON.stringify(res, null, 4).split('"').join('') + '\r\n';
  329. }
  330. };