1
1

RoomRandom.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /// <reference path="../Room.ts" />
  2. interface TrickierOptions {
  3. region : RegionRandom;
  4. map : RoomRandomMap;
  5. otherRoom : RoomRandom;
  6. }
  7. interface TrickyOptions extends TrickierOptions {
  8. otherRoomDirection : number;
  9. trickyRoomDirection : number;
  10. x : number;
  11. y : number;
  12. }
  13. interface RoomRandomNode {
  14. room : RoomRandom;
  15. coordinates : Array<number>;
  16. distance : number;
  17. }
  18. class RoomRandom extends Room {
  19. public connectableOn : Array<number> = [Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST];
  20. public randomizable = true; // non-randomizable rooms don't get placed automatically
  21. public placed = false;
  22. public appearChance = 75;
  23. public extraConnectionChance = 75;
  24. public backgroundImage = "tomato";
  25. public lastMap : RoomRandomMap;
  26. public constructor (id? : string, fodder? : boolean, forbidden? : boolean) {
  27. super(id, fodder);
  28. this.randomizable = forbidden != true;
  29. }
  30. public getBackgroundClass () {
  31. if (this.isImageDefined()) {
  32. return this.backgroundImage;
  33. }
  34. return "tomato";
  35. }
  36. private isImageDefined () {
  37. try {
  38. for (var i = 0; i < document.styleSheets.length; i++) {
  39. var rules = document.styleSheets[i]['rules'] || document.styleSheets[i]['cssRules'];
  40. for (var x in rules) {
  41. if (typeof rules[x].selectorText == 'string' && rules[x].selectorText == "." + this.backgroundImage) {
  42. return true;
  43. }
  44. }
  45. }
  46. return false;
  47. } catch (e) {
  48. console.warn("Unable to read image");
  49. return true;
  50. }
  51. }
  52. public isConnectableOn (oppositeDirection : number) {
  53. return this.connectableOn.indexOf(oppositeDirection) != -1;
  54. }
  55. public trickyCode : (options : TrickyOptions) => boolean;
  56. public getAnyDirection (options : TrickierOptions) : TrickyOptions {
  57. let directionShuffler = new Shuffler<number>(this.connectableOn);
  58. for (let direction = directionShuffler.getOne(); direction != undefined; direction = directionShuffler.getOne()) {
  59. let oppositeDirection = OppositeDirection[Direction[direction]];
  60. let otherCoordinates = options.map.getCoordinates(options.otherRoom);
  61. let wouldbeCoordinates = Room.shift(otherCoordinates, oppositeDirection);
  62. let trickyOptions = {
  63. otherRoom : options.otherRoom,
  64. otherRoomDirection : oppositeDirection,
  65. trickyRoomDirection : direction,
  66. map : options.map,
  67. region : options.region,
  68. x : wouldbeCoordinates[0],
  69. y : wouldbeCoordinates[1]
  70. };
  71. if (this.isPlaceable(trickyOptions)) {
  72. return trickyOptions;
  73. }
  74. }
  75. }
  76. public isPlaceable (options : TrickyOptions) {
  77. if (!this.isConnectableOn(options.trickyRoomDirection) || !options.map.isFree(options.x, options.y)) {
  78. // This can't connect through that!
  79. // That coordinate isn't free!
  80. return false;
  81. }
  82. if (options.otherRoom == undefined || !options.otherRoom.isConnectableOn(options.otherRoomDirection)) {
  83. // There is no other room there?
  84. // The other room doesn't like it this way
  85. return false;
  86. }
  87. // Do I have my own tricky code?
  88. if (this.trickyCode != undefined) {
  89. return this.trickyCode(options);
  90. }
  91. return true;
  92. }
  93. public getDistanceTo (room : RoomRandom) {
  94. let otherCoordinates = this.lastMap.getCoordinates(room);
  95. return this.getDistanceToCoordinates(otherCoordinates);
  96. }
  97. public getDistanceToCoordinates (otherCoordinates) {
  98. let myCoordinates = this.lastMap.getCoordinates(this);
  99. if (myCoordinates != undefined && otherCoordinates != undefined) {
  100. let c1 = myCoordinates;
  101. let c2 = otherCoordinates;
  102. return Math.abs(c1[0] - c2[0]) + Math.abs(c1[1] - c2[1]);
  103. }
  104. }
  105. /**
  106. * This implementation is sufficiently fast for constant use.
  107. * @param pathEnd
  108. * @param map
  109. * @param availableRooms
  110. * @returns {Array}
  111. */
  112. public findPathTo (pathEnd : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  113. validRoom = validRoom == undefined ? () => {return true;} : validRoom;
  114. let map = this.lastMap;
  115. let endPosition = map.getCoordinates(pathEnd);
  116. let open = [];
  117. let distance = (c1, c2) => {
  118. return Math.abs(c1[0] - c2[0]) + Math.abs(c1[1] - c2[1]);
  119. };
  120. let neighbors = (room : RoomRandom, x : number, y : number) => {
  121. let neighs = [];
  122. for (let direction = 0; direction < room.connections.length; direction++) {
  123. let otherRoom = room.connections[direction];
  124. if (otherRoom != undefined && open.indexOf(otherRoom) == -1 && validRoom(<RoomRandom> otherRoom)) {
  125. let dirCoordinates = Room.shift([x, y], direction);
  126. let dir = [otherRoom, dirCoordinates, distance(endPosition, dirCoordinates)];
  127. neighs.push(dir);
  128. }
  129. }
  130. return neighs.sort((a,b) => { return (<number> a[2]) - (<number> b[2]);});
  131. };
  132. let shortestPath = {
  133. length : map.getRoomCount()
  134. };
  135. let noPath = shortestPath;
  136. let cPath = [];
  137. let findPath = (myArray) => {
  138. let room = myArray[0];
  139. cPath.push(myArray);
  140. if (room == pathEnd) {
  141. if (shortestPath.length >= cPath.length) {
  142. shortestPath = cPath.slice(0);
  143. }
  144. } else if (shortestPath.length > (cPath.length)) {
  145. open.push(room);
  146. let otherRooms = neighbors(room, myArray[1][0], myArray[1][1]);
  147. for (let i = 0; i < otherRooms.length; i++) {
  148. if ((cPath.length + 1) < shortestPath.length) {
  149. findPath(otherRooms[i]);
  150. }
  151. }
  152. open.pop();
  153. }
  154. cPath.pop();
  155. };
  156. findPath([this, map.getCoordinates(this)]);
  157. return shortestPath != noPath ? shortestPath : undefined;
  158. }
  159. public getBestDirectionTo (otherRoom : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  160. let path = this.findPathTo(otherRoom, validRoom);
  161. if (path != undefined) {
  162. if (path.length == 1) {
  163. return undefined;
  164. }
  165. return this.connections.indexOf(path[1][0]);
  166. }
  167. }
  168. public getAStarPathTo (otherRoom : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  169. validRoom = validRoom != undefined ? validRoom : () => {return true};
  170. let distance = (c1, c2) => {
  171. return Math.abs(c1[0] - c2[0]) + Math.abs(c1[1] - c2[1]);
  172. };
  173. let isVisited = (room) => {
  174. return visited.indexOf(room) != -1;
  175. };
  176. let getNeighbors = (node : RoomRandomNode) => {
  177. let neighbors : Array<RoomRandomNode> = [];
  178. for (let direction = 0; direction < node.room.connections.length; direction++) {
  179. if (node.room.connections[direction] != undefined && !isVisited(node.room.connections[direction]) && validRoom(<RoomRandom> node.room.connections[direction])) {
  180. let coordinates = Room.shift(node.coordinates, direction);
  181. neighbors.push({
  182. room : <RoomRandom> node.room.connections[direction],
  183. coordinates : coordinates,
  184. distance : distance(coordinates, endNode.coordinates)
  185. });
  186. visited.push(<RoomRandom> node.room.connections[direction]);
  187. }
  188. }
  189. return neighbors;
  190. };
  191. let getClosestPath = () => {
  192. let shortest = 0;
  193. for (let i = 1; i < open.length; i++) {
  194. let lastPoint = open[i][open[i].length - 1];
  195. if (lastPoint.distance < open[shortest][open[shortest].length - 1].distance) {
  196. shortest = i;
  197. }
  198. }
  199. return shortest;
  200. };
  201. let endNode = {room : otherRoom, coordinates : this.lastMap.getCoordinates(otherRoom), distance : 0};
  202. let startCoordinates = this.lastMap.getCoordinates(this);
  203. let startNode = {room : this, coordinates : startCoordinates, distance : distance(startCoordinates, endNode.coordinates)};
  204. let open : Array<Array<RoomRandomNode>> = [[startNode]];
  205. let closed : Array<Array<RoomRandomNode>> = [];
  206. let shortestPath = this.lastMap.getRoomCount();
  207. let shortestIndex;
  208. let myPath;
  209. let closest = 0;
  210. let visited : Array<RoomRandom> = [this];
  211. while (open.length > 0) {
  212. myPath = open.splice(closest, 1)[0];
  213. if (myPath[myPath.length - 1].distance == 0) {
  214. let push = closed.push(myPath);
  215. if (myPath.length < shortestPath) {
  216. shortestPath = myPath.length;
  217. shortestIndex = push - 1;
  218. }
  219. // lazy, first path is very likely to be the best
  220. break;
  221. } else {
  222. let neighbors = getNeighbors(myPath[myPath.length - 1]);
  223. for (let i = 0; i < neighbors.length; i++) {
  224. open.push(myPath.concat([neighbors[i]]));
  225. }
  226. }
  227. for (let i = open.length - 1; i >= 0; i--) {
  228. if (open[i].length >= shortestPath) {
  229. open.splice(i, 1);
  230. }
  231. }
  232. closest = getClosestPath();
  233. }
  234. return closed[shortestIndex];
  235. }
  236. public getAStarBestDirectionTo (otherRoom : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  237. let path = this.getAStarPathTo(otherRoom, validRoom);
  238. if (path != undefined) {
  239. if (path.length == 1) {
  240. return undefined;
  241. }
  242. return this.connections.indexOf(path[1].room);
  243. }
  244. }
  245. public getConnectedDirection () {
  246. let shuffler = new Shuffler(Room.DIRECTIONS);
  247. for (let direction = shuffler.getOne(); direction != undefined; direction = shuffler.getOne()) {
  248. if (this.connections[direction] != undefined) {
  249. return direction;
  250. }
  251. }
  252. }
  253. // TODO: Return all things of type that are in placed RoomRandom.
  254. public static getActive (type : typeof Thing) {
  255. }
  256. }
  257. // random = new RoomRandom...
  258. // random.connectableOn = [Room.DIRECTION_NORTH, Room.DIRECTION_SOUTH...]