Skip to content

Commit ac25f6a

Browse files
authored
Merge branch 'master' into master
2 parents 5cfa531 + a48b5df commit ac25f6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+845
-395
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@screeps/engine",
3-
"version": "2.1.1",
3+
"version": "2.11.0",
44
"bin": {
55
"screeps-engine-main": "dist/main.js",
66
"screeps-engine-runner": "dist/runner.js",
@@ -13,15 +13,15 @@
1313
"bulk-require": "^0.2.1"
1414
},
1515
"devDependencies": {
16-
"babel-plugin-transform-es2015-destructuring": "^6.0.2",
17-
"babel-plugin-transform-strict-mode": "^6.0.2",
18-
"babel-preset-es2015": "^6.0.15",
19-
"gulp": "^3.8.8",
20-
"gulp-babel": "^6.0.0",
21-
"gulp-plumber": "^0.6.6",
22-
"gulp-sourcemaps": "^1.3.0",
23-
"gulp-traceur": "^0.13.0",
24-
"gulp-watch": "^1.1.0"
16+
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
17+
"babel-plugin-transform-strict-mode": "^6.24.1",
18+
"babel-preset-es2015": "^6.24.1",
19+
"gulp": "^3.9.1",
20+
"gulp-babel": "^6.1.2",
21+
"gulp-plumber": "^1.1.0",
22+
"gulp-sourcemaps": "^2.6.0",
23+
"gulp-traceur": "^0.17.2",
24+
"gulp-watch": "^4.3.11"
2525
},
2626
"license": "ISC",
2727
"author": "Artem Chivchalov <[email protected]>",

src/game/console.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var _ = require('lodash'),
22
messages = {},
3-
commandResults = {};
3+
commandResults = {},
4+
visual = {};
45

56
exports.makeConsole = function(id, sandboxedFunctionWrapper) {
67
messages[id] = [];
78
commandResults[id] = [];
9+
visual[id] = {};
810
return Object.create(null, {
911
log: {
1012
writable: true,
@@ -30,6 +32,31 @@ exports.makeConsole = function(id, sandboxedFunctionWrapper) {
3032
}
3133
commandResults[id].push(String(message));
3234
})
35+
},
36+
addVisual: {
37+
value: sandboxedFunctionWrapper(function(roomName, data) {
38+
roomName = roomName || "";
39+
visual[id][roomName] = visual[id][roomName] || "";
40+
if(visual[id][roomName].length > 500*1024) {
41+
throw new Error(`RoomVisual size in room ${roomName} has exceeded 500 KB limit`);
42+
}
43+
visual[id][roomName] += JSON.stringify(data)+"\n";
44+
})
45+
},
46+
getVisualSize: {
47+
value: sandboxedFunctionWrapper(function(roomName) {
48+
roomName = roomName || "";
49+
if(!visual[id][roomName]) {
50+
return 0;
51+
}
52+
return visual[id][roomName].length;
53+
})
54+
},
55+
clearVisual: {
56+
value: sandboxedFunctionWrapper(function(roomName) {
57+
roomName = roomName || "";
58+
visual[id][roomName] = "";
59+
})
3360
}
3461
});
3562
};
@@ -44,4 +71,10 @@ exports.getCommandResults = function(id) {
4471
var result = commandResults[id];
4572
commandResults[id] = [];
4673
return result;
74+
};
75+
76+
exports.getVisual = function(id) {
77+
var result = visual[id];
78+
visual[id] = [];
79+
return result;
4780
};

src/game/construction-sites.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
5252
if(!this.my && !(this.room && this.room.controller && this.room.controller.my)) {
5353
return C.ERR_NOT_OWNER;
5454
}
55-
intents.set(this.id, 'remove', {});
55+
intents.pushByName('room', 'removeConstructionSite', {roomName: data(this.id).room, id: this.id});
5656
return C.OK;
5757
});
5858

