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.
 
 
 

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