Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
RiftLurker authored Dec 14, 2017
2 parents 5cfa531 + a48b5df commit ac25f6a
Show file tree
Hide file tree
Showing 47 changed files with 845 additions and 395 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@screeps/engine",
"version": "2.1.1",
"version": "2.11.0",
"bin": {
"screeps-engine-main": "dist/main.js",
"screeps-engine-runner": "dist/runner.js",
Expand All @@ -13,15 +13,15 @@
"bulk-require": "^0.2.1"
},
"devDependencies": {
"babel-plugin-transform-es2015-destructuring": "^6.0.2",
"babel-plugin-transform-strict-mode": "^6.0.2",
"babel-preset-es2015": "^6.0.15",
"gulp": "^3.8.8",
"gulp-babel": "^6.0.0",
"gulp-plumber": "^0.6.6",
"gulp-sourcemaps": "^1.3.0",
"gulp-traceur": "^0.13.0",
"gulp-watch": "^1.1.0"
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"babel-plugin-transform-strict-mode": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-plumber": "^1.1.0",
"gulp-sourcemaps": "^2.6.0",
"gulp-traceur": "^0.17.2",
"gulp-watch": "^4.3.11"
},
"license": "ISC",
"author": "Artem Chivchalov <[email protected]>",
Expand Down
35 changes: 34 additions & 1 deletion src/game/console.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var _ = require('lodash'),
messages = {},
commandResults = {};
commandResults = {},
visual = {};

exports.makeConsole = function(id, sandboxedFunctionWrapper) {
messages[id] = [];
commandResults[id] = [];
visual[id] = {};
return Object.create(null, {
log: {
writable: true,
Expand All @@ -30,6 +32,31 @@ exports.makeConsole = function(id, sandboxedFunctionWrapper) {
}
commandResults[id].push(String(message));
})
},
addVisual: {
value: sandboxedFunctionWrapper(function(roomName, data) {
roomName = roomName || "";
visual[id][roomName] = visual[id][roomName] || "";
if(visual[id][roomName].length > 500*1024) {
throw new Error(`RoomVisual size in room ${roomName} has exceeded 500 KB limit`);
}
visual[id][roomName] += JSON.stringify(data)+"\n";
})
},
getVisualSize: {
value: sandboxedFunctionWrapper(function(roomName) {
roomName = roomName || "";
if(!visual[id][roomName]) {
return 0;
}
return visual[id][roomName].length;
})
},
clearVisual: {
value: sandboxedFunctionWrapper(function(roomName) {
roomName = roomName || "";
visual[id][roomName] = "";
})
}
});
};
Expand All @@ -44,4 +71,10 @@ exports.getCommandResults = function(id) {
var result = commandResults[id];
commandResults[id] = [];
return result;
};

exports.getVisual = function(id) {
var result = visual[id];
visual[id] = [];
return result;
};
2 changes: 1 addition & 1 deletion src/game/construction-sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(!this.my && !(this.room && this.room.controller && this.room.controller.my)) {
return C.ERR_NOT_OWNER;
}
intents.set(this.id, 'remove', {});
intents.pushByName('room', 'removeConstructionSite', {roomName: data(this.id).room, id: this.id});
return C.OK;
});

Expand Down
97 changes: 68 additions & 29 deletions src/game/creeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ var utils = require('./../utils'),

var runtimeData, intents, register, globals, controllersClaimedInTick;

function _getActiveBodyparts(body, type) {
var count = 0;
for(var i = body.length-1; i>=0; i--) {
if (body[i].hits <= 0)
break;
if (body[i].type === type)
count++;
}
return count;
}

function _hasActiveBodypart(body, type) {
for(var i = body.length-1; i>=0; i--) {
if (body[i].hits <= 0)
break;
if (body[i].type === type)
return true;
}
return false;
}

