1
1

RoomRandom.ts 11 KB

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