1
1
declare global {
2
2
interface RoomMemory {
3
3
roomName : string ;
4
+ roomOwner : string ;
5
+ roomStatus : string ;
4
6
exits : ExitsInformation ;
5
7
sources : Id < Source > [ ] ;
6
8
harvesters : number ;
9
+ containers : number ;
7
10
level : number ;
8
11
phase : number ;
9
12
lastChecked : number ;
10
13
}
11
14
}
12
15
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 ;
22
27
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 ;
28
39
}
29
40
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
+ }
32
53
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 ;
37
68
}
38
69
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
+ }
41
91
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
+ }
45
102
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
+ } ) ;
49
116
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
+ }
53
124
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
+ }
61
152
}
62
153
}
63
154
}
155
+
156
+ return freeSpaces ;
64
157
}
65
158
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
+ }
0 commit comments