Skip to content

Commit

Permalink
variable optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonBurke committed May 28, 2024
1 parent b1f25b6 commit 9ea5481
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
42 changes: 20 additions & 22 deletions src/room/creeps/creepMoveProcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { CustomPathFinderArgs, PathGoal, CustomPathFinder } from 'international/customPathFinder'
import { packCoord, packPos, packPosList, unpackCoord, unpackPos, unpackPosAt } from 'other/codec'
import {
Utils,
areCoordsEqual,
arePositionsEqual,
findAdjacentCoordsToCoord,
Expand Down Expand Up @@ -260,26 +261,23 @@ export class CreepMoveProcs {

static tryRunMoveRequest(creep: Creep | PowerCreep, moveTargets: MoveTargets) {
// revisit this in the future
/* if (creep instanceof Creep && creep.spawning) return */
if (creep instanceof Creep && creep.spawning) return

const moveRequest = creep.moveRequest
if (!moveRequest) return
/* const moveRequest = creep.moveRequest
if (!moveRequest) return
// If the creep is already registered to move where it wants to
if (creep.moveTarget === moveRequest) return */

if (creep.moveRequest && Game.flags[FlagNames.debugMoveRequests]) {
creep.room.targetVisual(creep.pos, unpackCoord(creep.moveRequest), true)
if (Game.flags[FlagNames.debugMoveRequests]) {
creep.room.targetVisual(creep.pos, unpackCoord(moveRequest), true)
}

// Target coord

let targetCoord: Coord | undefined = creep.moveRequest
? unpackCoord(creep.moveRequest)
: /* creep.actionCoord */ undefined

if (!targetCoord) return
if (packCoord(targetCoord) === creep.moveTarget) return
if (moveRequest === creep.moveTarget) return

if (creep.moveTarget) {
delete moveTargets[creep.moveTarget]
Expand Down Expand Up @@ -315,9 +313,8 @@ export class CreepMoveProcs {
visitedCreeps: Set<string>,
cost: number,
): number {
let targetCoord: Coord | undefined = creep.moveRequest
? unpackCoord(creep.moveRequest)
: /* creep.actionCoord */ undefined
const moveRequest = creep.moveRequest
const moveRequestCoord: Coord | undefined = moveRequest ? unpackCoord(moveRequest) : undefined

const creepMemory = Memory.creeps[creep.name]

Expand All @@ -328,7 +325,8 @@ export class CreepMoveProcs {

// For each adjacent and containing position for the creep

for (const coord of CreepMoveProcs.getMoveOptions(creep, targetCoord)) {
const moveOptions = CreepMoveProcs.getMoveOptions(creep, moveRequestCoord)
for (const coord of moveOptions) {
const packedCoord = packCoord(coord)

const creepInWayName = moveTargets[packedCoord]
Expand All @@ -353,16 +351,17 @@ export class CreepMoveProcs {
const terrainType = terrain.get(coord.x, coord.y)
if (terrainType === TERRAIN_MASK_WALL) continue

const reservationType = creep.room.roomManager.reservedCoords.get(packedCoord)
// Revisit later
/* const reservationType = creep.room.roomManager.reservedCoords.get(packedCoord)
// Don't shove onto spawning-reserved coords
if (reservationType === ReservedCoordTypes.spawning) continue
if (reservationType === ReservedCoordTypes.spawning) continue */

// Use scoring to determine the cost of using the coord compared to potential others

let potentialCost = cost

if (targetCoord) {
if (areCoordsEqual(targetCoord, coord)) {
if (moveRequestCoord) {
if (areCoordsEqual(moveRequestCoord, coord)) {
room.visual.text('T', coord.x, coord.y)
potentialCost -= 1
}
Expand Down Expand Up @@ -391,6 +390,7 @@ export class CreepMoveProcs {

if (room.coordHasStructureTypes(coord, impassibleStructureTypesSet)) continue

// Going to need to revisit this one
if (
creepMemory[CreepMemoryKeys.rampartOnlyShoving] &&
!room.findStructureAtCoord(
Expand Down Expand Up @@ -424,11 +424,8 @@ export class CreepMoveProcs {

const creepInWay = Game.creeps[creepInWayName] || Game.powerCreeps[creepInWayName]
if (creepInWay) {
let creepInWayTargetCoord: Coord | undefined = creepInWay.moveRequest
? unpackCoord(creepInWay.moveRequest)
: /* creepInWay.actionCoord */ undefined

if (creepInWayTargetCoord && areCoordsEqual(coord, creepInWayTargetCoord)) {
if (creepInWay.moveRequest === packedCoord) {
potentialCost += 1
}

Expand Down Expand Up @@ -501,7 +498,7 @@ export class CreepMoveProcs {
return moveOptions
}

if (targetCoord && getRange(creep.pos, targetCoord) <= 1) {
if (targetCoord) {
if (areCoordsEqual(targetCoord, creep.pos)) {
return moveOptions
}
Expand All @@ -513,6 +510,7 @@ export class CreepMoveProcs {

// Consider sorting by range to action coord, where closer is more preferred
moveOptions.push(...findAdjacentCoordsToCoord(creep.pos))
Utils.shuffleArray(moveOptions)

creep.moveOptions = moveOptions
return moveOptions
Expand Down
19 changes: 15 additions & 4 deletions src/room/roomFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,22 @@ Room.prototype.targetVisual = function (
opacity: 0.3,
})

this.visual.circle(coord2.x, coord2.y, {
opacity: 0.3,
fill: '',
const x1 = coord1.x
const y1 = coord1.y
const x2 = coord2.x
const y2 = coord2.y
const point1x = (x1 + 3 * x2) / 4 + (y1 - y2) / 8
const point1y = (y1 + 3 * y2) / 4 + (x2 - x1) / 8
const point1: [number, number] = [point1x, point1y]
const point2x = (x1 + 3 * x2) / 4 + (-y1 + y2) / 8
const point2y = (y1 + 3 * y2) / 4 + (-x2 + x1) / 8
const point2: [number, number] = [point2x, point2y]

this.visual.poly([[coord2.x, coord2.y], point1, point2, [coord2.x, coord2.y]], {
fill: customColors.green,
stroke: customColors.green,
radius: 0.1,
opacity: 0.5,
strokeWidth: 0.05,
})
}

Expand Down
12 changes: 12 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,16 @@ export class Utils {

return Math.ceil(rawCost / combinedCost) * combinedCost
}

/**
* Shuffles an array in place
*/
static shuffleArray<T>(array: T[]) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

0 comments on commit 9ea5481

Please sign in to comment.