npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

screeps-cache

v1.0.12

Published

A generalized solution for heap and memory caching in Screeps

Downloads

19

Readme

screeps-cache

A generalized solution for heap and memory caching in Screeps.

Definitions

  • Screeps: an MMO strategy sandbox game for programmers
  • Memory: A global object provided by Screeps to store data that is persisted across ticks and global resets.
  • Heap: A general term for data stored outside the scope of the main loop. This persists across ticks, but disappears when the code is reloaded in a global reset.

Rationale

We often want to cache data about the world state, either to preserve it if vision to a room is lost or to reduce expensive processing by reusing a saved value. There are many different ways to approach this.

Primary goals for this approach are:

  1. Facilitate a central store for data. We should be able to get all our data (regardless of whether it is cached in Memory or heap) from a single source.
  2. Reduce code reuse. Implementing caching should take a minimum amount of boilerplate.

Usage

This is an example of caching data about a game object:

class CachedContainer {
    constructor(public id: Id<StructureContainer>) { }

    @memoryCacheGetter(keyById, (i: CachedContainer) => Game.getObjectById(i.id)?.pos)
    public pos?: RoomPosition

    @memoryCacheGetter(keyById, (i: CachedContainer) => Game.getObjectById(i.id)?.structureType)
    public structureType?: StructureConstant

    @heapCacheGetter((i: CachedContainer) => Game.getObjectById(i.id)?.hits)
    public hits?: number

    @heapCacheGetter((i: CachedContainer) => Game.getObjectById(i.id)?.hitsMax)
    public hitsMax?: number

    @memoryCache(keyById)
    public isSource?: boolean
}

// Create a list of visible containers
let container = Object.values(Game.structures)
            .find(s => structureType === STRUCTURE_CONTAINER);
let cachedContainer = new CachedContainer(container.id);

// Access cached properties, whether container is now visible or not
console.log(cachedContainer.hits);
// Set custom cached properties
cachedContainer.isSource = true;

This doesn't need to map to an actual game object. You can create your own data objects, as long as the ID doesn't overlap with an actual game object:

class Franchise {
    public id = nanoid();
    constructor(source: Source, container?: StructureContainer) {
        this.sourceId = source.id;
        this.containerId = container?.id;
    }

    @memoryCache(keyById)
    public sourceId?: Id<Source>

    @memoryCache(keyById)
    public containerId?: Id<Source>
}

Architecture Design

Duplication may cause issues here. If you create two CachedContainer instances for the same ID, the properties cached in global Memory will be shared, but anything cached in heap will not.

As a corollary to this, the heap cache will only work if the objects are created in heap scope: if you re-create the objects in each tick of your main loop, the Memory will still persist, but the heap cache will be useless.

By default, the cached properties will only be updated when you get them. If you want to refresh all of the cached properties, you can do something like for (let i in cachedContainer) {} to hit them all.

This uses Memory.cache to store cached data. If you're already using that key for something else, it may cause unpredictable problems: beware.

The specific structure and the data you want to capture will vary depending on the rest of your implementation.

Practical Application

I wrote an article on how I'm integrating this into my own Screeps AI.