No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

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