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.
 
 
 

368 lines
8.6 KiB

  1. define([
  2. 'objects/objects',
  3. 'world/physics',
  4. 'world/spawners',
  5. 'world/resourceSpawner',
  6. 'config/zoneBase',
  7. 'world/randomMap'
  8. ], function(
  9. objects,
  10. physics,
  11. spawners,
  12. resourceSpawner,
  13. globalZone,
  14. randomMap
  15. ) {
  16. var mapFile = null;
  17. var mapscale = 38;
  18. var padding = null;
  19. var map = {
  20. name: null,
  21. layers: [],
  22. mapFile: null,
  23. size: {
  24. w: 0,
  25. h: 0
  26. },
  27. instanced: false,
  28. custom: null,
  29. collisionMap: null,
  30. clientMap: null,
  31. oldLayers: {
  32. tiles: null,
  33. walls: null,
  34. doodads: null
  35. },
  36. objBlueprints: [],
  37. spawn: {
  38. x: 0,
  39. y: 0
  40. },
  41. rooms: [],
  42. hiddenRooms: [],
  43. hiddenWalls: null,
  44. hiddenTiles: null,
  45. zone: null,
  46. init: function(args) {
  47. this.name = args.name;
  48. try {
  49. this.zone = require('../config/maps/' + this.name + '/zone');
  50. } catch (e) {
  51. this.zone = globalZone;
  52. }
  53. var chats = null;
  54. try {
  55. chats = require('../config/maps/' + this.name + '/chats');
  56. } catch (e) {}
  57. if (chats)
  58. this.zone.chats = chats;
  59. var dialogues = null;
  60. try {
  61. dialogues = require('../config/maps/' + this.name + '/dialogues');
  62. } catch (e) {}
  63. if (dialogues)
  64. this.zone.dialogues = dialogues;
  65. this.zone = extend(true, {}, globalZone, this.zone);
  66. var resources = this.zone.resources || {};
  67. for (var r in resources) {
  68. resourceSpawner.register(r, resources[r]);
  69. }
  70. mapFile = require('../config/maps/' + this.name + '/map');
  71. this.mapFile = mapFile;
  72. this.mapFile.properties = this.mapFile.properties || {};
  73. mapScale = mapFile.tilesets[0].tileheight;
  74. this.instanced = mapFile.properties.instanced;
  75. this.custom = mapFile.properties.custom;
  76. if (this.instanced)
  77. this.instanced = (this.instanced == '1');
  78. if (mapFile.properties.spawn)
  79. this.spawn = JSON.parse(mapFile.properties.spawn);
  80. },
  81. create: function() {
  82. this.getMapFile();
  83. this.clientMap = {
  84. zoneId: -1,
  85. map: this.layers,
  86. hiddenWalls: this.hiddenWalls,
  87. hiddenTiles: this.hiddenTiles,
  88. collisionMap: this.collisionMap,
  89. clientObjects: this.objBlueprints,
  90. padding: padding,
  91. hiddenRooms: this.hiddenRooms
  92. };
  93. },
  94. getMapFile: function() {
  95. this.build();
  96. randomMap = extend(true, {}, randomMap);
  97. this.oldMap = this.layers;
  98. randomMap.templates = extend(true, [], this.rooms);
  99. randomMap.generateMappings(this);
  100. for (var i = 0; i < this.size.w; i++) {
  101. var row = this.layers[i];
  102. for (var j = 0; j < this.size.h; j++) {
  103. var cell = row[j];
  104. if (!cell)
  105. continue;
  106. cell = cell.split(',');
  107. var cLen = cell.length;
  108. var newCell = '';
  109. for (var k = 0; k < cLen; k++) {
  110. var c = cell[k];
  111. var newC = randomMap.randomizeTile(c);
  112. newCell += newC;
  113. //Wall?
  114. if ((c >= 160) && (c <= 352) && (newC == 0)) {
  115. this.collisionMap[i][j] = 0;
  116. }
  117. if (k < cLen - 1)
  118. newCell += ',';
  119. }
  120. if (this.hiddenWalls[i][j])
  121. this.hiddenWalls[i][j] = randomMap.randomizeTile(this.hiddenWalls[i][j]);
  122. if (this.hiddenTiles[i][j])
  123. this.hiddenTiles[i][j] = randomMap.randomizeTile(this.hiddenTiles[i][j]);
  124. row[j] = newCell;
  125. }
  126. }
  127. randomMap.templates
  128. .filter(r => r.properties.mapping)
  129. .forEach(function(m) {
  130. var x = m.x;
  131. var y = m.y;
  132. var w = m.width;
  133. var h = m.height;
  134. for (var i = x; i < x + w; i++) {
  135. var row = this.layers[i];
  136. for (var j = y; j < y + h; j++) {
  137. row[j] = '';
  138. }
  139. }
  140. }, this);
  141. physics.init(this.collisionMap);
  142. padding = mapFile.properties.padding;
  143. mapFile = null;
  144. console.log('(M ' + this.name + '): Ready');
  145. },
  146. build: function() {
  147. this.size.w = mapFile.width;
  148. this.size.h = mapFile.height;
  149. this.layers = _.get2dArray(this.size.w, this.size.h, null);
  150. this.hiddenWalls = _.get2dArray(this.size.w, this.size.h, null);
  151. this.hiddenTiles = _.get2dArray(this.size.w, this.size.h, null);
  152. this.oldLayers.tiles = _.get2dArray(this.size.w, this.size.h, 0);
  153. this.oldLayers.walls = _.get2dArray(this.size.w, this.size.h, 0);
  154. this.oldLayers.objects = _.get2dArray(this.size.w, this.size.h, 0);
  155. var builders = {
  156. tile: this.builders.tile.bind(this),
  157. object: this.builders.object.bind(this)
  158. };
  159. this.collisionMap = _.get2dArray(this.size.w, this.size.h);
  160. //Rooms need to be ahead of exits
  161. mapFile.layers.rooms = (mapFile.layers.rooms || [])
  162. .sort(function(a, b) {
  163. if ((a.exit) && (!b.exit))
  164. return 1;
  165. else
  166. return 0;
  167. });
  168. for (var i = 0; i < mapFile.layers.length; i++) {
  169. var layer = mapFile.layers[i];
  170. var layerName = layer.name;
  171. if (!layer.visible)
  172. continue;
  173. var data = layer.data || layer.objects;
  174. var len = data.length;
  175. for (var j = 0; j < len; j++) {
  176. var cell = data[j];
  177. if ((cell.gid) || (cell.id))
  178. builders.object(layerName, cell, j);
  179. else
  180. builders.tile(layerName, cell, j);
  181. }
  182. }
  183. },
  184. builders: {
  185. getCellInfo: function(cell) {
  186. var flipX = null;
  187. if ((cell ^ 0x80000000) > 0) {
  188. flipX = true;
  189. cell = cell ^ 0x80000000;
  190. }
  191. var firstGid = 0;
  192. var sheetName = null;
  193. for (var s = 0; s < mapFile.tilesets.length; s++) {
  194. var tileset = mapFile.tilesets[s];
  195. if (tileset.firstgid <= cell) {
  196. sheetName = tileset.name;
  197. firstGid = tileset.firstgid;
  198. }
  199. }
  200. cell = cell - firstGid + 1;
  201. return {
  202. sheetName: sheetName,
  203. cell: cell,
  204. flipX: flipX
  205. };
  206. },
  207. tile: function(layerName, cell, i) {
  208. var y = ~~(i / this.size.w);
  209. var x = i - (y * this.size.w);
  210. if (cell == 0) {
  211. if (layerName == 'tiles')
  212. this.collisionMap[x][y] = 1;
  213. return;
  214. }
  215. var cellInfo = this.builders.getCellInfo(cell);
  216. var sheetName = cellInfo.sheetName;
  217. var cell = cellInfo.cell;
  218. if (sheetName == 'walls')
  219. cell += 192;
  220. else if (sheetName == 'objects')
  221. cell += 384;
  222. if ((layerName != 'hiddenWalls') && (layerName != 'hiddenTiles')) {
  223. var layer = this.layers;
  224. if (this.oldLayers[layerName])
  225. this.oldLayers[layerName][x][y] = cell;
  226. layer[x][y] = (layer[x][y] == null) ? cell : layer[x][y] + ',' + cell;
  227. }
  228. else if (layerName == 'hiddenWalls')
  229. this.hiddenWalls[x][y] = cell;
  230. else if (layerName == 'hiddenTiles')
  231. this.hiddenTiles[x][y] = cell;
  232. if (layerName.indexOf('walls') > -1)
  233. this.collisionMap[x][y] = 1;
  234. else if (sheetName.toLowerCase().indexOf('tiles') > -1) {
  235. if ((cell == 6) || (cell == 7) || (cell == 54) || (cell == 55) || (cell == 62) || (cell == 63) || (cell == 154))
  236. this.collisionMap[x][y] = 1;
  237. }
  238. },
  239. object: function(layerName, cell, i) {
  240. var clientObj = (layerName == 'clientObjects');
  241. var cellInfo = this.builders.getCellInfo(cell.gid);
  242. var name = (cell.name || '');
  243. var objZoneName = name;
  244. if (name.indexOf('|') > -1) {
  245. var split = name.split('|');
  246. name = split[0];
  247. objZoneName = split[1];
  248. }
  249. var blueprint = {
  250. clientObj: clientObj,
  251. sheetName: cellInfo.sheetName,
  252. cell: cellInfo.cell - 1,
  253. x: cell.x / mapScale,
  254. y: (cell.y / mapScale) - 1,
  255. name: name,
  256. properties: cell.properties || {}
  257. };
  258. if ((this.zone) && (this.zone.objects) && (this.zone.objects[objZoneName.toLowerCase()]))
  259. extend(true, blueprint, this.zone.objects[objZoneName.toLowerCase()]);
  260. if ((blueprint.properties.cpnNotice) || (layerName == 'rooms') || (layerName == 'hiddenRooms')) {
  261. blueprint.y++;
  262. blueprint.width = cell.width / mapScale;
  263. blueprint.height = cell.height / mapScale;
  264. } else if (cell.width == 24)
  265. blueprint.x++;
  266. if (layerName == 'rooms') {
  267. if (blueprint.properties.exit) {
  268. var room = this.rooms.find(function(r) {
  269. return (!(
  270. (blueprint.x + blueprint.width < r.x) ||
  271. (blueprint.y + blueprint.height < r.y) ||
  272. (blueprint.x >= r.x + r.width) ||
  273. (blueprint.y >= r.y + r.height)
  274. ));
  275. });
  276. room.exits.push(blueprint);
  277. } else {
  278. blueprint.exits = [];
  279. blueprint.objects = [];
  280. this.rooms.push(blueprint);
  281. }
  282. } else if (layerName == 'hiddenRooms') {
  283. this.hiddenRooms.push(blueprint);
  284. } else if (!clientObj) {
  285. if (!mapFile.properties.isRandom)
  286. spawners.register(blueprint, mapFile.properties.spawnCd);
  287. else {
  288. var room = this.rooms.find(function(r) {
  289. return (!(
  290. (blueprint.x < r.x) ||
  291. (blueprint.y < r.y) ||
  292. (blueprint.x >= r.x + r.width) ||
  293. (blueprint.y >= r.y + r.height)
  294. ));
  295. });
  296. room.objects.push(blueprint);
  297. }
  298. } else {
  299. var obj = objects.buildObjects([blueprint], true).getSimple(true);
  300. this.objBlueprints.push(obj);
  301. }
  302. }
  303. }
  304. }
  305. return map;
  306. });