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.
 
 
 

518 lines
12 KiB

  1. define([
  2. 'world/map',
  3. 'world/syncer',
  4. 'objects/objects',
  5. 'world/spawners',
  6. 'world/physics',
  7. 'world/resourceSpawner',
  8. 'config/spells/spellCallbacks',
  9. 'config/quests/questBuilder',
  10. 'world/randomMap',
  11. 'world/customMap'
  12. ], function(
  13. map,
  14. syncer,
  15. objects,
  16. spawners,
  17. physics,
  18. resourceSpawner,
  19. spellCallbacks,
  20. questBuilder,
  21. randomMap,
  22. customMap
  23. ) {
  24. return {
  25. instances: [],
  26. zoneId: -1,
  27. speed: 350,
  28. lastTime: 0,
  29. init: function(args) {
  30. this.zoneId = args.zoneId;
  31. spellCallbacks.init();
  32. map.init(args);
  33. if (!map.instanced) {
  34. spawners.init({
  35. objects: objects,
  36. syncer: syncer,
  37. zone: map.zone
  38. });
  39. map.create();
  40. map.clientMap.zoneId = this.zoneId;
  41. resourceSpawner.init({
  42. objects: objects,
  43. syncer: syncer,
  44. zone: map.zone,
  45. physics: physics,
  46. map: map
  47. });
  48. syncer.init({
  49. objects: objects
  50. });
  51. objects.init({
  52. objects: objects,
  53. syncer: syncer,
  54. physics: physics,
  55. zoneId: this.zoneId,
  56. spawners: spawners,
  57. questBuilder: questBuilder
  58. });
  59. questBuilder.init({
  60. spawners: spawners
  61. });
  62. this.addObject = this.nonInstanced.addObject.bind(this);
  63. this.onAddObject = this.nonInstanced.onAddObject.bind(this);
  64. this.updateObject = this.nonInstanced.updateObject.bind(this);
  65. this.queueAction = this.nonInstanced.queueAction.bind(this);
  66. this.performAction = this.nonInstanced.performAction.bind(this);
  67. this.removeObject = this.nonInstanced.removeObject.bind(this);
  68. this.onRemoveObject = this.nonInstanced.onRemoveObject.bind(this);
  69. this.tick = this.nonInstanced.tick.bind(this);
  70. this.tick();
  71. } else {
  72. spawners.init({
  73. zone: map.zone
  74. });
  75. map.create();
  76. map.clientMap.zoneId = this.zoneId;
  77. this.addObject = this.instanced.addObject.bind(this);
  78. this.onAddObject = this.instanced.onAddObject.bind(this);
  79. this.updateObject = this.instanced.updateObject.bind(this);
  80. this.queueAction = this.instanced.queueAction.bind(this);
  81. this.performAction = this.instanced.performAction.bind(this);
  82. this.removeObject = this.instanced.removeObject.bind(this);
  83. this.onRemoveObject = this.instanced.onRemoveObject.bind(this);
  84. if (map.mapFile.properties.isRandom)
  85. this.ttlGen = 0;
  86. this.tick = this.instanced.tick.bind(this);
  87. this.tick();
  88. }
  89. },
  90. nonInstanced: {
  91. tick: function() {
  92. objects.update();
  93. spawners.update();
  94. resourceSpawner.update();
  95. syncer.update();
  96. setTimeout(this.tick.bind(this), this.speed);
  97. },
  98. addObject: function(msg) {
  99. var obj = msg.obj;
  100. obj.serverId = obj.id;
  101. delete obj.id;
  102. if ((msg.keepPos) && (!physics.isValid(obj.x, obj.y))) {
  103. msg.keepPos = false;
  104. }
  105. if ((!msg.keepPos) || (obj.x == null)) {
  106. obj.x = map.spawn.x;
  107. obj.y = map.spawn.y;
  108. }
  109. obj.spawn = {
  110. x: map.spawn.x,
  111. y: map.spawn.y
  112. };
  113. syncer.queue('onGetMap', map.clientMap, [obj.serverId]);
  114. if (!msg.transfer)
  115. objects.addObject(obj, this.onAddObject.bind(this));
  116. else {
  117. var o = objects.transferObject(obj);
  118. questBuilder.obtain(o);
  119. }
  120. },
  121. onAddObject: function(obj) {
  122. questBuilder.obtain(obj);
  123. },
  124. updateObject: function(msg) {
  125. var obj = objects.find(o => o.serverId == msg.id);
  126. if (!obj)
  127. return;
  128. var msgObj = msg.obj;
  129. var components = msgObj.components || [];
  130. delete msgObj.components;
  131. for (var p in msgObj) {
  132. obj[p] = msgObj[p];
  133. }
  134. var cLen = components.length;
  135. for (var i = 0; i < cLen; i++) {
  136. var c = components[i];
  137. var component = obj[c.type];
  138. for (var p in c) {
  139. component[p] = c[p];
  140. }
  141. }
  142. },
  143. queueAction: function(msg) {
  144. var obj = objects.find(o => o.serverId == msg.id);
  145. if (!obj)
  146. return;
  147. obj.queue(msg.action);
  148. },
  149. performAction: function(msg) {
  150. var obj = null;
  151. var targetId = msg.action.targetId;
  152. if (!targetId)
  153. obj = objects.find(o => o.serverId == msg.id);
  154. else {
  155. obj = objects.find(o => o.id == targetId);
  156. if (obj) {
  157. var action = msg.action;
  158. if (!action.data)
  159. action.data = {};
  160. action.data.sourceId = msg.id;
  161. }
  162. }
  163. if (!obj)
  164. return;
  165. obj.performAction(msg.action);
  166. },
  167. removeObject: function(msg) {
  168. var obj = msg.obj;
  169. obj = objects.find(o => o.serverId == obj.id);
  170. if (!obj) {
  171. //We should probably never reach this
  172. return;
  173. }
  174. if (obj.auth)
  175. obj.auth.doSave();
  176. if (obj.player)
  177. obj.fireEvent('beforeRezone');
  178. obj.destroyed = true;
  179. },
  180. onRemoveObject: function(obj) {
  181. }
  182. },
  183. instanced: {
  184. tick: function() {
  185. if (map.mapFile.properties.isRandom) {
  186. if (this.ttlGen <= 0) {
  187. if (!map.oldMap)
  188. map.oldMap = map.clientMap.map;
  189. if (!map.oldCollisionMap)
  190. map.oldCollisionMap = map.collisionMap;
  191. spawners.reset();
  192. randomMap.generate({
  193. map: map,
  194. physics: physics,
  195. spawners: spawners
  196. });
  197. this.ttlGen = 2000;
  198. } else
  199. this.ttlGen--;
  200. }
  201. var instances = this.instances;
  202. var iLen = instances.length;
  203. for (var i = 0; i < iLen; i++) {
  204. var instance = instances[i];
  205. instance.objects.update();
  206. instance.spawners.update();
  207. instance.resourceSpawner.update();
  208. instance.syncer.update();
  209. if (instance.closeTtl != null) {
  210. var hasPlayers = instance.objects.objects.some(o => o.player);
  211. if (hasPlayers) {
  212. delete instance.closeTtl;
  213. continue;
  214. }
  215. instance.closeTtl--;
  216. if (instance.closeTtl <= 0) {
  217. instances.splice(i, 1);
  218. i--;
  219. iLen--;
  220. }
  221. } else {
  222. var isEmpty = !instance.objects.objects.some(o => o.player);
  223. if (isEmpty) {
  224. //Zones reset after being empty for 10 minutes
  225. instance.closeTtl = 2;
  226. }
  227. }
  228. }
  229. setTimeout(this.tick.bind(this), this.speed);
  230. },
  231. addObject: function(msg) {
  232. var obj = msg.obj;
  233. var instanceId = msg.instanceId;
  234. //Maybe a party member is in here already?
  235. var social = obj.components.find(c => c.type == 'social');
  236. if ((social) && (social.party)) {
  237. var party = social.party;
  238. var instances = this.instances;
  239. var iLen = instances.length;
  240. for (var i = 0; i < iLen; i++) {
  241. var instance = instances[i];
  242. var partyInside = instance.objects.objects.some(o => party.indexOf(o.serverId) > -1);
  243. if (partyInside) {
  244. if (instance.id != obj.instanceId)
  245. msg.keepPos = false;
  246. obj.instanceId = instance.id;
  247. obj.instance = instance;
  248. instanceId = instance.id;
  249. break;
  250. }
  251. }
  252. }
  253. if (msg.transfer)
  254. msg.keepPos = false;
  255. var exists = this.instances.find(i => i.id == instanceId);
  256. if (exists) {
  257. if ((msg.keepPos) && (!exists.physics.isValid(obj.x, obj.y)))
  258. msg.keepPos = false;
  259. }
  260. if ((!msg.keepPos) || (obj.x == null)) {
  261. obj.x = map.spawn.x;
  262. obj.y = map.spawn.y;
  263. }
  264. obj.spawn = {
  265. x: map.spawn.x,
  266. y: map.spawn.y
  267. };
  268. if (exists) {
  269. //Keep track of what the connection id is (sent from the server)
  270. obj.serverId = obj.id;
  271. delete obj.id;
  272. obj.spawn = {
  273. x: exists.map.spawn.x,
  274. y: exists.map.spawn.y
  275. };
  276. exists.syncer.queue('onGetMap', exists.map.clientMap, [obj.serverId]);
  277. if (!msg.transfer) {
  278. exists.objects.addObject(obj, this.onAddObject.bind(this, msg.keepPos));
  279. } else {
  280. var newObj = exists.objects.transferObject(obj);
  281. this.onAddObject(false, newObj);
  282. }
  283. process.send({
  284. method: 'object',
  285. serverId: obj.serverId,
  286. obj: {
  287. instanceId: exists.id
  288. }
  289. });
  290. } else
  291. obj = this.instanced.createInstance.call(this, obj, msg.transfer);
  292. },
  293. onAddObject: function(keepPos, obj) {
  294. if (!keepPos) {
  295. obj.x = obj.instance.map.spawn.x;
  296. obj.y = obj.instance.map.spawn.y;
  297. }
  298. obj.instance.spawners.scale(obj.stats.values.level);
  299. obj.instance.questBuilder.obtain(obj);
  300. },
  301. updateObject: function(msg) {
  302. var id = msg.id;
  303. var instanceId = msg.instanceId;
  304. var exists = this.instances.find(i => i.id == instanceId);
  305. if (!exists)
  306. return;
  307. var obj = exists.objects.find(o => o.serverId == id);
  308. if (!obj) {
  309. console.log('OBJECT NOT FOUND!!!!!!!!!!!!!!!!!!!!!!!!!!!');
  310. console.log(msg);
  311. }
  312. var msgObj = msg.obj;
  313. var components = msgObj.components || [];
  314. delete msgObj.components;
  315. for (var p in msgObj) {
  316. obj[p] = msgObj[p];
  317. }
  318. var cLen = components.length;
  319. for (var i = 0; i < cLen; i++) {
  320. var c = components[i];
  321. var component = obj[c.type];
  322. for (var p in c) {
  323. component[p] = c[p];
  324. }
  325. }
  326. },
  327. performAction: function(msg) {
  328. var id = msg.id;
  329. var instanceId = msg.instanceId;
  330. var exists = this.instances.find(i => i.id == instanceId);
  331. if (!exists)
  332. return;
  333. var obj = exists.objects.find(o => o.serverId == id);
  334. if (!obj)
  335. return;
  336. obj.performAction(msg.action);
  337. },
  338. queueAction: function(msg) {
  339. var id = msg.id;
  340. var instanceId = msg.instanceId;
  341. var exists = this.instances.find(i => i.id == instanceId);
  342. if (!exists)
  343. return;
  344. var obj = exists.objects.find(o => o.serverId == id);
  345. if (obj)
  346. obj.queue(msg.action);
  347. },
  348. removeObject: function(msg) {
  349. var obj = msg.obj;
  350. var instanceId = msg.instanceId;
  351. var exists = this.instances.find(i => i.id == instanceId);
  352. if (!exists)
  353. return;
  354. var obj = msg.obj;
  355. obj = exists.objects.find(o => o.serverId == obj.id);
  356. if (!obj)
  357. return;
  358. if (obj.auth)
  359. obj.auth.doSave();
  360. obj.destroyed = true;
  361. },
  362. onRemoveObject: function(obj) {
  363. },
  364. createInstance: function(objToAdd, transfer) {
  365. var instance = {
  366. id: objToAdd.name + '_' + (+new Date),
  367. objects: extend(true, {}, objects),
  368. spawners: extend(true, {}, spawners),
  369. syncer: extend(true, {}, syncer),
  370. physics: extend(true, {}, physics),
  371. resourceSpawner: extend(true, {}, resourceSpawner),
  372. zoneId: this.zoneId,
  373. zone: map.zone,
  374. closeTtl: null,
  375. questBuilder: extend(true, {}, questBuilder),
  376. map: {
  377. spawn: extend(true, {}, map.spawn),
  378. clientMap: extend(true, {}, map.clientMap)
  379. }
  380. };
  381. instance.objects.init(instance);
  382. instance.spawners.init(instance);
  383. instance.syncer.init(instance);
  384. instance.resourceSpawner.init(instance);
  385. instance.questBuilder.init(instance);
  386. this.instances.push(instance);
  387. var onDone = this.instanced.onCreateInstance.bind(this, instance, objToAdd, transfer);
  388. if (map.custom) {
  389. instance.customMap = extend(true, {}, customMap);
  390. instance.customMap.load(instance, objToAdd, onDone);
  391. }
  392. else
  393. onDone();
  394. },
  395. onCreateInstance: function(instance, objToAdd, transfer) {
  396. objToAdd.instance = instance;
  397. objToAdd.instanceId = instance.id;
  398. //Keep track of what the connection id is (sent from the server)
  399. objToAdd.serverId = objToAdd.id;
  400. delete objToAdd.id;
  401. var obj = null;
  402. instance.syncer.queue('onGetMap', instance.map.clientMap, [objToAdd.serverId]);
  403. if (!transfer)
  404. obj = instance.objects.addObject(objToAdd, this.onAddObject.bind(this, false));
  405. else {
  406. obj = instance.objects.transferObject(objToAdd);
  407. obj.x = instance.map.spawn.x;
  408. obj.y = instance.map.spawn.y;
  409. instance.questBuilder.obtain(obj);
  410. obj.instance.spawners.scale(obj.stats.values.level);
  411. }
  412. process.send({
  413. method: 'object',
  414. serverId: obj.serverId,
  415. obj: {
  416. instanceId: instance.id
  417. }
  418. });
  419. return obj;
  420. }
  421. },
  422. };
  423. });