Skip to content

Commit

Permalink
Make memory property configurable
Browse files Browse the repository at this point in the history
This allows actually using the custom serialization explained at
http://support.screeps.com/hc/en-us/articles/203016642-Working-with-memory

Right now `Creep#memory`, `Flag#memory`, `Room#memory` and `Spawn#Memory` always
access `globals.Memory` which is not changeable from inside users code.
Therefor Memory parsing will happen twice, once by the users code and
once by the first access to `globals.Memory`, resulting in two different
objects.

The solution to this would be tracking down why setting `Memory` (or
`global.Memory`) in code doesn't change `globals.Memory`, but I don't
have enough experience with the screeps server and the vm module.

For now this just makes the `memory` property configurable, so we can
change it to actually access our version of the parsed Memory.

Test case:
```
module.exports.loop = function loop() {
    Memory = JSON.parse(RawMemory.get());

    const creepName = "test";
    const creep = Game.creeps[creepName];

    if (!creep) {
        _.first(_.values(Game.spawns)).createCreep([MOVE], creepName);
    } else {
        console.log(`--- Test case (tick: ${Game.time}) ---`);
        Memory.creeps[creepName].tick = Game.time;

        const gMemory = Memory.creeps[creepName].tick;
        const cMemory = creep.memory.tick;

        if (globalMemoryTick === creepMemoryTick) {
            console.log(`success: ${gMemory} === ${cMemory}`);
        } else {
            console.log(`failed: ${gMemory} !== ${cMemory}`);
        }
    }

    RawMemory.set(JSON.stringify(Memory));
}
```
  • Loading branch information
RiftLurker authored and Leo Friedrichs committed Feb 3, 2017
1 parent 7df5e3f commit e8c9365
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/game/creeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
throw new Error('Could not set creep memory');
}
globals.Memory.creeps[this.name] = value;
}
},
configurable: true
});

Creep.prototype.toString = register.wrapFn(function() {
Expand Down
3 changes: 2 additions & 1 deletion src/game/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
throw new Error('Could not set flag memory');
}
globals.Memory.flags[this.name] = value;
}
},
configurable: true
});

Flag.prototype.toString = register.wrapFn(function() {
Expand Down
3 changes: 2 additions & 1 deletion src/game/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
throw new Error('Could not set room memory');
}
globals.Memory.rooms[this.name] = value;
}
},
configurable: true
});

Room.prototype.find = register.wrapFn(function(type, opts) {
Expand Down
3 changes: 2 additions & 1 deletion src/game/structures.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
throw new Error('Could not set spawn memory');
}
globals.Memory.spawns[data(this.id).name] = value;
}
},
configurable: true
});

StructureSpawn.prototype.toString = register.wrapFn(function() {
Expand Down

0 comments on commit e8c9365

Please sign in to comment.