src/game/creeps.js

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ var utils = require('./../utils'),
66

77
var runtimeData, intents, register, globals, controllersClaimedInTick;
88

9+
function _getActiveBodyparts(body, type) {
10+
var count = 0;
11+
for(var i = body.length-1; i>=0; i--) {
12+
if (body[i].hits <= 0)
13+
break;
14+
if (body[i].type === type)
15+
count++;
16+
}
17+
return count;
18+
}
19+
20+
function _hasActiveBodypart(body, type) {
21+
for(var i = body.length-1; i>=0; i--) {
22+
if (body[i].hits <= 0)
23+
break;
24+
if (body[i].type === type)
25+
return true;
26+
}
27+
return false;
28+
}
29+
930
exports.make = function(_runtimeData, _intents, _register, _globals) {
1031

1132
runtimeData = _runtimeData;
@@ -116,7 +137,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
116137
if(data(this.id).fatigue > 0) {
117138
return C.ERR_TIRED;
118139
}
119-
if(this.getActiveBodyparts(C.MOVE) == 0) {
140+
if(!_hasActiveBodypart(this.body, C.MOVE)) {
120141
return C.ERR_NO_BODYPART;
121142
}
122143
direction = +direction;
@@ -129,16 +150,23 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
129150

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

153+
var visualized = false;
154+
132155
if(!this.my) {
133156
return C.ERR_NOT_OWNER;
134157
}
135158
if(this.spawning) {
136159
return C.ERR_BUSY;
137160
}
138-
if(data(this.id).fatigue > 0) {
161+
if(_.isObject(firstArg)) {
162+
opts = _.clone(secondArg);
163+
}
164+
opts = opts || {};
165+
166+
if(data(this.id).fatigue > 0 && (!opts || !opts.visualizePathStyle)) {
139167
return C.ERR_TIRED;
140168
}
141-
if(this.getActiveBodyparts(C.MOVE) == 0) {
169+
if(!_hasActiveBodypart(this.body, C.MOVE)) {
142170
return C.ERR_NO_BODYPART;
143171
}
144172

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

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

154-
if(_.isObject(firstArg)) {
155-
opts = _.clone(secondArg);
156-
}
157-
opts = opts || {};
158-
159182
if(_.isUndefined(opts.reusePath)) {
160183
opts.reusePath = 5;
161184
}
162185
if(_.isUndefined(opts.serializeMemory)) {
163186
opts.serializeMemory = true;
164187
}
165188

189+
if(opts.visualizePathStyle) {
190+
_.defaults(opts.visualizePathStyle, {fill: 'transparent', stroke: '#fff', lineStyle: 'dashed', strokeWidth: .15, opacity: .1});
191+
}
192+
166193
if(x == this.pos.x && y == this.pos.y && roomName == this.pos.roomName) {
167194
return C.OK;
168195
}
@@ -230,6 +257,10 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
230257
if(path.length == 0) {
231258
return this.pos.isNearTo(targetPos) ? C.OK : C.ERR_NO_PATH;
232259
}
260+
if(opts.visualizePathStyle) {
261+
this.room.visual.poly(path, opts.visualizePathStyle);
262+
visualized = true;
263+
}
233264
var result = this.moveByPath(path);
234265

235266
if(result == C.OK) {
@@ -256,8 +287,12 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
256287
if(path.length == 0) {
257288
return C.ERR_NO_PATH;
258289
}
259-
this.move(path[0].direction);
260-
return C.OK;
290+
291+
if(opts.visualizePathStyle && !visualized) {
292+
this.room.visual.poly(path, opts.visualizePathStyle);
293+
}
294+
295+
return this.move(path[0].direction);
261296
});
262297

263298
Creep.prototype.moveByPath = register.wrapFn(function(path) {
@@ -298,7 +333,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
298333
if(this.spawning) {
299334
return C.ERR_BUSY;
300335
}
301-
if(this.getActiveBodyparts(C.WORK) == 0) {
336+
if(!_hasActiveBodypart(this.body, C.WORK)) {
302337
return C.ERR_NO_BODYPART;
303338
}
304339
if(!target || !target.id) {
@@ -589,7 +624,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
589624
if(!amount) {
590625
amount = Math.min( data(target.id)[resourceType], emptySpace );
591626
}
592-
if(data(target.id)[resourceType] || data(target.id)[resourceType] < amount) {
627+
if(!data(target.id)[resourceType] || data(target.id)[resourceType] < amount) {
593628
return C.ERR_NOT_ENOUGH_RESOURCES;
594629
}
595630
if(amount > emptySpace) {
@@ -656,7 +691,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
656691
});
657692

658693
Creep.prototype.getActiveBodyparts = register.wrapFn(function(type) {
659-
return _.filter(this.body, (i) => i.hits > 0 && i.type == type).length;
694+
return _getActiveBodyparts(this.body, type);
660695
});
661696

662697
Creep.prototype.attack = register.wrapFn(function(target) {
@@ -667,7 +702,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
667702
if(this.spawning) {
668703
return C.ERR_BUSY;
669704
}
670-
if(this.getActiveBodyparts(C.ATTACK) == 0) {
705+
if(!_hasActiveBodypart(this.body, C.ATTACK)) {
671706
return C.ERR_NO_BODYPART;
672707
}
673708
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
@@ -695,7 +730,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
695730
if(this.spawning) {
696731
return C.ERR_BUSY;
697732
}
698-
if(this.getActiveBodyparts(C.RANGED_ATTACK) == 0) {
733+
if(!_hasActiveBodypart(this.body, C.RANGED_ATTACK)) {
699734
return C.ERR_NO_BODYPART;
700735
}
701736
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
@@ -723,7 +758,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
723758
if(this.spawning) {
724759
return C.ERR_BUSY;
725760
}
726-
if(this.getActiveBodyparts(C.RANGED_ATTACK) == 0) {
761+
if(!_hasActiveBodypart(this.body, C.RANGED_ATTACK)) {
727762
return C.ERR_NO_BODYPART;
728763
}
729764
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
@@ -743,7 +778,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
743778
if(this.spawning) {
744779
return C.ERR_BUSY;
745780
}
746-
if(this.getActiveBodyparts(C.HEAL) == 0) {
781+
if(!_hasActiveBodypart(this.body, C.HEAL)) {
747782
return C.ERR_NO_BODYPART;
748783
}
749784
if(!target || !target.id || !register.creeps[target.id] || !(target instanceof globals.Creep)) {
@@ -770,7 +805,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
770805
if(this.spawning) {
771806
return C.ERR_BUSY;
772807
}
773-
if(this.getActiveBodyparts(C.HEAL) == 0) {
808+
if(!_hasActiveBodypart(this.body, C.HEAL)) {
774809
return C.ERR_NO_BODYPART;
775810
}
776811
if(!target || !target.id || !register.creeps[target.id] || !(target instanceof globals.Creep)) {
@@ -797,7 +832,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
797832
if(this.spawning) {
798833
return C.ERR_BUSY;
799834
}
800-
if(this.getActiveBodyparts(C.WORK) == 0) {
835+
if(!_hasActiveBodypart(this.body, C.WORK)) {
801836
return C.ERR_NO_BODYPART;
802837
}
803838
if(!this.carry.energy) {
@@ -825,7 +860,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
825860
if(this.spawning) {
826861
return C.ERR_BUSY;
827862
}
828-
if(this.getActiveBodyparts(C.WORK) == 0) {
863+
if(!_hasActiveBodypart(this.body, C.WORK)) {
829864
return C.ERR_NO_BODYPART;
830865
}
831866
if(!this.carry.energy) {
@@ -838,12 +873,12 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
838873
if(!this.pos.inRangeTo(target, C.RANGE_BUILD)) {
839874
return C.ERR_NOT_IN_RANGE;
840875
}
841-
if(_.contains(['spawn','extension','constructedWall'], target.structureType) &&
876+
if(_.contains(C.OBSTACLE_OBJECT_TYPES, target.structureType) &&
842877
_.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))) {
843878
return C.ERR_INVALID_TARGET;
844879
}
845880

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

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

895-
var controllersClaimed = _.filter(runtimeData.userObjects, {type: 'controller'}).length + controllersClaimedInTick;
896-
if (controllersClaimed && (!runtimeData.user.gcl || runtimeData.user.gcl < C.GCL_MULTIPLY * Math.pow(controllersClaimed, C.GCL_POW))) {
930+
var controllersClaimed = runtimeData.user.rooms.length + controllersClaimedInTick;
931+
if (controllersClaimed &&
932+
(!runtimeData.user.gcl || runtimeData.user.gcl < utils.calcNeededGcl(controllersClaimed + 1))) {
897933
return C.ERR_GCL_NOT_ENOUGH;
898934
}
899935
if (controllersClaimed >= C.GCL_NOVICE && runtimeData.rooms[this.room.name].novice > Date.now()) {
@@ -903,7 +939,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
903939
register.assertTargetObject(target);
904940
return C.ERR_INVALID_TARGET;
905941
}
906-
if(this.getActiveBodyparts(C.CLAIM) == 0) {
942+
if(!_hasActiveBodypart(this.body, C.CLAIM)) {
907943
return C.ERR_NO_BODYPART;
908944
}
909945
if(!target.pos.inRangeTo(this.pos, C.RANGE_CLAIM_CONTROLLER)) {
@@ -939,7 +975,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
939975
register.assertTargetObject(target);
940976
return C.ERR_INVALID_TARGET;
941977
}
942-
if(this.getActiveBodyparts(C.CLAIM) < 5) {
978+
if(!_getActiveBodyparts(this.body, C.CLAIM)) {
943979
return C.ERR_NO_BODYPART;
944980
}
945981
if(!target.pos.inRangeTo(this.pos, C.RANGE_ATTACK_CONTROLLER)) {
@@ -948,6 +984,9 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
948984
if(!target.owner && !target.reservation) {
949985
return C.ERR_INVALID_TARGET;
950986
}
987+
if(data(target.id).upgradeBlocked > runtimeData.time) {
988+
return C.ERR_TIRED;
989+
}
951990
if(this.room.controller && !this.room.controller.my && this.room.controller.safeMode) {
952991
return C.ERR_NO_BODYPART;
953992
}
@@ -964,7 +1003,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
9641003
if(this.spawning) {
9651004
return C.ERR_BUSY;
9661005
}
967-
if(this.getActiveBodyparts(C.WORK) == 0) {
1006+
if(!_hasActiveBodypart(this.body, C.WORK)) {
9681007
return C.ERR_NO_BODYPART;
9691008
}
9701009
if(!this.carry.energy) {
@@ -1016,7 +1055,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
10161055
if(target.reservation && target.reservation.username != runtimeData.user.username) {
10171056
return C.ERR_INVALID_TARGET;
10181057
}
1019-
if(!this.getActiveBodyparts(C.CLAIM)) {
1058+
if(!_hasActiveBodypart(this.body, C.CLAIM)) {
10201059
return C.ERR_NO_BODYPART;
10211060
}
10221061

@@ -1061,7 +1100,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
10611100
if(this.spawning) {
10621101
return C.ERR_BUSY;
10631102
}
1064-
if(this.getActiveBodyparts(C.WORK) == 0) {
1103+
if(!_hasActiveBodypart(this.body, C.WORK)) {
10651104
return C.ERR_NO_BODYPART;
10661105
}
10671106
if(!target || !target.id || !register.structures[target.id] ||

0 commit comments

Comments
 (0)