Skip to content

Commit 87ad09c

Browse files
committed
Working on init memory and room information
1 parent 08e52a7 commit 87ad09c

File tree

3 files changed

+175
-50
lines changed

3 files changed

+175
-50
lines changed

src/init.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare global {
2+
interface Memory {
3+
username: string;
4+
}
5+
}
6+
7+
export default class init {
8+
userName: string;
9+
10+
constructor(roomName:string) {
11+
this.userName = this.getUserName(roomName);
12+
}
13+
14+
getUserName(roomName:string) {
15+
const myStructures = Game.rooms[roomName].find(FIND_MY_SPAWNS);
16+
return myStructures[0].owner.username;
17+
}
18+
}

src/initRoom.ts

Lines changed: 140 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,167 @@
11
declare global {
22
interface RoomMemory {
33
roomName: string;
4+
roomOwner: string;
5+
roomStatus: string;
46
exits: ExitsInformation;
57
sources: Id<Source>[];
68
harvesters: number;
9+
containers: number;
710
level: number;
811
phase: number;
912
lastChecked: number;
1013
}
1114
}
1215

13-
const initRoom = (roomName:string) => {
14-
Memory.rooms[roomName].roomName = Game.rooms[roomName].name;
15-
Memory.rooms[roomName].exits = Game.map.describeExits(roomName);
16-
Memory.rooms[roomName].sources = getSources(roomName);
17-
Memory.rooms[roomName].harvesters = getHarvesters(roomName);
18-
Memory.rooms[roomName].level = getLevel(roomName);
19-
Memory.rooms[roomName].phase = getPhase(roomName);
20-
Memory.rooms[roomName].lastChecked = Game.time;
21-
};
16+
export default class initRoom {
17+
roomName: string;
18+
roomOwner: string;
19+
roomStatus: string;
20+
exits: ExitsInformation;
21+
sources: Id<Source>[];
22+
harvesters: number;
23+
containers: number;
24+
level: number;
25+
phase: number;
26+
lastChecked: number;
2227

23-
const getSources = (roomName:string) => {
24-
let sources = Game.rooms[roomName].find(FIND_SOURCES);
25-
let sourceId:Id<Source>[] = [];
26-
for (let source of sources) {
27-
sourceId.push(source.id);
28+
constructor(roomName:string) {
29+
this.roomName = roomName;
30+
this.roomOwner = this.getRoomOwner(roomName);
31+
this.roomStatus = this.getRoomStatus(roomName);
32+
this.exits = Game.map.describeExits(roomName);
33+
this.sources = this.getSources(roomName);
34+
this.harvesters = this.getHarvesters(roomName);
35+
this.containers = this.getContainers(roomName);
36+
this.level = this.getLevel(roomName);
37+
this.phase = this.getPhase(roomName);
38+
this.lastChecked = Game.time;
2839
}
2940

30-
return sourceId;
31-
};
41+
initMemory(roomName = this.roomName): void {
42+
Memory.rooms[roomName].roomName = roomName;
43+
Memory.rooms[roomName].roomOwner = this.roomOwner;
44+
Memory.rooms[roomName].roomStatus = this.roomStatus;
45+
Memory.rooms[roomName].exits = this.exits;
46+
Memory.rooms[roomName].sources = this.sources;
47+
Memory.rooms[roomName].harvesters = this.harvesters;
48+
Memory.rooms[roomName].containers = this.containers;
49+
Memory.rooms[roomName].level = this.level;
50+
Memory.rooms[roomName].phase = this.phase;
51+
Memory.rooms[roomName].lastChecked = this.lastChecked;
52+
}
3253

33-
const getHarvesters = (roomName:string) => {
34-
let harvesterCount = 0;
35-
for(let source of Memory.rooms[roomName].sources) {
36-
harvesterCount = harvesterCount + getFreeSpacesAroundSource(source).length;
54+
/**
55+
*
56+
* @param {string} roomName - The name of the room
57+
* @returns {string} The username of the room owner, None if the room isn't owned, or Neutral if there is no controller
58+
*/
59+
getRoomOwner(roomName:string) {
60+
let owner;
61+
if (Game.rooms[roomName].controller) {
62+
if (Game.rooms[roomName].controller?.owner) {
63+
owner = Game.rooms[roomName].controller?.owner?.username || "None";
64+
} owner = "None";
65+
} owner = "Neutral";
66+
this.roomOwner = owner;
67+
return this.roomOwner;
3768
}
3869

39-
return harvesterCount;
40-
};
70+
/**
71+
*
72+
* @param {string} roomName - The name of the room
73+
* @returns {string} The status of the room either "Mine" if user owns, "Hostile" if other user owns or has reserved room, "Reserved" if user has it reserved, "Available" if room has controller but no owner, "Neutral" if room has no controller
74+
*/
75+
getRoomStatus(roomName:string) {
76+
let status;
77+
if (Game.rooms[roomName].controller) {
78+
if (Game.rooms[roomName].controller?.my) {
79+
status = "Mine";
80+
} else if (Game.rooms[roomName].controller?.owner?.username) {
81+
status = "Hostile";
82+
} else if (Game.rooms[roomName].controller?.reservation?.username) {
83+
if (Game.rooms[roomName].controller?.reservation?.username === Memory.username) {
84+
status = "Reserved";
85+
} else status = "Hostile";
86+
} status = "Available";
87+
} status = "Neutral";
88+
this.roomStatus = status;
89+
return this.roomStatus;
90+
}
4191

42-
const getLevel = (roomName:string) => {
43-
return Game.rooms[roomName].controller?.level ||0;
44-
};
92+
getSources(roomName:string) {
93+
const sources = Game.rooms[roomName].find(FIND_SOURCES);
94+
let sourceId:Id<Source>[] = [];
95+
for (let source of sources) {
96+
sourceId.push(source.id);
97+
}
98+
99+
this.sources = sourceId
100+
return this.sources;
101+
}
45102

46-
const getPhase = (roomName:string) => {
47-
return 0;
48-
};
103+
getHarvesters(roomName:string) {
104+
let harvesterCount = 0;
105+
for(let source of this.sources) {
106+
harvesterCount = harvesterCount + this.getFreeSpacesAroundSource(source).length;
107+
}
108+
this.harvesters = harvesterCount;
109+
return this.harvesters;
110+
}
111+
112+
getContainers(roomName:string) {
113+
const containers = Game.rooms[roomName].find(FIND_STRUCTURES, {
114+
filter: { structureType: STRUCTURE_CONTAINER}
115+
});
49116

50-
const getFreeSpacesAroundSource = (sourceId:Id<Source>) => {
51-
let source = Game.getObjectById(sourceId);
52-
let freeSpaces = [];
117+
const buildingContainers = Game.rooms[roomName].find(FIND_CONSTRUCTION_SITES, {
118+
filter: { structureType: STRUCTURE_CONTAINER}
119+
});
120+
121+
this.containers = containers.length + buildingContainers.length;
122+
return this.containers;
123+
}
53124

54-
if (source) {
55-
for (let x = (source.pos.x -1); x < (source.pos.x + 2); x++) {
56-
for (let y = (source.pos.y -1); y < (source.pos.y +2); y++) {
57-
if ((source.pos.x === x) && (source.pos.y === y)) {continue;}
58-
let terrrain = source.room.getTerrain();
59-
if (terrrain.get(x,y) !== TERRAIN_MASK_WALL) {
60-
freeSpaces.push([x,y]);
125+
getLevel(roomName:string) {
126+
this.level = Game.rooms[roomName].controller?.level || 0;
127+
return this.level;
128+
}
129+
130+
getPhase(roomName:string) {
131+
this.phase = 0;
132+
return this.phase;
133+
}
134+
135+
getLastChecked(roomName:string) {
136+
this.lastChecked = Game.time;
137+
return this.lastChecked;
138+
}
139+
140+
getFreeSpacesAroundSource(sourceId:Id<Source>) {
141+
let source = Game.getObjectById(sourceId);
142+
let freeSpaces = [];
143+
144+
if (source) {
145+
for (let x = (source.pos.x -1); x < (source.pos.x + 2); x++) {
146+
for (let y = (source.pos.y -1); y < (source.pos.y +2); y++) {
147+
if ((source.pos.x === x) && (source.pos.y === y)) {continue;}
148+
let terrrain = source.room.getTerrain();
149+
if (terrrain.get(x,y) !== TERRAIN_MASK_WALL) {
150+
freeSpaces.push([x,y]);
151+
}
61152
}
62153
}
63154
}
155+
156+
return freeSpaces;
64157
}
65158

66-
return freeSpaces;
67-
};
68-
69-
export default (initRoom);
159+
getMemoryUpdates(roomName:string) {
160+
Memory.rooms[roomName].roomOwner = this.getRoomOwner(roomName);
161+
Memory.rooms[roomName].roomStatus = this.getRoomStatus(roomName);
162+
Memory.rooms[roomName].containers = this.getContainers(roomName);
163+
Memory.rooms[roomName].level = this.getLevel(roomName);
164+
Memory.rooms[roomName].phase = this.getPhase(roomName);
165+
Memory.rooms[roomName].lastChecked = this.getLastChecked(roomName);
166+
}
167+
}

src/main.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import roleUpgrader, { Upgrader } from "role.upgrader";
1212
import roleBuilder, { Builder } from "role.builder";
1313
import "prototype.spawn";
1414
import towerManager from "towerManager";
15-
import initRoom from "initRoom";
1615
import clearMemory from "helper";
16+
import initRoom from "initRoom";
17+
import init from "init";
1718

1819
declare global {
1920
/*
@@ -49,6 +50,12 @@ declare global {
4950
// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change
5051
// This utility uses source maps to get the line numbers and file names of the original, TS source code
5152
export const loop = () => {
53+
// Get my username if required
54+
if (!Memory.username) {
55+
const roomName = Object.keys(Game.rooms)[0];
56+
let initalise = new init(roomName);
57+
Memory.username = initalise.getUserName(roomName);
58+
}
5259

5360
// Automatically delete memory of missing creeps
5461
for (const name in Memory.creeps) {
@@ -59,17 +66,19 @@ export const loop = () => {
5966
}
6067

6168
// Ensure Room Memory is Initalised
62-
for (const name in Game.rooms) {
63-
if (!Memory.rooms[name].roomName) {
64-
initRoom(name);
65-
}
69+
for (const roomName in Game.rooms) {
70+
const roomInit = new initRoom(roomName);
71+
72+
if (!Memory.rooms[roomName]) Memory.rooms[roomName] = {} as any;
73+
if (!Memory.rooms[roomName].roomName) roomInit.initMemory(roomName);
74+
if (Memory.rooms[roomName].lastChecked % 100 === 5) roomInit.getMemoryUpdates(roomName);
6675
}
6776

6877
// Get Current Progress
6978
Memory.gcl = Game.gcl;
7079

7180
// Run Tick Logic
72-
for (let name in Game.creeps) {
81+
for (const name in Game.creeps) {
7382
let creep = Game.creeps[name];
7483
if (creep.memory.role === 'harvester') {
7584
roleHarvester.run(creep);
@@ -83,7 +92,7 @@ export const loop = () => {
8392
}
8493

8594
// Run Spawn Logic
86-
for (let name in Game.spawns) {
95+
for (const name in Game.spawns) {
8796
let spawn = Game.spawns[name];
8897
if (!spawn.spawning && (spawn.room.energyAvailable >=
8998
(BODYPART_COST['move'] + BODYPART_COST['carry'] + BODYPART_COST['work']))) {
@@ -92,7 +101,7 @@ export const loop = () => {
92101
}
93102

94103
// Run Tower Logic
95-
let towers = filter(Game.structures, (s:Structure) => s.structureType == STRUCTURE_TOWER) as StructureTower[];
104+
const towers = filter(Game.structures, (s:Structure) => s.structureType == STRUCTURE_TOWER) as StructureTower[];
96105
for (let tower of towers) {
97106
towerManager.attackClosestHostile(tower);
98107
towerManager.repairClosestStructure(tower);

0 commit comments

Comments
 (0)