create a screep code to evolute my colony
- User
create a screep code to evolute my colony
- Assistant
It looks like you meant "Screeps" code instead of "screep code." Screeps is a game where you write JavaScript code to control your colony of creeps (programmable units) in a real-time strategy environment. The goal is to "evolve" your colony by harvesting energy, building structures, upgrading your room controller, and expanding.
I'll provide a basic Screeps script to get you started. This code will:
- Spawn creeps with roles like "harvester" (to collect energy) and "upgrader" (to upgrade your room controller).
- Manage creep behavior based on their roles.
- Keep things simple so you can build upon it.
Key Concepts:
- Roles: Creeps are assigned roles (e.g., 'harvester' or 'upgrader') to perform specific tasks.
- Spawning: The script checks if you need more creeps and spawns them using your spawn structure.
- Memory: Screeps uses a memory system to store data for creeps.
- Loop: The main function runs every game tick.
This is a beginner-friendly setup. You can evolve it by adding more roles (e.g., builders or defenders) as your colony grows.
How to Use This Code:
- Log in to your Screeps account and go to the code editor.
- Replace your
main.jsfile with the code below. (Screeps uses modules, so you'll need to create separate files for each role.) - Create new files in your Screeps project:
role.harvester.jsrole.upgrader.js
- Test and iterate: Run the game and watch your creeps in action. Adjust numbers (e.g., how many creeps to spawn) based on your room's needs.
Here's the code:
main.js (Your main entry point)
// Load role modules var roleHarvester = require('role.harvester'); var roleUpgrader = require('role.upgrader'); module.exports.loop = function () { // 1. Clear memory of dead creeps to free up resources for (var name in Memory.creeps) { if (!Game.creeps[name]) { delete Memory.creeps[name]; console.log('Clearing non-existing creep memory: ' + name); } } // 2. Count existing creeps by role var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester'); var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader'); // 3. Spawn new creeps if needed // Assuming your spawn is named 'Spawn1' (change if different) if (harvesters.length < 2) { // Aim for at least 2 harvesters var newName = 'Harvester' + Game.time; Game.spawns['Spawn1'].spawnCreep([WORK, CARRY, MOVE], newName, {memory: {role: 'harvester'}}); console.log('Spawning new harvester: ' + newName); } if (upgraders.length < 2) { // Aim for at least 2 upgraders var newName = 'Upgrader' + Game.time; Game.spawns['Spawn1'].spawnCreep([WORK, CARRY, MOVE], newName, {memory: {role: 'upgrader'}}); console.log('Spawning new upgrader: ' + newName); } // 4. Run creep logic for (var name in Game.creeps) { var creep = Game.creeps[name]; if (creep.memory.role == 'harvester') { roleHarvester.run(creep); // Run harvester behavior } else if (creep.memory.role == 'upgrader') { roleUpgrader.run(creep); // Run upgrader behavior } } };role.harvester.js (Behavior for harvesters)
var roleHarvester = { /** @param {Creep} creep */ run: function(creep) { if (creep.store.getFreeCapacity() > 0) { // If the creep has space, harvest energy var sources = creep.room.find(FIND_SOURCES); if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); creep.say('🔄 Harvest'); } } else { // If full, transfer energy to structures like spawn or extensions var targets = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_TOWER) && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; } }); if (targets.length > 0) { if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); creep.say('🚚 Transfer'); } } } } }; module.exports = roleHarvester;role.upgrader.js (Behavior for upgraders)
var roleUpgrader = { /** @param {Creep} creep */ run: function(creep) { if (creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) { // If upgrading and out of energy, switch to harvesting creep.memory.upgrading = false; creep.say('🔄 Harvest'); } if (!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) { // If not upgrading and full of energy, switch to upgrading creep.memory.upgrading = true; creep.say('⚡ Upgrade'); } if (creep.memory.upgrading) { // Upgrade the room controller if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}}); } } else { // Harvest energy var sources = creep.room.find(FIND_SOURCES); if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); } } } }; module.exports = roleUpgrader;Explanation of the Code:
- Memory Clearing: Prevents memory leaks by removing data for creeps that no longer exist.
- Creep Spawning: The script checks the number of creeps and spawns new ones with basic body parts ([WORK, CARRY, MOVE]). You can make bodies more advanced later (e.g., add more WORK parts for faster harvesting).
- Harvester Behavior: Harvests energy from the nearest source and deposits it into your spawn, extensions, or towers.
- Upgrader Behavior: Harvests energy when needed and upgrades your room controller to level up your colony.
- Visualization: The
visualizePathStylehelps you see creep paths in the game for debugging.
Tips to Evolve Your Colony:
- Add More Roles: Create a 'builder' role to construct new structures like extensions or towers.
- Optimize Spawning: Adjust the numbers (e.g.,
if (harvesters.length < 4)) based on your room's energy sources and size. - Handle Multiple Rooms: This code is for a single room. For multi-room setups, add logic to manage creeps across rooms.
- Defend Your Colony: Add a 'defender' role once you encounter hostiles.
- Error Handling: In a real game, add checks for things like no available spawn or low energy.
- Performance: Use Lodash (which is built into Screeps) for efficient filtering, as shown.
This should give your colony a solid foundation. As you play, you'll encounter challenges like energy shortages or enemy creeps—use the Screeps console to debug and iterate. If you need more advanced features or have specific requests (e.g., adding builders), let me know! 😊
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.