Skip to content

Commit

Permalink
improve work request management
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonBurke committed May 26, 2024
1 parent 539266f commit 825c1ba
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/other/profilerRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { StatsManager } from 'international/stats'
import { ConstructionManager } from 'room/construction/construction'
import { RemotesManager } from 'room/commune/remotesManager'
import { HaulRequestManager } from 'room/commune/haulRequestManager'
import { WorkRequestManager } from 'room/commune/workRequest'
import { Quad } from 'room/creeps/roleManagers/antifa/quad'
import { DynamicSquad } from 'room/creeps/roleManagers/antifa/dynamicSquad'
import { Duo } from 'room/creeps/roleManagers/antifa/duo'
Expand Down Expand Up @@ -116,6 +115,7 @@ import { RoomServices } from 'room/roomServices'
import { HaulerNeedOps } from 'room/commune/haulerNeedOps'
import { MyCreepServices } from 'room/creeps/myCreepServices'
import { MyPowerCreepServices } from 'room/creeps/myPowerCreepServices'
import { WorkRequestOps } from 'room/commune/workRequest'

export function profilerRegister() {
// Classes
Expand Down Expand Up @@ -162,7 +162,6 @@ export function profilerRegister() {
profiler.registerClass(ConstructionManager, 'ConstructionManager')
profiler.registerClass(RemotesManager, 'RemotesManager')
profiler.registerClass(HaulRequestManager, 'HaulRequestManager')
profiler.registerClass(WorkRequestManager, 'WorkRequestManager')
profiler.registerClass(EndTickCreepManager, 'EndTickCreepManager')
profiler.registerClass(LinkManager, 'LinkManager')
profiler.registerClass(DefenceProcs, 'DefenceProcs')
Expand Down Expand Up @@ -193,6 +192,7 @@ export function profilerRegister() {
profiler.registerClass(ObserverProcs, 'ObserverProcs')
profiler.registerClass(PowerSpawnProcs, 'PowerSpawnProcs')
profiler.registerClass(HaulerNeedOps, 'HaulerNeedOps')
profiler.registerClass(WorkRequestOps, "WorkRequestOps")

// Creep classes

Expand Down
3 changes: 0 additions & 3 deletions src/room/commune/commune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import './factory'
import { LabManager } from './labs'
import './links'
import { RemotesManager } from './remotesManager'
import { WorkRequestManager } from './workRequest'
import { CombatRequestManager } from './combatRequest'
import { HaulRequestManager } from './haulRequestManager'
import { packCoord, unpackPosAt } from 'other/codec'
Expand Down Expand Up @@ -49,7 +48,6 @@ export class CommuneManager {

remotesManager: RemotesManager

workRequestManager: WorkRequestManager
combatRequestManager: CombatRequestManager
haulRequestManager: HaulRequestManager

Expand Down Expand Up @@ -90,7 +88,6 @@ export class CommuneManager {

this.remotesManager = new RemotesManager(this)

this.workRequestManager = new WorkRequestManager(this)
this.combatRequestManager = new CombatRequestManager(this)
this.haulRequestManager = new HaulRequestManager(this)

Expand Down
46 changes: 44 additions & 2 deletions src/room/commune/communeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { RoomUtils } from 'room/roomUtils'
import { CommuneDataOps, communeData } from './communeData'
import { CommuneUtils } from './communeUtils'
import {
CombatRequestKeys,
Result,
RoomLogisticsRequestTypes,
RoomMemoryKeys,
RoomTypes,
WorkRequestKeys,
creepRoles,
haulerUpdateDefault,
} from '../../constants/general'
Expand All @@ -24,6 +26,7 @@ import { TowerProcs } from './towerProcs'
import { HaulerNeedOps } from './haulerNeedOps'
import { CommuneManager } from './commune'
import { RoomOps } from 'room/roomOps'
import { WorkRequestOps } from './workRequest'

/**
* Minor processes for communes
Expand Down Expand Up @@ -158,7 +161,7 @@ export class CommuneOps {
TerminalProcs.preTickRun(room)
communeManager.remotesManager.initRun()
communeManager.haulRequestManager.preTickRun()
communeManager.workRequestManager.preTickRun()
WorkRequestOps.tryCreateWorkRequest(room)
}

private static preTickTest(room: Room) {
Expand All @@ -184,7 +187,7 @@ export class CommuneOps {

TerminalProcs.run(room)

communeManager.workRequestManager.run()
WorkRequestOps.manageWorkRequest(room)
communeManager.combatRequestManager.run()
communeManager.haulRequestManager.run()

Expand Down Expand Up @@ -351,4 +354,43 @@ export class CommuneOps {
remoteMemory[RoomMemoryKeys.type] = RoomTypes.neutral
RoomNameUtils.cleanMemory(remoteName)
}

static stopWorkRequestResponse(room: Room, deleteCombat?: boolean) {
const roomMemory = Memory.rooms[room.name]
const request = Memory.workRequests[roomMemory[RoomMemoryKeys.workRequest]]

if (deleteCombat) {
CommuneOps.deleteCombatForWorkRequest(room)
}

delete request[WorkRequestKeys.responder]
delete roomMemory[RoomMemoryKeys.workRequest]
}

static abandonWorkRequest(room: Room, abandonTime: number = 20000) {
const roomMemory = Memory.rooms[room.name]

CommuneOps.deleteCombatForWorkRequest(room)

delete Memory.workRequests[roomMemory[RoomMemoryKeys.workRequest]]
delete roomMemory[RoomMemoryKeys.workRequest]
}

static deleteCombatForWorkRequest(room: Room) {
const workRequestName = Memory.rooms[room.name][RoomMemoryKeys.workRequest]
const combatRequest = Memory.combatRequests[workRequestName]
if (!combatRequest) return

if (combatRequest[CombatRequestKeys.responder]) {
const combatRequestResponder = Game.rooms[combatRequest[CombatRequestKeys.responder]]
CommuneOps.deleteCombatRequest(
combatRequestResponder,
combatRequest[CombatRequestKeys.responder],
combatRequestResponder.memory[RoomMemoryKeys.combatRequests].indexOf(workRequestName),
)
return
}

delete Memory.combatRequests[workRequestName]
}
}
99 changes: 22 additions & 77 deletions src/room/commune/workRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,25 @@ import { CommuneOps } from './communeOps'

const checkRoomStatusInverval = randomIntRange(200, 500)

export class WorkRequestManager {
communeManager: CommuneManager
export class WorkRequestOps {
static tryCreateWorkRequest(room: Room) {

constructor(communeManager: CommuneManager) {
this.communeManager = communeManager
}
// create a workRequest if needed

preTickRun() {
const { room } = this.communeManager
if (room.roomManager.structures.spawn.length) return

// create a workRequest if needed
let request = Memory.workRequests[room.name]
if (request) {
request[WorkRequestKeys.priority] = 0
return
}

if (room.roomManager.structures.spawn.length) return

let request = Memory.workRequests[room.name]
if (request) {
request[WorkRequestKeys.priority] = 0
return
request = Memory.workRequests[room.name] = {
[WorkRequestKeys.priority]: 0,
}
}

request = Memory.workRequests[room.name] = {
[WorkRequestKeys.priority]: 0,
}
}

run() {
const { room } = this.communeManager
static manageWorkRequest(room: Room) {
const roomMemory = Memory.rooms[room.name]

const requestName = roomMemory[RoomMemoryKeys.workRequest]
Expand All @@ -55,7 +47,7 @@ export class WorkRequestManager {

// if we have no spawn, stop
if (!room.roomManager.structures.spawn.length) {
this.stopResponse(true)
CommuneOps.stopWorkRequestResponse(room, true)
return
}

Expand All @@ -68,7 +60,7 @@ export class WorkRequestManager {
) {
// Delete the request so long as the new type isn't ally

this.stopResponse(type !== RoomTypes.ally)
CommuneOps.stopWorkRequestResponse(room, type !== RoomTypes.ally)
return
}

Expand All @@ -78,25 +70,25 @@ export class WorkRequestManager {
Memory.rooms[room.name][RoomMemoryKeys.status] !==
Memory.rooms[requestName][RoomMemoryKeys.status]
) {
this.delete(requestName, request)
CommuneOps.abandonWorkRequest(room)
return
}

// If the request has been abandoned, have the commune abandon it too

if (request[WorkRequestKeys.abandon] > 0) {
this.stopResponse()
CommuneOps.stopWorkRequestResponse(room)
return
}

if (room.energyCapacityAvailable < 650) {
this.stopResponse()
CommuneOps.stopWorkRequestResponse(room)
return
}

const requestRoom = Game.rooms[requestName]
if (!requestRoom && Game.gcl.level === CollectiveManager.communes.size) {
this.stopResponse(true)
CommuneOps.stopWorkRequestResponse(room, true)
return
}

Expand All @@ -111,7 +103,7 @@ export class WorkRequestManager {
requestRoom.roomManager.structures.spawn.length &&
requestRoom.roomManager.structures.spawn.find(spawn => spawn.my)
) {
this.delete(requestName, request)
CommuneOps.abandonWorkRequest(room)
return
}

Expand All @@ -121,7 +113,8 @@ export class WorkRequestManager {
if (invaderCores.length) {
// Abandon for the core's remaining existance plus the estimated reservation time

this.abandon(
CommuneOps.abandonWorkRequest(
room,
invaderCores[0].effects[EFFECT_COLLAPSE_TIMER].ticksRemaining + CONTROLLER_RESERVE_MAX,
)
return
Expand Down Expand Up @@ -158,53 +151,5 @@ export class WorkRequestManager {
if (request[WorkRequestKeys.minDamage] > 0 || request[WorkRequestKeys.minHeal] > 0) this.abandonRemote()
} */
}

private stopResponse(deleteCombat?: boolean) {
const roomMemory = this.communeManager.room.memory
const request = Memory.workRequests[roomMemory[RoomMemoryKeys.workRequest]]

if (deleteCombat) this.deleteCombat()

delete request[WorkRequestKeys.responder]
delete roomMemory[RoomMemoryKeys.workRequest]
}

private delete(requestName: string, request: WorkRequest) {
const roomMemory = this.communeManager.room.memory

this.deleteCombat()

delete Memory.workRequests[roomMemory[RoomMemoryKeys.workRequest]]
delete roomMemory[RoomMemoryKeys.workRequest]
}

private abandon(abandonTime: number = 20000) {
const roomMemory = this.communeManager.room.memory
const request = Memory.workRequests[roomMemory[RoomMemoryKeys.workRequest]]

this.deleteCombat()

request[WorkRequestKeys.abandon] = abandonTime
delete request[WorkRequestKeys.responder]
delete roomMemory[RoomMemoryKeys.workRequest]
}

private deleteCombat() {
const workRequestName = this.communeManager.room.memory[RoomMemoryKeys.workRequest]
const combatRequest = Memory.combatRequests[workRequestName]
if (!combatRequest) return

if (combatRequest[CombatRequestKeys.responder]) {
const combatRequestResponder = Game.rooms[combatRequest[CombatRequestKeys.responder]]
CommuneOps.deleteCombatRequest(
combatRequestResponder,
combatRequest[CombatRequestKeys.responder],
combatRequestResponder.memory[RoomMemoryKeys.combatRequests].indexOf(workRequestName),
)
return
}

delete Memory.combatRequests[workRequestName]
}
}

0 comments on commit 825c1ba

Please sign in to comment.