Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

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