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.
 
 
 

250 regels
4.6 KiB

  1. const { viewDistanceX, viewDistanceY } = consts;
  2. module.exports = {
  3. buffer: {},
  4. dirty: false,
  5. init: function (msg) {
  6. this.objects = msg.objects;
  7. },
  8. update: function () {
  9. let objects = this.objects;
  10. let oList = objects.objects;
  11. let oLen = oList.length;
  12. let pList = oList.filter(f => f.player && !f.destroyed);
  13. let pLen = pList.length;
  14. if (pLen === 0)
  15. this.updateZoneEmpty(objects, oList, oLen);
  16. else if (pLen > 0)
  17. this.updateZoneNotEmpty(objects, oList, oLen, pList, pLen);
  18. oLen = oList.length;
  19. for (let i = 0; i < oList.length; i++)
  20. oList[i].syncer.reset();
  21. },
  22. updateZoneEmpty: function (objects, oList, oLen) {
  23. for (let i = 0; i < oLen; i++) {
  24. let o = oList[i];
  25. if (!o.destroyed)
  26. continue;
  27. objects.removeObject(o);
  28. oLen--;
  29. i--;
  30. }
  31. this.sendServerModuleMessages();
  32. },
  33. updateZoneNotEmpty: function (objects, oList, oLen, pList, pLen) {
  34. let queueFunction = this.queue.bind(this, 'onGetObject');
  35. let cache = {};
  36. for (let i = 0; i < oLen; i++) {
  37. let o = oList[i];
  38. let canBeSeenBy = o.canBeSeenBy;
  39. let oId = o.id;
  40. let ox = o.x;
  41. let oy = o.y;
  42. if (!o.syncer)
  43. continue;
  44. let destroyed = o.destroyed;
  45. let destroyedBy = o.destroyedBy;
  46. let sync = null;
  47. let syncSelf = null;
  48. if (!destroyed) {
  49. sync = o.syncer.get();
  50. syncSelf = o.syncer.get(true);
  51. o.syncer.locked = true;
  52. } else {
  53. sync = {
  54. id: o.id,
  55. destroyed: true,
  56. destroyedBy
  57. };
  58. objects.removeObject(o);
  59. oLen--;
  60. i--;
  61. }
  62. let toList = [];
  63. let completeList = [];
  64. let completeObj = null;
  65. let sendTo = false;
  66. let sendComplete = false;
  67. for (let j = 0; j < pLen; j++) {
  68. let p = pList[j];
  69. let px = p.x;
  70. let py = p.y;
  71. const canSee = (
  72. Math.abs(ox - px) <= viewDistanceX && Math.abs(oy - py) < viewDistanceY &&
  73. (
  74. !canBeSeenBy ||
  75. canBeSeenBy === p.name
  76. )
  77. );
  78. let hasSeen = p.player.hasSeen(oId);
  79. if (hasSeen) {
  80. if (canSee) {
  81. if (p.id === oId && syncSelf)
  82. queueFunction(syncSelf, [ p.serverId ]);
  83. if (sync) {
  84. toList.push(p.serverId);
  85. sendTo = true;
  86. }
  87. }
  88. if (destroyed || !canSee) {
  89. if (!canSee)
  90. queueFunction({ id: oId, destroyed: true }, [ p.serverId ]);
  91. p.player.unsee(oId);
  92. }
  93. } else if (!destroyed && canSee) {
  94. let cached = null;
  95. if (p.id === oId) {
  96. let syncO = o.getSimple(true);
  97. syncO.self = true;
  98. queueFunction(syncO, [ p.serverId ]);
  99. p.player.see(oId);
  100. continue;
  101. } else {
  102. cached = cache[oId];
  103. if (!cached)
  104. cached = cache[oId] = o.getSimple();
  105. }
  106. completeObj = cached;
  107. completeList.push(p.serverId);
  108. sendComplete = true;
  109. p.player.see(oId);
  110. }
  111. }
  112. if (sendTo)
  113. queueFunction(sync, toList);
  114. if (sendComplete)
  115. queueFunction(completeObj, completeList);
  116. }
  117. this.send();
  118. },
  119. queue: function (event, obj, to) {
  120. //Send to all players in zone?
  121. if (to === -1) {
  122. let pList = this.objects.objects.filter(o => o.player);
  123. to = pList.map(p => p.serverId);
  124. }
  125. if (!to.length)
  126. return;
  127. this.dirty = true;
  128. let buffer = this.buffer;
  129. let list = buffer[event] || (buffer[event] = []);
  130. list.push({
  131. to: to,
  132. obj: obj
  133. });
  134. },
  135. flushForTarget: function (targetServerId) {
  136. const buffer = this.buffer;
  137. for (let p in buffer) {
  138. const list = buffer[p];
  139. list.forEach(l => l.to.spliceWhere(f => f === targetServerId));
  140. list.spliceWhere(l => !l.to.length);
  141. if (!list.length)
  142. delete buffer[p];
  143. }
  144. },
  145. processDestroyedObject: function (obj) {
  146. const { objects, queue } = this;
  147. const { id, serverId } = obj;
  148. obj.destroyed = true;
  149. //We mark forceDestroy to tell objects that we're destroying an object outside of the
  150. // syncer's update method
  151. obj.forceDestroy = true;
  152. const msg = {
  153. id: id,
  154. destroyed: true
  155. };
  156. objects.removeObject(obj);
  157. this.flushForTarget(serverId);
  158. const fnQueueMsg = queue.bind(this, 'onGetObject');
  159. //Find any players that have seen this obj
  160. objects
  161. .filter(o => !o.destroyed && o?.player?.hasSeen(id))
  162. .forEach(o => {
  163. fnQueueMsg(msg, [o.serverId]);
  164. });
  165. },
  166. send: function () {
  167. if (!this.dirty)
  168. return;
  169. this.dirty = false;
  170. process.send({
  171. method: 'events',
  172. data: this.buffer
  173. });
  174. this.buffer = {};
  175. },
  176. sendServerModuleMessages: function () {
  177. if (!this.dirty)
  178. return;
  179. this.dirty = false;
  180. const serverModuleMsgs = this.buffer.serverModule;
  181. if (serverModuleMsgs) {
  182. process.send({
  183. method: 'events',
  184. data: {
  185. serverModule: serverModuleMsgs
  186. }
  187. });
  188. }
  189. this.buffer = {};
  190. }
  191. };