Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xperimental
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xperimental/b6b880ac4a5da13b8cb5 to your computer and use it in GitHub Desktop.
Save xperimental/b6b880ac4a5da13b8cb5 to your computer and use it in GitHub Desktop.
Simple code from screeps.com
[
{
"_id": 0,
"modules": {
"main": "var _ = require('lodash');\nvar spawn = require('spawn');\nvar harvester = require('harvester');\nvar builder = require('builder');\nvar guard = require('guard');\n\nGame.s = spawn;\nGame.r = {\n h: harvester,\n b: builder,\n g: guard\n};\n\n_.forEach(_.values(Game.creeps), function (c) {\n _.forEach(_.values(Game.r), function(r) {\n if (c.memory.role == r.role) {\n r.brain(c);\n }\n });\n});\n",
"harvester": "/*\n * Module code goes here. Use 'module.exports' to export things:\n * module.exports = 'a thing';\n *\n * You can import it from another modules like this:\n * var mod = require('harvester'); // -> 'a thing'\n */\nmodule.exports = {\n role: 'harvester',\n body: [Game.WORK, Game.CARRY, Game.MOVE, Game.MOVE],\n brain: function (c) {\n if (c.energy < c.energyCapacity) {\n var sources = c.room.find(Game.SOURCES_ACTIVE);\n if (sources.length) {\n c.moveTo(sources[0]);\n c.harvest(sources[0]);\n }\n } else {\n var s = Game.spawns.Spawn1;\n c.moveTo(s);\n c.transferEnergy(s);\n }\n }\n};\n",
"builder": "/*\n * Module code goes here. Use 'module.exports' to export things:\n * module.exports = 'a thing';\n *\n * You can import it from another modules like this:\n * var mod = require('builder'); // -> 'a thing'\n */\nmodule.exports = {\n role: 'builder',\n body: [Game.WORK, Game.WORK, Game.WORK, Game.CARRY, Game.MOVE, Game.MOVE],\n brain: function (c) {\n if (c.energy > 0) {\n var t = c.room.find(Game.CONSTRUCTION_SITES);\n if (t) {\n c.moveTo(t[0]);\n c.build(t[0]);\n }\n } else {\n var s = Game.spawns.Spawn1;\n c.moveTo(s);\n s.transferEnergy(c);\n }\n }\n};",
"guard": "/*\n * Module code goes here. Use 'module.exports' to export things:\n * module.exports = 'a thing';\n *\n * You can import it from another modules like this:\n * var mod = require('guard'); // -> 'a thing'\n */\nmodule.exports = {\n role: 'guard',\n body: [Game.ATTACK, Game.TOUGH, Game.MOVE, Game.MOVE],\n brain: function(c) {\n var t = c.room.find(Game.HOSTILE_CREEPS);\n if (t.length) {\n c.moveTo(t[0]);\n c.attack(t[0]);\n }\n }\n};\n",
"spawn": "/*\n * Module code goes here. Use 'module.exports' to export things:\n * module.exports = 'a thing';\n *\n * You can import it from another modules like this:\n * var mod = require('spawn'); // -> 'a thing'\n */\nmodule.exports = {\n c: function(role) {\n var s = Game.spawns.Spawn1;\n var name = s.createCreep(role.body);\n Memory.creeps[name].role = role.role;\n }\n};"
}
},
{
"_id": 1,
"modules": {
"main": ""
}
},
{
"_id": 2,
"modules": {
"main": "var _ = require('lodash'), healer = require('healer'), findAttack = require('findAttack'); for(var i in Game.creeps) { var creep = Game.creeps[i]; if(creep.getActiveBodyparts('heal') > 0) { healer(creep); } else if(/Defend/.test(creep.name)) { findAttack(Game.creeps[i], false, true); } else if(/Siege/.test(creep.name)) { findAttack(Game.creeps[i], true); } else { findAttack(Game.creeps[i], false); } } ",
"findAttack": "module.exports = function(creep, ignoreCreeps, defend) { function find(opts) { opts = opts || {}; opts.filter = function(i) { return i.pos.inRangeTo(creep, 7); }; var target; if(!ignoreCreeps) { target = creep.pos.findNearest(Game.HOSTILE_CREEPS, opts); } if(!target) { opts.filter = null; target = creep.pos.findNearest(Game.HOSTILE_SPAWNS, opts); } if(target) { creep.moveTo(target, opts); } return target; } var target, healers = creep.room.find(Game.MY_CREEPS, {filter: function(i) { return i.getActiveBodyparts('heal') > 0; }}); if(defend) { var siege = creep.room.find(Game.MY_CREEPS, {filter: function(i) { return /Siege/.test(i.name); }}); if(siege.length > 0) { target = siege[0]; creep.moveTo(target); } } if(!target && creep.hits < creep.hitsMax / 2 && healers.length > 0) { target = creep.pos.findNearest(Game.MY_CREEPS, {filter: function(i) { return i.getActiveBodyparts('heal') > 0; }}); if(!target || creep.moveTo(target) != Game.OK) { target = null; } } if(!target) { target = find(); if(!target) { target = find({ignoreDestructibleStructures: true}); } if(!target) { return; } creep.attack(target); } if(creep.getActiveBodyparts(Game.RANGED_ATTACK)) { var targets = creep.pos.findInRange(Game.HOSTILE_CREEPS, 3); if(targets.length) { creep.rangedAttack(targets[0]); } else { targets = creep.pos.findInRange(Game.HOSTILE_SPAWNS, 3); if(targets.length) { creep.rangedAttack(targets[0]); } } } }",
"healer": "module.exports = function(creep) { var target = creep.pos.findNearest(Game.MY_CREEPS, {filter: function(i) { return i != creep && i.hits < i.hitsMax; }}); if(!target) { target = creep.pos.findNearest(Game.MY_CREEPS, {filter: function(i) { return i != creep; }}); } if(!target) { return; } if(target.hits == target.hitsMax || creep.heal(target) != Game.OK) { creep.moveTo(target); } if(creep.getActiveBodyparts(Game.RANGED_ATTACK)) { var targets = creep.pos.findInRange(Game.HOSTILE_CREEPS, 3); if(targets.length) { creep.rangedAttack(targets[0]); } else { targets = creep.pos.findInRange(Game.HOSTILE_SPAWNS, 3); if(targets.length) { creep.rangedAttack(targets[0]); } } } }"
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment