RegionRandom.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /// <reference path="../Region.ts" />
  2. /// <reference path="../Shuffler.ts" />
  3. /// <reference path="ShufflerDirection.ts" />
  4. /// <reference path="RoomRandomMap.ts" />
  5. /// <reference path="RoomRandom.ts" />
  6. /// <reference path="RoomRandomFodder.ts" />
  7. interface RandomizingRoomOptions {
  8. region : RegionRandom;
  9. room : RoomRandom;
  10. map : RoomRandomMap;
  11. }
  12. class RegionRandom extends Region {
  13. private randomized = false;
  14. public map : RoomRandomMap;
  15. public fodderRoomClass : typeof RoomRandom = RoomRandomFodder;
  16. public placedRooms : Array<RoomRandom> = [];
  17. public constructor (name : string, map? : RoomRandomMap) {
  18. super(name);
  19. this.map = map == undefined ? new RoomRandomMap() : map;
  20. }
  21. public async randomize () {
  22. if (!this.randomized) {
  23. await RegionRandom.rulebookRandomizeRegion.execute({
  24. noun : this
  25. });
  26. }
  27. }
  28. public static rng : () => number = () => { return Math.random(); };
  29. public static rulebookRandomizeRegion = new Rulebook<Region>("Randomizing Random Region something");
  30. public static rulebookPlaceRoom = new Rulebook<RandomizingRoomOptions>("Placing Random Room something");
  31. public static rulebookBeforePlaceRoom = new Rulebook<RandomizingRoomOptions>("Before placing Random Room something");
  32. public static rulebookAfterPlaceRoom = new Rulebook<RandomizingRoomOptions>("After placing Random Room something");
  33. public static ruleFirstRandomizeRegion = RegionRandom.rulebookRandomizeRegion.createAndAddRule({
  34. name : "Empty map cache to start randomizing region",
  35. firstPriority : Rule.PRIORITY_HIGHEST,
  36. code : runner => {
  37. let region = <RegionRandom> runner.noun;
  38. // We don't want to connect a region to another region unintentionally
  39. region.map.emptyCache();
  40. }
  41. });
  42. public static ruleBasicRandomizeRegion = RegionRandom.rulebookRandomizeRegion.createAndAddRule({
  43. name : "Randomize all unplaced, randomizable rooms in region something",
  44. code : async runner => {
  45. let region = <RegionRandom> runner.noun;
  46. let roomShuffler = new Shuffler(
  47. Region.InRelation.getAllRightTypes(region, RoomRandom).filter((room : RoomRandom) => {
  48. return room.randomizable && !room.placed &&
  49. (room == WorldState.player.getRoom() || (RegionRandom.rng() * 100) <= room.appearChance);
  50. }), RegionRandom.rng
  51. );
  52. for (let room = roomShuffler.getOne(); room != undefined; room = roomShuffler.getOne()) {
  53. // Prevent available connections from getting too low
  54. // If we're placing rooms with too few availableConnections, we might end up reaching 0 availableConnections
  55. // Which would mean no more rooms can be placed.
  56. while (region.map.rooms > 0 && region.map.getAvailableConnections() < 4) {
  57. let fodder = new region.fodderRoomClass();
  58. let options = <RandomizingRoomOptions> {
  59. map : region.map,
  60. room : fodder,
  61. region : region
  62. };
  63. region.place(fodder);
  64. await RegionRandom.rulebookBeforePlaceRoom.execute({noun : options});
  65. await RegionRandom.rulebookPlaceRoom.execute({noun : options});
  66. await RegionRandom.rulebookAfterPlaceRoom.execute({noun : options});
  67. }
  68. let options = <RandomizingRoomOptions> {
  69. map : region.map,
  70. room : room,
  71. region : region
  72. };
  73. await RegionRandom.rulebookBeforePlaceRoom.execute({noun : options});
  74. await RegionRandom.rulebookPlaceRoom.execute({noun : options});
  75. await RegionRandom.rulebookAfterPlaceRoom.execute({noun : options});
  76. if (!room.placed) {
  77. Elements.CurrentTurnHandler.printAsError(new Say("Was unable to place room ", room, ". Game might be unplayable."));
  78. }
  79. }
  80. }
  81. });
  82. public static ruleAddExtraConnections = RegionRandom.rulebookRandomizeRegion.createAndAddRule({
  83. firstPriority : Rule.PRIORITY_LOWEST,
  84. name : "Add extra connections to rooms in region",
  85. code : runner => {
  86. let region = <RegionRandom> runner.noun;
  87. let placedRooms = Region.InRelation.getAllRightTypes(region, RoomRandom).filter((room : RoomRandom) => {
  88. return room.randomizable && room.placed;
  89. });
  90. placedRooms.forEach((room : RoomRandom) => {
  91. let myCoordinates = region.map.getCoordinates(room);
  92. let directionShuffler = new Shuffler<number>(room.connectableOn.slice(0), RegionRandom.rng);
  93. for (let direction = directionShuffler.getOne(); direction != undefined && (RegionRandom.rng() * 100) <= room.extraConnectionChance; direction = directionShuffler.getOne()) {
  94. if (room.connections[direction] == undefined) {
  95. let otherCoordinates = Room.shift(myCoordinates, direction);
  96. let otherRoom = region.map.getRoom(otherCoordinates[0], otherCoordinates[1]);
  97. if (otherRoom != undefined && otherRoom.randomizable
  98. && otherRoom.isConnectableOn(OppositeDirection[Direction[direction]])
  99. && (RegionRandom.rng() * 100) <= otherRoom.extraConnectionChance) {
  100. room.mapRoom(otherRoom, direction);
  101. }
  102. }
  103. }
  104. });
  105. }
  106. });
  107. public static rulePlaceFirstRoom = RegionRandom.rulebookPlaceRoom.createAndAddRule({
  108. name : "Placing First room something",
  109. firstPriority : Rule.PRIORITY_HIGHEST,
  110. code : runner => {
  111. let placingOptions = <RandomizingRoomOptions> runner.noun;
  112. if (placingOptions.map.isFree(0, 0)) {
  113. placingOptions.map.map(placingOptions.room, 0, 0);
  114. return true;
  115. }
  116. },
  117. conditions : runner => {
  118. return runner.noun.map.rooms == 0;
  119. }
  120. });
  121. public static rulePlaceNonTrickyRoom = RegionRandom.rulebookPlaceRoom.createAndAddRule({
  122. name : "Placing non-tricky room something",
  123. firstPriority : Rule.PRIORITY_HIGH,
  124. code : runner => {
  125. let placingOptions = <RandomizingRoomOptions> runner.noun;
  126. //let directionShuffler = new Shuffler<number>(placingOptions.room.connectableOn);
  127. let preferredGrowthDirection = placingOptions.map.getPreferredGrowth();
  128. let directionShuffler = new ShufflerDirection(placingOptions.room.connectableOn, preferredGrowthDirection);
  129. //for (let direction = directionShuffler.getOne(); direction != undefined; direction = directionShuffler.getOne()) {
  130. for (let direction = directionShuffler.getDirection(); direction != undefined; direction = directionShuffler.getDirection()) {
  131. let oppositeDirection = OppositeDirection[Direction[direction]];
  132. let connectableRoom = placingOptions.map.getAnyFromCache(oppositeDirection);
  133. if (connectableRoom != undefined) {
  134. let otherCoordinates = placingOptions.map.getCoordinates(connectableRoom);
  135. let myCoordinates = Room.shift(otherCoordinates, oppositeDirection);
  136. placingOptions.room.mapRoom(connectableRoom, direction);
  137. placingOptions.map.map(placingOptions.room, myCoordinates[0], myCoordinates[1]);
  138. return true;
  139. }
  140. }
  141. },
  142. conditions : runner => {
  143. return runner.noun.room.trickyCode == undefined;
  144. }
  145. });
  146. public static rulePlaceTrickyRoom = RegionRandom.rulebookPlaceRoom.createAndAddRule({
  147. name : "Placing tricky room something",
  148. code : runner => {
  149. let placingOptions = <RandomizingRoomOptions> runner.noun;
  150. let placedRooms = <Array<RoomRandom>> placingOptions.region.getRooms().filter((room) => {
  151. return room instanceof RoomRandom && room.placed;
  152. });
  153. if (placedRooms.length == 0) {
  154. Elements.CurrentTurnHandler.printAsError("Unable to place room " + placingOptions.room.getPrintedName() + ": There are no rooms to connect to!");
  155. return false;
  156. }
  157. let roomShuffler = new Shuffler(placedRooms, RegionRandom.rng);
  158. for (let connectableRoom = roomShuffler.getOne(); connectableRoom != undefined; connectableRoom = roomShuffler.getOne()) {
  159. let trickier = <TrickierOptions> {
  160. region : placingOptions.region,
  161. map : placingOptions.map,
  162. otherRoom : connectableRoom
  163. };
  164. let tricky = placingOptions.room.getAnyDirection(trickier);
  165. if (tricky != undefined) {
  166. placingOptions.room.mapRoom(connectableRoom, tricky.trickyRoomDirection);
  167. placingOptions.map.map(placingOptions.room, tricky.x, tricky.y);
  168. return true;
  169. }
  170. }
  171. // THIS WORKS
  172. // I DON'T KNOW WHY
  173. // I DON'T CARE WHY
  174. // LEAVE
  175. let connectableThroughFodder = (fodderStep : number, connectingRoom : RoomRandom) => {
  176. let trickier = <TrickierOptions> {
  177. region : placingOptions.region,
  178. map : placingOptions.map,
  179. otherRoom : connectingRoom
  180. };
  181. if (fodderStep == 0) {
  182. return placingOptions.room.getAnyDirection(trickier);
  183. } else {
  184. let newFodder = new (placingOptions.region.fodderRoomClass)();
  185. //let directionShuffler = new Shuffler<number>(newFodder.connectableOn, RegionRandom.rng);
  186. let preferredGrowthDirection = placingOptions.map.getPreferredGrowth();
  187. let directionShuffler = new ShufflerDirection(placingOptions.room.connectableOn, preferredGrowthDirection);
  188. for (let direction = directionShuffler.getDirection(); direction != undefined; direction = directionShuffler.getDirection()) {
  189. let oppositeDirection = OppositeDirection[Direction[direction]];
  190. let otherCoordinates = placingOptions.map.getCoordinates(connectingRoom);
  191. let wouldbeCoordinates = Room.shift(otherCoordinates, oppositeDirection);
  192. let fodderTricky = {
  193. otherRoom : connectingRoom,
  194. otherRoomDirection : oppositeDirection,
  195. trickyRoomDirection : direction,
  196. map : placingOptions.map,
  197. region : placingOptions.region,
  198. x : wouldbeCoordinates[0],
  199. y : wouldbeCoordinates[1]
  200. };
  201. if (newFodder.isPlaceable(fodderTricky)) {
  202. newFodder.mapRoom(connectingRoom, fodderTricky.trickyRoomDirection);
  203. placingOptions.map.map(newFodder, fodderTricky.x, fodderTricky.y);
  204. let nextTricky = connectableThroughFodder(fodderStep - 1, newFodder);
  205. if (nextTricky != undefined) {
  206. placingOptions.region.place(newFodder);
  207. return nextTricky;
  208. } else {
  209. newFodder.unmapRoom(fodderTricky.trickyRoomDirection);
  210. placingOptions.map.unmap(fodderTricky.x, fodderTricky.y);
  211. }
  212. }
  213. }
  214. }
  215. };
  216. for (let fodderLevel = 1; fodderLevel < 10; fodderLevel++) {
  217. roomShuffler.restart();
  218. for (let connectableRoom = roomShuffler.getOne(); connectableRoom != undefined; connectableRoom = roomShuffler.getOne()) {
  219. let tricky = connectableThroughFodder(fodderLevel, connectableRoom);
  220. if (tricky != undefined) {
  221. placingOptions.room.mapRoom(tricky.otherRoom, tricky.trickyRoomDirection);
  222. placingOptions.map.map(placingOptions.room, tricky.x, tricky.y);
  223. return true;
  224. }
  225. }
  226. }
  227. Elements.CurrentTurnHandler.printAsError("Unable to place room " + placingOptions.room.getPrintedName() + ": All attempts failed");
  228. return false;
  229. }
  230. });
  231. }