Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Estecka committed Dec 9, 2018
0 parents commit 05acdb8
Show file tree
Hide file tree
Showing 21 changed files with 742 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/X.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require("XArray");
require("XCreep");
require("XObject");
require("XRoom");
require("XRoomObject");
require("XRoomVisual");
require("XRoomVisual.Resources");
require("XStructure");
24 changes: 24 additions & 0 deletions Assets/XArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Object.defineProperty(Array.prototype, "remove", {
configurable: true,
enumerable: false,
value: function(value){
this.splice(this.indexOf(value), 1)
}
})

_.findBy = function(array, predicate){
let smallest = NaN;
let value = undefined;
for(let i in array){
let r = predicate(array[i]);
if (r === true)
return array[i];
// console.log(r+' '+!isNaN(r));
if (r!==false && !(smallest<r)){
// console.log(boop)
smallest = r;
value = array[i];
}
}
return value;
}
73 changes: 73 additions & 0 deletions Assets/XCreep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
Object.defineProperty(Creep.prototype, "cargoSize", {
configurable: true,
get: function(){ return _.sum(this.carry); }
});
Object.defineProperty(Creep.prototype, "isFull", {
configurable: true,
get: function(){ return this.cargoSize >= this.carryCapacity; }
});
Object.defineProperty(Creep.prototype, "isEmpty", {
configurable: true,
get: function(){ return this.cargoSize <= 0; }
});

Creep.prototype.withdrawMax = function(target, type, amount){
let free = this.carryCapacity - this.cargoSize;
free = Math.min(free, amount, target.store[type]);

if (free <=0 && amount != 0)
return ERR_FULL;

return this.withdraw(target, type, free);
}


Creep.prototype.withdrawAll = function(target, filter) {
let free = this.carryCapacity - this.cargoSize;
if (free <= 0)
return ERR_FULL;

for (let res in target.store) {
if (target.store[res] > 0 && (!filter || filter.includes(res) )) {
return this.withdraw(target, res, Math.min(target.store[res], free));
}
}
return ERR_NOT_ENOUGH_RESOURCES;
};

Creep.prototype.transferAllEnergy = function(target){
if (target.energyCapacity == undefined)
return ERR_INVALID_TARGET;

let capacity = target.energyCapacity-target.energy;
if (capacity == 0)
return ERR_FULL;

if (capacity>this.carry.energy)
capacity = this.carry.energy;

return this.transfer(target, RESOURCE_ENERGY, capacity);

};

Creep.prototype.transferAll = function(target, filter = null) {
let free = target.storeCapacity - _.sum(target.store);
if (free <= 0)
return ERR_FULL;

for (let res in this.carry) {
if ((this.carry[res]>0) && (!filter || filter.includes(res))) {
return this.transfer(target, res, Math.min(this.carry[res], free));
}
}
return ERR_NOT_ENOUGH_RESOURCES;
};

Creep.prototype.moveToRoom = function(name){
try {
return this.moveTo(new RoomPosition(25,25, name), {range: 20})
} catch(e) {
console.log(e);
return ERR_INVALID_ARGS;
}
}
9 changes: 9 additions & 0 deletions Assets/XObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global.logObject = function(object){
if (object == null)
return console.log(object);
let log = "";
Object.keys(object).forEach(
key => log += key+" : "+object[key]+"\n"
);
console.log(log);
};
142 changes: 142 additions & 0 deletions Assets/XRoom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const repairIDcache = {};
const repairIDExpiration = {};
const REFRESH_TIME = 100;

Object.defineProperty(Room.prototype, "repairList", {
configurable: true,
get: function(){
if (this._repairList)
return this._repairlist;

else
if (repairIDcache[this.name] && repairIDExpiration[this.name] > Game.time) {
repairIDcache[this.name] = _.filter(repairIDcache[this.name], id => {
let str = Game.getObjectById(id);
return str && str.hits < str.hitsMax;
});
return this._repairlist = repairIDcache[this.name].map(id=>Game.getObjectById(id));
}

else {
this._repairlist = this.find(FIND_STRUCTURES, {filter: str => str.hits<str.hitsMax });
repairIDcache[this.name] = this._repairlist.map(obj => obj.id);
repairIDExpiration[this.name] = Game.time + REFRESH_TIME;
return this._repairlist;
}
},
});

