KungFuClan / KungFuScreeps

https://screepers.gitbook.io/screeps-typescript-starter/
9 stars 4 forks source link

[A] Create interface & implementation for each cost matrix (PARENT TICKET) #102

Open jakewaldrip opened 4 years ago

jakewaldrip commented 4 years ago

Caching of Cost Matrices should be done by introducing a variable at the top of each class file:

export class CostMatrixExample{
    public static cache: CostMatrixCache = {};
}
interface CostMatrixCache {
    [roomName: string]: {
              matrixName: { cm: CostMatrix, expireTime: number }
     }
}

The expire time on a CostMatrixCache will signify the maximum Game.time that a matrix can be used. Once the Game.time is greater than the expireTime, the matrix is invalid and should be regenerated.

export class CostMatrixExample{
    Get(params): CostMatrix {
        if(params.ignoreCreep) { // for the sake of example, the only options are ignoreCreep and ignoreStructure
             return this.ExampleMatrix_ignoreCreep(params.roomName);
        }
    }

    ExampleMatrix_ignoreCreep(roomName: string): CostMatrix {
        if(cache[roomName] !== undefined &&
           cache[roomName].ignoreCreep !== undefined &&
           cache[roomname].ignoreCreep.expireTime >= Game.time()) {
             return cache[roomName].ignoreCreep.cm;
          }
         // else generate the matrix and store it into the cache structure,
         // setting the expireTime = Game.time() + #ticks to cache the matrix
        return matrix;
    }
}

PARENT TICKET, Closed once all child tickets are closed