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.
 
 
 

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