Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. import {Diplomat} from "./Diplomat";
  2. import {TradeNetwork} from "./TradeNetwork";
  3. export class WorldMap {
  4.  
  5. public controlledRooms: {[roomName: string]: Room } = {};
  6.  
  7. public allyMap: {[roomName: string]: RoomMemory } = {};
  8. public allyRooms: Room[] = [];
  9. public tradeMap: {[roomName: string]: RoomMemory } = {};
  10. public tradeRooms: Room[] = [];
  11. public foesMap: {[roomName: string]: RoomMemory } = {};
  12. public foesRooms: Room[] = [];
  13.  
  14. public activeNukes: {tick: number; roomName: string}[];
  15. public artRooms = ARTROOMS;
  16.  
  17. private diplomat: Diplomat;
  18.  
  19. constructor(diplomat: Diplomat) {
  20. this.diplomat = diplomat;
  21.  
  22. if (!Memory.empire) { Memory.empire = {}; }
  23. _.defaults(Memory.empire, {
  24. activeNukes: {}
  25. });
  26. this.activeNukes = Memory.empire.activeNukes;
  27. }
  28.  
  29. init() {
  30.  
  31. for (let roomName in Memory.rooms) {
  32. let memory = Memory.rooms[roomName];
  33. let room = Game.rooms[roomName];
  34.  
  35. if (room) {
  36. this.updateMemory(room);
  37. if (room.controller && room.controller.my) {
  38. this.radar(room);
  39. this.controlledRooms[roomName] = room;
  40. }
  41. }
  42.  
  43. if (this.diplomat.allies[memory.owner]) {
  44. this.allyMap[roomName] = memory;
  45. if (room) { this.allyRooms.push(room); }
  46. }
  47. if (this.diplomat.foes[memory.owner]) {
  48. this.foesMap[roomName] = memory;
  49. if (room) { this.foesRooms.push(room); }
  50. }
  51. if (memory.nextTrade) {
  52. this.tradeMap[roomName] = memory;
  53. if (room) { this.tradeRooms.push(room); }
  54. }
  55. }
  56. }
  57.  
  58. actions() {
  59. this.reportNukes();
  60. }
  61.  
  62. public addNuke(activeNuke: {tick: number; roomName: string}) {
  63. this.activeNukes.push(activeNuke);
  64. }
  65.  
  66. public reportNukes() {
  67. if (Game.time % TICK_FULL_REPORT !== 0) return;
  68.  
  69. for (let activeNuke of this.activeNukes) {
  70. console.log(`EMPIRE: ${Game.time - activeNuke.tick} till our nuke lands in ${activeNuke.roomName}`);
  71. }
  72. }
  73.  
  74.  
  75. private updateMemory(room: Room) {
  76. if (room.controller) {
  77. room.memory.level = room.controller.level;
  78. if (room.controller.owner) {
  79. room.memory.owner = room.controller.owner.username;
  80. }
  81. if (room.controller.owner && !room.controller.my) {
  82. room.memory.occupied = true;
  83. }
  84. else if (room.memory.occupied) {
  85. delete room.memory.occupied;
  86. }
  87. }
  88. }
  89.  
  90. private radar(scanningRoom: Room) {
  91. if (scanningRoom.controller.level < 8) { return; }
  92. if (Game.time < scanningRoom.memory.nextRadar) { return; }
  93.  
  94. // find observer
  95. let observer = _(scanningRoom.find<StructureObserver>(FIND_STRUCTURES))
  96. .filter(s => s.structureType === STRUCTURE_OBSERVER)
  97. .head();
  98. if (!observer) {
  99. console.log(`NETWORK: please add an observer in ${scanningRoom.name} to participate in network`);
  100. scanningRoom.memory.nextRadar = Game.time + 1000;
  101. return;
  102. }
  103.  
  104. if (!scanningRoom.memory.radarData) {
  105. console.log(`NETWORK: Beginning full radar scan in ${scanningRoom.name}`);
  106. scanningRoom.memory.radarData = { x: -10, y: -10 };
  107. }
  108. let radarData = scanningRoom.memory.radarData;
  109.  
  110. // scan loop
  111. let scanComplete = false;
  112. while (!scanComplete) {
  113. let roomName = WorldMap.findRelativeRoomName(scanningRoom.name, radarData.x, radarData.y);
  114. let scannedRoom = Game.rooms[roomName];
  115. if (scannedRoom) {
  116. scannedRoom.memory.nextScan = Game.time + RADAR_INTERVAL;
  117. this.evaluateTrade(scannedRoom);
  118. // TODO: room selection code
  119. }
  120. else {
  121. if (!Memory.rooms[roomName]) Memory.rooms[roomName] = {} as RoomMemory;
  122. let roomMemory = Memory.rooms[roomName];
  123. if (!roomMemory.nextScan || Game.time >= roomMemory.nextScan) {
  124. observer.observeRoom(roomName);
  125. break;
  126. }
  127. }
  128.  
  129. scanComplete = this.incrementScan(radarData);
  130. if (scanComplete) {
  131. scanningRoom.memory.nextRadar = Game.time + RADAR_INTERVAL;
  132. console.log(`RADAR: Scan complete at ${scanningRoom.name}`);
  133. delete scanningRoom.memory.radarData;
  134. }
  135. }
  136. }
  137.  
  138. private evaluateTrade(room: Room) {
  139. if (!room.controller || room.controller.my || !TradeNetwork.canTrade(room)
  140. || !this.diplomat.partners[room.controller.owner.username]) { return; }
  141. if (!room.memory.nextTrade) { room.memory.nextTrade = Game.time; }
  142. }
  143.  
  144. private incrementScan(radarData: {x: number; y: number}) {
  145. // increment
  146. radarData.x++;
  147. if (radarData.x > 10) {
  148. radarData.x = -10;
  149. radarData.y++;
  150. if (radarData.y > 10) {
  151. return true;
  152. }
  153.  
  154. }
  155. }
  156.  
  157. public static findRelativeRoomName(roomName: string, xDelta: number, yDelta: number): string {
  158. let coords = this.getRoomCoordinates(roomName);
  159. let xDir = coords.xDir;
  160. let yDir = coords.yDir;
  161. let x = coords.x + xDelta;
  162. let y = coords.y + yDelta;
  163. if (x < 0) {
  164. x = Math.abs(x) - 1;
  165. xDir = this.negaDirection(xDir);
  166. }
  167. if (y < 0) {
  168. y = Math.abs(y) - 1;
  169. yDir = this.negaDirection(yDir);
  170. }
  171.  
  172. return xDir + x + yDir + y;
  173. }
  174.  
  175. public static findRoomCoordDeltas(origin: string, otherRoom: string): {x: number, y: number} {
  176. let originCoords = this.getRoomCoordinates(origin);
  177. let otherCoords = this.getRoomCoordinates(otherRoom);
  178. let xDelta = otherCoords.x - originCoords.x;
  179. if (originCoords.xDir === otherCoords.xDir) {
  180. if (originCoords.xDir === "W") {
  181. xDelta = -xDelta;
  182. }
  183. }
  184. else {
  185. xDelta = otherCoords.x + originCoords.x + 1;
  186. if (originCoords.xDir === "E") {
  187. xDelta = -xDelta;
  188. }
  189. }
  190. let yDelta = otherCoords.y - originCoords.y;
  191. if (originCoords.yDir === otherCoords.yDir) {
  192. if (originCoords.yDir === "S") {
  193. yDelta = -yDelta;
  194. }
  195. }
  196. else {
  197. yDelta = otherCoords.y + originCoords.y + 1;
  198. if (originCoords.yDir === "N") {
  199. yDelta = -yDelta
  200. }
  201. }
  202. return {x: xDelta, y: yDelta};
  203. }
  204.  
  205. public static findRelativeRoomDir(origin: string, otherRoom: string): number {
  206. let coordDeltas = this.findRoomCoordDeltas(origin, otherRoom);
  207. if (Math.abs(coordDeltas.x) === Math.abs(coordDeltas.y)) {
  208. if (coordDeltas.x > 0) {
  209. if (coordDeltas.y > 0) {
  210. return 2;
  211. }
  212. else {
  213. return 4;
  214. }
  215. }
  216. else if (coordDeltas.x < 0) {
  217. if (coordDeltas.y > 0) {
  218. return 8;
  219. }
  220. else {
  221. return 6;
  222. }
  223. }
  224. else {
  225. // must be the same missionRoom, no direction
  226. return 0;
  227. }
  228. }
  229. else {
  230. if (Math.abs(coordDeltas.x) > Math.abs(coordDeltas.y)) {
  231. if (coordDeltas.x > 0) {
  232. return 3;
  233. }
  234. else {
  235. return 7;
  236. }
  237. }
  238. else {
  239. if (coordDeltas.y > 0) {
  240. return 1;
  241. }
  242. else {
  243. return 5;
  244. }
  245. }
  246. }
  247. }
  248.  
  249. public static negaDirection(dir: string): string {
  250. switch (dir) {
  251. case "W":
  252. return "E";
  253. case "E":
  254. return "W";
  255. case "N":
  256. return "S";
  257. case "S":
  258. return "N";
  259. }
  260. }
  261.  
  262. /**
  263. * Return missionRoom coordinates for a given Room, authored by tedivm
  264. * @param roomName
  265. * @returns {{x: (string|any), y: (string|any), x_dir: (string|any), y_dir: (string|any)}}
  266. */
  267.  
  268. public static getRoomCoordinates(roomName: string): RoomCoord {
  269.  
  270. let coordinateRegex = /(E|W)(\d+)(N|S)(\d+)/g;
  271. let match = coordinateRegex.exec(roomName);
  272. if (!match) return;
  273.  
  274. let xDir = match[1];
  275. let x = match[2];
  276. let yDir = match[3];
  277. let y = match[4];
  278.  
  279. return {
  280. x: Number(x),
  281. y: Number(y),
  282. xDir: xDir,
  283. yDir: yDir,
  284. };
  285. }
  286.  
  287. public static roomTypeFromName(roomName: string): number {
  288. let coords = this.getRoomCoordinates(roomName);
  289. if (coords.x % 10 === 0 || coords.y % 10 === 0) {
  290. return ROOMTYPE_ALLEY;
  291. }
  292. else if (coords.x % 5 === 0 && coords.y % 5 === 0) {
  293. return ROOMTYPE_CORE;
  294. }
  295. else if (coords.x % 10 === 6 || coords.x % 10 === 4 || coords.y % 10 === 6 || coords.y % 10 === 4) {
  296. return ROOMTYPE_SOURCEKEEPER;
  297. }
  298. else {
  299. return ROOMTYPE_CONTROLLER;
  300. }
  301. }
  302.  
  303. public static findNearestCore(roomName: string): string {
  304.  
  305. let roomCoords = this.getRoomCoordinates(roomName);
  306. let x = Math.floor(roomCoords.x / 10) + 5;
  307. let y = Math.floor(roomCoords.y / 10) + 5;
  308.  
  309. return roomCoords.xDir + x + roomCoords.yDir + y;
  310. }
  311. }
  312.  
  313. export const ARTROOMS = {
  314.  
  315. };
  316.  
  317. export const TICK_FULL_REPORT = 0;
  318. export const ROOMTYPE_SOURCEKEEPER = -1301;
  319. export const ROOMTYPE_CORE = -1302;
  320. export const ROOMTYPE_CONTROLLER = -1303;
  321. export const ROOMTYPE_ALLEY = -1304;
  322. export const RADAR_INTERVAL = 10000;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement