Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tombstone implementation #74

Merged
merged 21 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
54d1039
Fix simultaneous execution of `attackController` (#70)
RiftLurker Jan 1, 2018
e9aaba6
feat(runtime): isolated-vm integration
artch Jan 9, 2018
66f1714
feat(runtime): isolated-vm integration
artch Jan 17, 2018
ee3e9a2
feat(runtime): isolated-vm integration
artch Jan 18, 2018
5e5aea3
feat(runtime): isolated-vm integration
artch Jan 18, 2018
89bbcab
feat(runtime): isolated-vm integration
artch Jan 23, 2018
b997a3d
Tombstone implementation
AlinaNova21 Jan 23, 2018
b78f101
Fixed new creeps auto-killing on blcoked spawns and recylcing
AlinaNova21 Jan 23, 2018
ef1b319
Drop to container
AlinaNova21 Jan 24, 2018
63673b2
Adjust tombstones to use a stub creep
AlinaNova21 Jan 24, 2018
17b65a6
Only store body types, removed duplicate creepBody, fix TTL saving
AlinaNova21 Jan 24, 2018
55cce7c
Fixed syntax error and hitsMax
AlinaNova21 Jan 24, 2018
6e552a3
Fixed containers being overfilled
AlinaNova21 Jan 24, 2018
a8a4bc4
Adjusted so that any energy that won't fit into container (if it exis…
AlinaNova21 Jan 24, 2018
dc951b0
Fix Tombstones to never drop into containers when decayed.
AlinaNova21 Jan 25, 2018
55838d0
Fix saving public say to tombstone
AlinaNova21 Jan 25, 2018
95194bd
Fixed tombstone not spawning for TTL deaths
AlinaNova21 Jan 26, 2018
53cd12b
Tombstones: Don't pass stats to _die for TTL death
AlinaNova21 Jan 26, 2018
3765726
Allow creep say intent to be processed before the suicide intent
AlinaNova21 Jan 26, 2018
a1bdabe
Merge branch 'tombstone' of https://github.com/ags131/engine into ags…
artch Jan 31, 2018
63feb7c
small fixes
artch Jan 31, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/game/creeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
if(!_.contains(C.RESOURCES_ALL, resourceType)) {
return C.ERR_INVALID_ARGS;
}
if(!target || !target.id || !register.structures[target.id] || !(target instanceof globals.Structure)) {
if(!target || !target.id || ((!register.structures[target.id] || !(target instanceof globals.Structure) ) && !(target instanceof globals.Tombstone))) {
register.assertTargetObject(target);
return C.ERR_INVALID_TARGET;
}
Expand Down
7 changes: 7 additions & 0 deletions src/game/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
flags: {},
constructionSites: {},
minerals: {},
tombstones: {},
nukes: {}
});

Expand Down Expand Up @@ -190,6 +191,7 @@
require('./nukes').make(runtimeData, intents, register, globals);
require('./resources').make(runtimeData, intents, register, globals);
require('./flags').make(runtimeData, intents, register, globals);
require('./tombstones').make(runtimeData, intents, register, globals);
require('./construction-sites').make(runtimeData, intents, register, globals);
require('./path-finder').make(runtimeData, intents, register, globals);

Expand Down Expand Up @@ -308,6 +310,11 @@
addObjectToRegister(register, 'nukes', register._objects[i], object);
addObjectToFindCache(register, C.FIND_NUKES, register._objects[i], object);
}
if (object.type == 'tombstone') {
register._objects[i] = new globals.Tombstone(i);
addObjectToRegister(register, 'tombstones', register._objects[i], object);
addObjectToFindCache(register, C.FIND_TOMBSTONES, register._objects[i], object);
}

if (object.type == 'constructionSite') {
register._objects[i] = new globals.ConstructionSite(i);
Expand Down
4 changes: 4 additions & 0 deletions src/game/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
structure: register.byRoom[id].structures,
flag: register.byRoom[id].flags,
constructionSite: register.byRoom[id].constructionSites,
tombstone: register.byRoom[id].tombstones,
nuke: register.byRoom[id].nukes
},
lookTypeSpatialRegisters: {
Expand All @@ -480,6 +481,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
structure: register.byRoom[id].spatial.structures,
flag: register.byRoom[id].spatial.flags,
constructionSite: register.byRoom[id].spatial.constructionSites,
tombstone: register.byRoom[id].spatial.tombstones,
nuke: register.byRoom[id].spatial.nukes
}
};
Expand Down Expand Up @@ -734,6 +736,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
_lookSpatialRegister(this.name, C.LOOK_CONSTRUCTION_SITES, x,y, result);
_lookSpatialRegister(this.name, C.LOOK_TERRAIN, x,y, result);
_lookSpatialRegister(this.name, C.LOOK_NUKES, x,y, result);
_lookSpatialRegister(this.name, C.LOOK_TOMBSTONES, x,y, result);

return result;
});
Expand Down Expand Up @@ -771,6 +774,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
_lookAreaMixedRegister(this.name, C.LOOK_CONSTRUCTION_SITES, top, left, bottom, right, true, asArray, result);
_lookAreaMixedRegister(this.name, C.LOOK_TERRAIN, top, left, bottom, right, true, asArray, result);
_lookAreaMixedRegister(this.name, C.LOOK_NUKES, top, left, bottom, right, true, asArray, result);
_lookAreaMixedRegister(this.name, C.LOOK_TOMBSTONES, top, left, bottom, right, true, asArray, result);

return result;
});
Expand Down
150 changes: 150 additions & 0 deletions src/game/tombstones.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
var utils = require('./../utils'),
rooms = require('./rooms'),
driver = utils.getDriver(),
C = driver.constants,
_ = require('lodash');

var runtimeData, intents, register, globals;

function _storeGetter(o) {
var result = {energy: o.energy};

C.RESOURCES_ALL.forEach(resourceType => {
if (o[resourceType]) {
result[resourceType] = o[resourceType];
}
});

return result;
}

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

runtimeData = _runtimeData;
intents = _intents;
register = _register;
globals = _globals;

if(globals.Tombstone) {
return;
}

var data = (id) => {
if(!runtimeData.roomObjects[id]) {
throw new Error("Could not find an object with ID "+id);
}
return runtimeData.roomObjects[id];
};

var Tombstone = register.wrapFn(function(id) {
let self = this
var _data = data(id);
globals.RoomObject.call(this, _data.x, _data.y, _data.room);
this.id = id;
});

Tombstone.prototype = Object.create(globals.RoomObject.prototype);
Tombstone.prototype.constructor = Tombstone;
utils.defineGameObjectProperties(Tombstone.prototype, data, {
owner: (o) => _.isUndefined(o.user) || o.user === null ? undefined : {
username: runtimeData.users[o.user].username
},
my: (o) => _.isUndefined(o.user) ? undefined : o.user == runtimeData.user._id,
deathTime: (o) => o.deathTime,
store: _storeGetter,
ticksToDecay: (o) => o.decayTime - runtimeData.time,
creep: (o) => {
let creep = new globals.Creep();
globals.RoomObject.call(creep, o.x, o.y, o.room);
Object.defineProperties(creep, {
id: {
enumerable: true,
get() {
return o.creepId;
}
},
name: {
enumerable: true,
get() {
return o.creepName;
}
},
spawning: {
enumerable: true,
get() {
return false;
}
},
my: {
enumerable: true,
get() {
return o.user == runtimeData.user._id;
}
},
body: {
enumerable: true,
get() {
return _.map(o.creepBody, type => ({ type, hits: 0 }))
}
},
owner: {
enumerable: true,
get() {
return _.isUndefined(o.user) || o.user === null ? undefined : {
username: runtimeData.users[o.user].username
}
}
},
ticksToLive: {
enumerable: true,
get() {
return o.creepTicksToLive;
}
},
carryCapacity: {
enumerable: true,
get() {
return _.reduce(o.creepBody, (result, type) => result += type === C.CARRY ? C.CARRY_CAPACITY : 0, 0);
}
},
carry: {
enumerable: true,
get() {
return { energy: 0 };
}
},
fatigue: {
enumerable: true,
get() {
return 0;
}
},
hits: {
enumerable: true,
get() {
return 0;
}
},
hitsMax: {
enumerable: true,
get() {
return o.creepBody.length * 100;
}
},
saying: {
enumerable: true,
get() {
return o.creepSaying;
}
}
});
return creep;
},
});