exports.make = function(_runtimeData, _intents, _register, _globals) {

runtimeData = _runtimeData;
Expand Down Expand Up @@ -116,7 +137,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(data(this.id).fatigue > 0) {
return C.ERR_TIRED;
}
if(this.getActiveBodyparts(C.MOVE) == 0) {
if(!_hasActiveBodypart(this.body, C.MOVE)) {
return C.ERR_NO_BODYPART;
}
direction = +direction;
Expand All @@ -129,16 +150,23 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {

Creep.prototype.moveTo = register.wrapFn(function(firstArg, secondArg, opts) {

var visualized = false;

if(!this.my) {
return C.ERR_NOT_OWNER;
}
if(this.spawning) {
return C.ERR_BUSY;
}
if(data(this.id).fatigue > 0) {
if(_.isObject(firstArg)) {
opts = _.clone(secondArg);
}
opts = opts || {};

if(data(this.id).fatigue > 0 && (!opts || !opts.visualizePathStyle)) {
return C.ERR_TIRED;
}
if(this.getActiveBodyparts(C.MOVE) == 0) {
if(!_hasActiveBodypart(this.body, C.MOVE)) {
return C.ERR_NO_BODYPART;
}

Expand All @@ -151,18 +179,17 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {

var targetPos = new globals.RoomPosition(x,y,roomName);

if(_.isObject(firstArg)) {
opts = _.clone(secondArg);
}
opts = opts || {};

if(_.isUndefined(opts.reusePath)) {
opts.reusePath = 5;
}
if(_.isUndefined(opts.serializeMemory)) {
opts.serializeMemory = true;
}

if(opts.visualizePathStyle) {
_.defaults(opts.visualizePathStyle, {fill: 'transparent', stroke: '#fff', lineStyle: 'dashed', strokeWidth: .15, opacity: .1});
}

if(x == this.pos.x && y == this.pos.y && roomName == this.pos.roomName) {
return C.OK;
}
Expand Down Expand Up @@ -230,6 +257,10 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(path.length == 0) {
return this.pos.isNearTo(targetPos) ? C.OK : C.ERR_NO_PATH;
}
if(opts.visualizePathStyle) {
this.room.visual.poly(path, opts.visualizePathStyle);
visualized = true;
}
var result = this.moveByPath(path);

if(result == C.OK) {
Expand All @@ -256,8 +287,12 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(path.length == 0) {
return C.ERR_NO_PATH;
}
this.move(path[0].direction);
return C.OK;

if(opts.visualizePathStyle && !visualized) {
this.room.visual.poly(path, opts.visualizePathStyle);
}

return this.move(path[0].direction);
});

Creep.prototype.moveByPath = register.wrapFn(function(path) {
Expand Down Expand Up @@ -298,7 +333,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.WORK) == 0) {
if(!_hasActiveBodypart(this.body, C.WORK)) {
return C.ERR_NO_BODYPART;
}
if(!target || !target.id) {
Expand Down Expand Up @@ -589,7 +624,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(!amount) {
amount = Math.min( data(target.id)[resourceType], emptySpace );
}
if(data(target.id)[resourceType] || data(target.id)[resourceType] < amount) {
if(!data(target.id)[resourceType] || data(target.id)[resourceType] < amount) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
if(amount > emptySpace) {
Expand Down Expand Up @@ -656,7 +691,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
});

Creep.prototype.getActiveBodyparts = register.wrapFn(function(type) {
return _.filter(this.body, (i) => i.hits > 0 && i.type == type).length;
return _getActiveBodyparts(this.body, type);
});

Creep.prototype.attack = register.wrapFn(function(target) {
Expand All @@ -667,7 +702,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.ATTACK) == 0) {
if(!_hasActiveBodypart(this.body, C.ATTACK)) {
return C.ERR_NO_BODYPART;
}
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
Expand Down Expand Up @@ -695,7 +730,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.RANGED_ATTACK) == 0) {
if(!_hasActiveBodypart(this.body, C.RANGED_ATTACK)) {
return C.ERR_NO_BODYPART;
}
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
Expand Down Expand Up @@ -723,7 +758,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.RANGED_ATTACK) == 0) {
if(!_hasActiveBodypart(this.body, C.RANGED_ATTACK)) {
return C.ERR_NO_BODYPART;
}
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
Expand All @@ -743,7 +778,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.HEAL) == 0) {
if(!_hasActiveBodypart(this.body, C.HEAL)) {
return C.ERR_NO_BODYPART;
}
if(!target || !target.id || !register.creeps[target.id] || !(target instanceof globals.Creep)) {
Expand All @@ -770,7 +805,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.HEAL) == 0) {
if(!_hasActiveBodypart(this.body, C.HEAL)) {
return C.ERR_NO_BODYPART;
}
if(!target || !target.id || !register.creeps[target.id] || !(target instanceof globals.Creep)) {
Expand All @@ -797,7 +832,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.WORK) == 0) {
if(!_hasActiveBodypart(this.body, C.WORK)) {
return C.ERR_NO_BODYPART;
}
if(!this.carry.energy) {
Expand Down Expand Up @@ -825,7 +860,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.WORK) == 0) {
if(!_hasActiveBodypart(this.body, C.WORK)) {
return C.ERR_NO_BODYPART;
}
if(!this.carry.energy) {
Expand All @@ -838,12 +873,12 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(!this.pos.inRangeTo(target, C.RANGE_BUILD)) {
return C.ERR_NOT_IN_RANGE;
}
if(_.contains(['spawn','extension','constructedWall'], target.structureType) &&
if(_.contains(C.OBSTACLE_OBJECT_TYPES, target.structureType) &&
_.any(register.objectsByRoom[data(this.id).room], (i) => i.x == target.pos.x && i.y == target.pos.y && _.contains(C.OBSTACLE_OBJECT_TYPES, i.type))) {
return C.ERR_INVALID_TARGET;
}

var buildPower = this.getActiveBodyparts(C.WORK) * C.BUILD_POWER,
var buildPower = _getActiveBodyparts(this.body, C.WORK) * C.BUILD_POWER,
buildRemaining = target.progressTotal - target.progress,
buildEffect = Math.min(buildPower, buildRemaining, this.carry.energy);

Expand Down Expand Up @@ -892,8 +927,9 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
return C.ERR_BUSY;
}

var controllersClaimed = _.filter(runtimeData.userObjects, {type: 'controller'}).length + controllersClaimedInTick;
if (controllersClaimed && (!runtimeData.user.gcl || runtimeData.user.gcl < C.GCL_MULTIPLY * Math.pow(controllersClaimed, C.GCL_POW))) {
var controllersClaimed = runtimeData.user.rooms.length + controllersClaimedInTick;
if (controllersClaimed &&
(!runtimeData.user.gcl || runtimeData.user.gcl < utils.calcNeededGcl(controllersClaimed + 1))) {
return C.ERR_GCL_NOT_ENOUGH;
}
if (controllersClaimed >= C.GCL_NOVICE && runtimeData.rooms[this.room.name].novice > Date.now()) {
Expand All @@ -903,7 +939,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
register.assertTargetObject(target);
return C.ERR_INVALID_TARGET;
}
if(this.getActiveBodyparts(C.CLAIM) == 0) {
if(!_hasActiveBodypart(this.body, C.CLAIM)) {
return C.ERR_NO_BODYPART;
}
if(!target.pos.inRangeTo(this.pos, C.RANGE_CLAIM_CONTROLLER)) {
Expand Down Expand Up @@ -939,7 +975,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
register.assertTargetObject(target);
return C.ERR_INVALID_TARGET;
}
if(this.getActiveBodyparts(C.CLAIM) < 5) {
if(!_getActiveBodyparts(this.body, C.CLAIM)) {
return C.ERR_NO_BODYPART;
}
if(!target.pos.inRangeTo(this.pos, C.RANGE_ATTACK_CONTROLLER)) {
Expand All @@ -948,6 +984,9 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(!target.owner && !target.reservation) {
return C.ERR_INVALID_TARGET;
}
if(data(target.id).upgradeBlocked > runtimeData.time) {
return C.ERR_TIRED;
}
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
return C.ERR_NO_BODYPART;
}
Expand All @@ -964,7 +1003,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.WORK) == 0) {
if(!_hasActiveBodypart(this.body, C.WORK)) {
return C.ERR_NO_BODYPART;
}
if(!this.carry.energy) {
Expand Down Expand Up @@ -1016,7 +1055,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(target.reservation && target.reservation.username != runtimeData.user.username) {
return C.ERR_INVALID_TARGET;
}
if(!this.getActiveBodyparts(C.CLAIM)) {
if(!_hasActiveBodypart(this.body, C.CLAIM)) {
return C.ERR_NO_BODYPART;
}

Expand Down Expand Up @@ -1061,7 +1100,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(this.spawning) {
return C.ERR_BUSY;
}
if(this.getActiveBodyparts(C.WORK) == 0) {
if(!_hasActiveBodypart(this.body, C.WORK)) {
return C.ERR_NO_BODYPART;
}
if(!target || !target.id || !register.structures[target.id] ||
Expand Down
Loading

0 comments on commit ac25f6a

Please sign in to comment.