RoomRandom.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, 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 myCoordinates = this.lastMap.getCoordinates(this);
  95. let otherCoordinates = this.lastMap.getCoordinates(room);
  96. if (myCoordinates != undefined && otherCoordinates != undefined) {
  97. let c1 = myCoordinates;
  98. let c2 = otherCoordinates;
  99. return Math.abs(c1[0] - c2[0]) + Math.abs(c1[1] - c2[1]);
  100. }
  101. }
  102. /**
  103. * This implementation is sufficiently fast for constant use.
  104. * @param pathEnd
  105. * @param map
  106. * @param availableRooms
  107. * @returns {Array}
  108. */
  109. public findPathTo (pathEnd : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  110. validRoom = validRoom == undefined ? () => {return true;} : validRoom;
  111. let map = this.lastMap;
  112. let endPosition = map.getCoordinates(pathEnd);
  113. let open = [];
  114. let distance = (c1, c2) => {
  115. return Math.abs(c1[0] - c2[0]) + Math.abs(c1[1] - c2[1]);
  116. };
  117. let neighbors = (room : RoomRandom, x : number, y : number) => {
  118. let neighs = [];
  119. for (let direction = 0; direction < room.connections.length; direction++) {
  120. let otherRoom = room.connections[direction];
  121. if (otherRoom != undefined && open.indexOf(otherRoom) == -1 && validRoom(<RoomRandom> otherRoom)) {
  122. let dirCoordinates = Room.shift([x, y], direction);
  123. let dir = [otherRoom, dirCoordinates, distance(endPosition, dirCoordinates)];
  124. neighs.push(dir);
  125. }
  126. }
  127. return neighs.sort((a,b) => { return (<number> a[2]) - (<number> b[2]);});
  128. };
  129. let shortestPath = {
  130. length : map.getRoomCount()
  131. };
  132. let noPath = shortestPath;
  133. let cPath = [];
  134. let findPath = (myArray) => {
  135. let room = myArray[0];
  136. cPath.push(myArray);
  137. if (room == pathEnd) {
  138. if (shortestPath.length >= cPath.length) {
  139. shortestPath = cPath.slice(0);
  140. }
  141. } else if (shortestPath.length > (cPath.length)) {
  142. open.push(room);
  143. let otherRooms = neighbors(room, myArray[1][0], myArray[1][1]);
  144. for (let i = 0; i < otherRooms.length; i++) {
  145. if ((cPath.length + 1) < shortestPath.length) {
  146. findPath(otherRooms[i]);
  147. }
  148. }
  149. open.pop();
  150. }
  151. cPath.pop();
  152. };
  153. findPath([this, map.getCoordinates(this)]);
  154. return shortestPath != noPath ? shortestPath : undefined;
  155. }
  156. public getBestDirectionTo (otherRoom : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  157. let path = this.findPathTo(otherRoom, validRoom);
  158. if (path != undefined) {
  159. if (path.length == 1) {
  160. return undefined;
  161. }
  162. return this.connections.indexOf(path[1][0]);
  163. }
  164. }
  165. public getAStarPathTo (otherRoom : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  166. validRoom = validRoom != undefined ? validRoom : () => {return true};
  167. let distance = (c1, c2) => {
  168. return Math.abs(c1[0] - c2[0]) + Math.abs(c1[1] - c2[1]);
  169. };
  170. let isVisited = (room) => {
  171. return visited.indexOf(room) != -1;
  172. };
  173. let getNeighbors = (node : RoomRandomNode) => {
  174. let neighbors : Array<RoomRandomNode> = [];
  175. for (let direction = 0; direction < node.room.connections.length; direction++) {
  176. if (node.room.connections[direction] != undefined && !isVisited(node.room.connections[direction]) && validRoom(<RoomRandom> node.room.connections[direction])) {
  177. let coordinates = Room.shift(node.coordinates, direction);
  178. neighbors.push({
  179. room : <RoomRandom> node.room.connections[direction],
  180. coordinates : coordinates,
  181. distance : distance(coordinates, endNode.coordinates)
  182. });
  183. visited.push(<RoomRandom> node.room.connections[direction]);
  184. }
  185. }
  186. return neighbors;
  187. };
  188. let getClosestPath = () => {
  189. let shortest = 0;
  190. for (let i = 1; i < open.length; i++) {
  191. let lastPoint = open[i][open[i].length - 1];
  192. if (lastPoint.distance < open[shortest][open[shortest].length - 1].distance) {
  193. shortest = i;
  194. }
  195. }
  196. return shortest;
  197. };
  198. let endNode = {room : otherRoom, coordinates : this.lastMap.getCoordinates(otherRoom), distance : 0};
  199. let startCoordinates = this.lastMap.getCoordinates(this);
  200. let startNode = {room : this, coordinates : startCoordinates, distance : distance(startCoordinates, endNode.coordinates)};
  201. let open : Array<Array<RoomRandomNode>> = [[startNode]];
  202. let closed : Array<Array<RoomRandomNode>> = [];
  203. let shortestPath = this.lastMap.getRoomCount();
  204. let shortestIndex;
  205. let myPath;
  206. let closest = 0;
  207. let visited : Array<RoomRandom> = [this];
  208. while (open.length > 0) {
  209. myPath = open.splice(closest, 1)[0];
  210. if (myPath[myPath.length - 1].distance == 0) {
  211. let push = closed.push(myPath);
  212. if (myPath.length < shortestPath) {
  213. shortestPath = myPath.length;
  214. shortestIndex = push - 1;
  215. }
  216. // lazy, first path is very likely to be the best
  217. break;
  218. } else {
  219. let neighbors = getNeighbors(myPath[myPath.length - 1]);
  220. for (let i = 0; i < neighbors.length; i++) {
  221. open.push(myPath.concat([neighbors[i]]));
  222. }
  223. }
  224. for (let i = open.length - 1; i >= 0; i--) {
  225. if (open[i].length >= shortestPath) {
  226. open.splice(i, 1);
  227. }
  228. }
  229. closest = getClosestPath();
  230. }
  231. return closed[shortestIndex];
  232. }
  233. public getAStarBestDirectionTo (otherRoom : RoomRandom, validRoom? : (room : RoomRandom) => boolean) {
  234. let path = this.getAStarPathTo(otherRoom, validRoom);
  235. if (path != undefined) {
  236. if (path.length == 1) {
  237. return undefined;
  238. }
  239. return this.connections.indexOf(path[1].room);
  240. }
  241. }
  242. public getConnectedDirection () {
  243. let shuffler = new Shuffler(Room.DIRECTIONS);
  244. for (let direction = shuffler.getOne(); direction != undefined; direction = shuffler.getOne()) {
  245. if (this.connections[direction] != undefined) {
  246. return direction;
  247. }
  248. }
  249. }
  250. // TODO: Return all things of type that are in placed RoomRandom.
  251. public static getActive (type : typeof Thing) {
  252. }
  253. }
  254. // random = new RoomRandom...
  255. // random.connectableOn = [Room.DIRECTION_NORTH, Room.DIRECTION_SOUTH...]