Tombstone.prototype.toString = register.wrapFn(function() {
return `[Tombstone #${this.id}]`;
});

globals.Tombstone = Tombstone;
};
5 changes: 5 additions & 0 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ function processRoom(roomId, {intents, objects, users, terrain, gameTime, roomIn
if(object.type == 'nuke') {
roomInfo.active = true;
}
if(object.type == 'tombstone') {
roomInfo.active = true;
}
if(object.type == 'portal') {
roomInfo.active = true;
}
Expand Down Expand Up @@ -268,6 +271,8 @@ function processRoom(roomId, {intents, objects, users, terrain, gameTime, roomIn
require('./processor/intents/containers/tick')(object, objects, terrain, bulk, userBulk, roomController, gameTime);
if (object.type == 'terminal')
require('./processor/intents/terminal/tick')(object, objects, terrain, bulk, userBulk, roomController, gameTime);
if (object.type == 'tombstone')
require('./processor/intents/tombstones/tick')(object, objects, terrain, bulk, userBulk, roomController, gameTime);

if (object.type == 'nuke') {
require('./processor/intents/nukes/tick')(object, objects, terrain, bulk, userBulk, roomController, stats, gameTime, roomInfo);
Expand Down
53 changes: 48 additions & 5 deletions src/processor/intents/creeps/_die.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var _ = require('lodash'),
driver = utils.getDriver(),
C = driver.constants;

module.exports = function(object, roomObjects, bulk, stats, dropRate) {
module.exports = function(object, roomObjects, bulk, stats, dropRate, gameTime) {

if(dropRate === undefined) {
dropRate = C.CREEP_CORPSE_RATE;
Expand All @@ -12,6 +12,23 @@ module.exports = function(object, roomObjects, bulk, stats, dropRate) {
bulk.remove(object._id);
delete roomObjects[object._id];

let tombstone = {
type: 'tombstone',
room: object.room,
x: object.x,
y: object.y,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x, y, and room are the same as in the tombstone, no need to store them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are the tombstone x, y, and room

user: object.user,
deathTime: gameTime,
decayTime: gameTime + (object.body.length * C.TOMBSTONE_DECAY_PER_PART),
creepId: object._id,
creepName: object.name,
creepTicksToLive: object.ageTime - gameTime,
creepBody: _.map(object.body, b => b.type),
creepSaying: object.actionLog && object.actionLog.say && object.actionLog.say.isPublic ? object.actionLog.say.message : undefined
}

let container = _.find(roomObjects, { type: 'container', x: object.x, y: object.y })

if(dropRate > 0 && !object.userSummoned) {
var lifeTime = _.any(object.body, {type: C.CLAIM}) ? C.CREEP_CLAIM_LIFE_TIME : C.CREEP_LIFE_TIME;
var bodyResources = {energy: utils.calcCreepCost(object.body) * dropRate * object._ticksToLive / lifeTime};
Expand All @@ -25,20 +42,46 @@ module.exports = function(object, roomObjects, bulk, stats, dropRate) {
});

_.forEach(bodyResources, (amount, resourceType) => {
amount = Math.floor(amount)
if(amount > 0) {
require('./_create-energy')(object.x, object.y, object.room, Math.floor(amount), roomObjects, bulk,
resourceType, object.dropToContainer);
if(container && container.hits > 0) {
let targetTotal = utils.calcResources(container);
let toContainerAmount = Math.min(amount, container.energyCapacity - targetTotal);
if(toContainerAmount > 0) {
container[resourceType] = container[resourceType] || 0;
container[resourceType] += toContainerAmount;
bulk.update(container, {[resourceType]: container[resourceType]});
amount -= toContainerAmount;
}
}
if(amount > 0){
tombstone[resourceType] = (tombstone[resourceType] || 0) + amount;
}
}
});

C.RESOURCES_ALL.forEach(resourceType => {
if (object[resourceType] > 0) {
require('./_create-energy')(object.x, object.y, object.room,
object[resourceType], roomObjects, bulk, resourceType, object.dropToContainer);
let amount = object[resourceType]
if(container && container.hits > 0) {
let targetTotal = utils.calcResources(container);
let toContainerAmount = Math.min(amount, container.energyCapacity - targetTotal);
if(toContainerAmount > 0) {
container[resourceType] = container[resourceType] || 0;
container[resourceType] += toContainerAmount;
bulk.update(container, {[resourceType]: container[resourceType]});
amount -= toContainerAmount;
}
}
if(amount > 0){
tombstone[resourceType] = (tombstone[resourceType] || 0) + amount;
}
}
});
}

bulk.insert(tombstone)

if (stats && object.user != '3' && object.user != '2') {
stats.inc('creepsLost', object.user, object.body.length);
}
Expand Down
3 changes: 2 additions & 1 deletion src/processor/intents/creeps/intents.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var _ = require('lodash');

var priorities = {
rangedHeal: ['heal'],
attackController: ['rangedHeal', 'heal'],
dismantle: ['attackController','rangedHeal','heal'],
repair: ['dismantle','attackController','rangedHeal','heal'],
build: ['repair','dismantle','attackController','rangedHeal','heal'],
Expand All @@ -27,4 +28,4 @@ module.exports = function(object, objectIntents, roomObjects, roomTerrain, bulk,
modules[name](object, objectIntents[name], roomObjects, roomTerrain, bulk, bulkUsers, roomController, stats, gameTime, roomInfo, users);
}
});
};
};
4 changes: 2 additions & 2 deletions src/processor/intents/creeps/suicide.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var _ = require('lodash'),
driver = utils.getDriver(),
C = driver.constants;

module.exports = function(object, intent, roomObjects, roomTerrain, bulk, bulkUsers, roomController, stats) {
module.exports = function(object, intent, roomObjects, roomTerrain, bulk, bulkUsers, roomController, stats, gameTime) {

if(object.type != 'creep') {
return;
Expand All @@ -12,6 +12,6 @@ module.exports = function(object, intent, roomObjects, roomTerrain, bulk, bulkUs
return;
}

require('./_die')(object, roomObjects, bulk, undefined, object.user == '2' ? 0 : undefined);
require('./_die')(object, roomObjects, bulk, undefined, object.user == '2' ? 0 : undefined, gameTime);

};
17 changes: 4 additions & 13 deletions src/processor/intents/creeps/tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,7 @@ module.exports = function(object, roomObjects, roomTerrain, bulk, bulkUsers, roo
}

if(gameTime >= object.ageTime-1) {

C.RESOURCES_ALL.forEach(resourceType => {
var amount = object[resourceType];
if (amount) {
require('./_create-energy')(object.x, object.y, object.room, amount, roomObjects, bulk, resourceType, object.dropToContainer);
}
});

bulk.remove(object._id);
delete roomObjects[object._id];
require('./_die')(object, roomObjects, bulk, stats, undefined, gameTime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this will add to the creepsLost stat, which isn't exactly what you want.
Using undefined instead of stats will fix this already, but maybe it's better to add options to disable this behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, IIRC, somewhere else it also passes undefined as stats, updating now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its spawn recycle-creep where it passes undefined

}
}

Expand All @@ -121,11 +112,11 @@ module.exports = function(object, roomObjects, roomTerrain, bulk, bulkUsers, roo
}

if(_.isNaN(object.hits) || object.hits <= 0) {
require('./_die')(object, roomObjects, bulk, stats);
require('./_die')(object, roomObjects, bulk, stats, undefined, gameTime);
}

if(object.userSummoned && _.any(roomObjects, i => i.type == 'creep' && i.user != '2' && i.user != roomController.user)) {
require('./_die')(object, roomObjects, bulk, stats);
require('./_die')(object, roomObjects, bulk, stats, undefined, gameTime);
}

let oldHits = object.hits;
Expand All @@ -145,7 +136,7 @@ module.exports = function(object, roomObjects, roomTerrain, bulk, bulkUsers, roo
}

if(object.hits <= 0) {
require('./_die')(object, roomObjects, bulk, stats);
require('./_die')(object, roomObjects, bulk, undefined, undefined, gameTime);
}
else if(object.hits != oldHits) {

Expand Down
Loading