/** Creates a creep using the first available Spawn in the room.
* @param [string] body
* @param {string} name
* @param {Object} memory
**/
Room.prototype.createCreep = function(body, name, memory){
if (!this.controller.my)
return ERR_NOT_OWNER; //You don't own this room
let spawnList = this.find(FIND_MY_SPAWNS);
if (!spawnList.length)
return ERR_NOT_FOUND; //You have no spawns there

let theChosenOne;
for(let spawn in spawnList){
spawn = spawnList[spawn];
if(spawn.spawning)
continue;
else
theChosenOne = spawn;
break;
}

if (theChosenOne==undefined)
return ERR_BUSY; //All spawns are busy

return theChosenOne.createCreep(body, name, memory);
}


let findEmptiestFuel = function(array, free = 0){
let result = undefined;
for (var i in array){
let obj = array[i];
let f = obj.energyCapacity - obj.energy;
if (f>free){
free = f;
result = obj;
}
}
return result;
}

/** Find the best spot to drop your energy **/
Room.prototype.findBestRefuelSpot = function(structures = [STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER, STRUCTURE_LAB]){
console.log(new Error("Deprecated function used : findRefuelSpot").stack);
if (!this.controller.my)
return null;

/** @type {Structure[]} str **/
let all = this.find(FIND_MY_STRUCTURES);
let free = 0;
let obj = null;
let result = null //this.controller;

for (let i in structures){
let some = _.filter(all, (s)=>s.structureType == structures[i] && !s.memory.output);
obj = findEmptiestFuel(some, free);
if (obj && obj.energyCapacity-obj.energy > free)
result = obj;
if (result)
break;
}
return result;
};

let findEmptiestStore = function(array, free = 0){
let result = null;
for (var i in array){
let obj = array[i];
let f = obj.storeCapacity - _.sum(obj.store);
if (f>free){
free = f;
result = obj;
}
}
return result;
}
/** Find the best spot to drop your cargo **/
Room.prototype.findBestStorage = function(structures = [STRUCTURE_STORAGE, STRUCTURE_CONTAINER]){
console.log(new Error("Deprecated function used : findBestStorage").stack);

/** @type {Structure[]} str **/
let all = this.find(FIND_STRUCTURES);
let free = 0;
let obj = null;
let result = null;

for (let i in structures){
let some = _.filter(all, (s)=>(s.structureType == structures[i]));
obj = findEmptiestStore(some, free);
if (obj && obj.storeCapacity-_.sum(obj.store) > free)
result = obj;
}
return result;
};

Room.prototype.findStoredResource = function(resourceType, structures = [STRUCTURE_CONTAINER, STRUCTURE_STORAGE]){
let all = this.find(FIND_STRUCTURES);

let amount = 0;
let result = null;
for (let i in structures){
let some = _.filter(all, (s)=>s.structureType == structures[i]);
for (let str in some){
str = some[str];
let avail = str.store[resourceType];
if (avail > 0 && avail > amount){
amount = avail;
result = str;
}
}
}
return result;
}
31 changes: 31 additions & 0 deletions Assets/XRoomObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
RoomObject.prototype.say = function(speech){
let style, points, balloon;
balloon = new RoomVisual(this.room.name);
points = [
[this.pos.x-0.5, this.pos.y-1],
[this.pos.x, this.pos.y-0.5],
[this.pos.x+0.5, this.pos.y-1],
];
style = {
"stroke":"#ccc",
"fill": "#ccc",
"opacity": 1,
}
balloon.poly(points, style);

style = {
"color": "black",
"font": "bold 0.58 sans-serif",
"backgroundColor": "#ccc",
"backgroundPadding":0.15,
}
balloon.text(speech, this.pos.x, this.pos.y-1.25, style);
return 0;
}

// Creep.prototype._say = Creep.prototype.say;
// Creep.prototype.say = function(message){
// return this.my ?
// this._say(message):
// RoomObject.prototype.say.call(this, message);
// }
4 changes: 4 additions & 0 deletions Assets/XRoomPosition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RoomPosition.prototype.fromLiteral = function(obj){
try { return new RoomPosition(obj.x, obj.y, obj.roomName); }
catch (e) { return null; }
};
Loading

0 comments on commit 05acdb8

Please sign in to comment.