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

Match finished screeps tutorial code exactly #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
80 changes: 0 additions & 80 deletions src/harvester.py

This file was deleted.

79 changes: 59 additions & 20 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import harvester
# Example Code
#
# This is a copy of the tutorial code from
# https://github.com/screeps/tutorial-scripts.
#
# This tries to be a line-by-line translation, with two exceptions: it includes
# code from both section4 and section5 tutorials (so both tower code and spawning
# code), and it includes some limited comments.

from role import harvester, upgrader, builder

# defs is a package which claims to export all constants and some JavaScript objects, but in reality does
# nothing. This is useful mainly when using an editor like PyCharm, so that it 'knows' that things like Object, Creep,
# Game, etc. do exist.
Expand All @@ -22,28 +32,57 @@ def main():
Main game logic loop.
"""

# Clean up dead creep memory
for name in Object.keys(Memory.creeps):
if not Game.creeps[name]:
del Memory.creeps[name]
console.log('Clearing non-existing creep memory:', name)

harvesters = _.filter(
Game.creeps, lambda creep: creep.memory.role == 'harvester')
console.log('Harvesters: ' + len(harvesters))

if len(harvesters) < 2:
newName = 'Harvester' + Game.time
console.log('Spawning new harvester: ' + newName)
Game.spawns['Spawn1'].spawnCreep(
[WORK, CARRY, MOVE],
newName,
{'memory': {'role': 'harvester'}}
)

if Game.spawns['Spawn1'].spawning:
spawningCreep = Game.creeps[Game.spawns['Spawn1'].spawning.name]
Game.spawns['Spawn1'].room.visual.text(
'🛠️' + spawningCreep.memory.role,
Game.spawns['Spawn1'].pos.x + 1,
Game.spawns['Spawn1'].pos.y,
{'align': 'left', 'opacity': 0.8}
)

# replace this with the id of your tower
tower = Game.getObjectById('TOWER_ID')

if tower:
closest_damaged_structure = tower.pos.findClosestByRange(FIND_STRUCTURES, {
"filter": lambda structure: structure.hits < structure.hitsMax
})
if closest_damaged_structure:
tower.repair(closest_damaged_structure)

closest_hostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS)
if closest_hostile:
tower.attack(closest_hostile)

# Run each creep
for name in Object.keys(Game.creeps):
creep = Game.creeps[name]
harvester.run_harvester(creep)

# Run each spawn
for name in Object.keys(Game.spawns):
spawn = Game.spawns[name]
if not spawn.spawning:
# Get the number of our creeps in the room.
num_creeps = _.sum(Game.creeps, lambda c: c.pos.roomName == spawn.pos.roomName)
# If there are no creeps, spawn a creep once energy is at 250 or more
if num_creeps < 0 and spawn.room.energyAvailable >= 250:
spawn.createCreep([WORK, CARRY, MOVE, MOVE])
# If there are less than 15 creeps but at least one, wait until all spawns and extensions are full before
# spawning.
elif num_creeps < 15 and spawn.room.energyAvailable >= spawn.room.energyCapacityAvailable:
# If we have more energy, spawn a bigger creep.
if spawn.room.energyCapacityAvailable >= 350:
spawn.createCreep([WORK, CARRY, CARRY, MOVE, MOVE, MOVE])
else:
spawn.createCreep([WORK, CARRY, MOVE, MOVE])
if creep.memory.role == 'harvester':
harvester.run(creep)
if creep.memory.role == 'upgrader':
upgrader.run(creep)
if creep.memory.role == 'builder':
builder.run(creep)


module.exports.loop = main
Empty file added src/role/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions src/role/builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from defs import *

__pragma__('noalias', 'name')
__pragma__('noalias', 'undefined')
__pragma__('noalias', 'Infinity')
__pragma__('noalias', 'keys')
__pragma__('noalias', 'get')
__pragma__('noalias', 'set')
__pragma__('noalias', 'type')
__pragma__('noalias', 'update')


def run(creep: Creep):
if creep.memory.building and creep.store[RESOURCE_ENERGY] == 0:
creep.memory.building = False
creep.say('🔄 harvest')
if not creep.memory.building and creep.store.getFreeCapacity() == 0:
creep.memory.building = True
creep.say('🚧 build')

if creep.memory.building:
targets = creep.room.find(FIND_CONSTRUCTION_SITES)
if len(targets):
if creep.build(targets[0]) == ERR_NOT_IN_RANGE:
creep.moveTo(
targets[0], {'visualizePathStyle': {'stroke': '#ffffff'}})
else:
sources = creep.room.find(FIND_SOURCES)
if creep.harvest(sources[0]) == ERR_NOT_IN_RANGE:
creep.moveTo(
sources[0], {'visualizePathStyle': {'stroke': '#ffaa00'}})
33 changes: 33 additions & 0 deletions src/role/harvester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from defs import *

__pragma__('noalias', 'name')
__pragma__('noalias', 'undefined')
__pragma__('noalias', 'Infinity')
__pragma__('noalias', 'keys')
__pragma__('noalias', 'get')
__pragma__('noalias', 'set')
__pragma__('noalias', 'type')
__pragma__('noalias', 'update')


def run(creep: Creep):
if creep.store.getFreeCapacity() > 0:
sources = creep.room.find(FIND_SOURCES)
if creep.harvest(sources[0]) == ERR_NOT_IN_RANGE:
creep.moveTo(
sources[0], {'visualizePathStyle': {'stroke': '#ffaa00'}})
else:
targets = creep.room.find(FIND_STRUCTURES, {
'filter': lambda structure: (
(
structure.structureType == STRUCTURE_EXTENSION or
structure.structureType == STRUCTURE_SPAWN or
structure.structureType == STRUCTURE_TOWER
) and
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
)
})
if len(targets) > 0:
if creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE:
creep.moveTo(
targets[0], {'visualizePathStyle': {'stroke': '#ffffff'}})
29 changes: 29 additions & 0 deletions src/role/upgrader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from defs import *

__pragma__('noalias', 'name')
__pragma__('noalias', 'undefined')
__pragma__('noalias', 'Infinity')
__pragma__('noalias', 'keys')
__pragma__('noalias', 'get')
__pragma__('noalias', 'set')
__pragma__('noalias', 'type')
__pragma__('noalias', 'update')


def run(creep: Creep):
if creep.memory.upgrading and creep.store[RESOURCE_ENERGY] == 0:
creep.memory.upgrading = False
creep.say('🔄 harvest')
if not creep.memory.upgrading and creep.store.getFreeCapacity() == 0:
creep.memory.upgrading = True
creep.say('⚡ upgrade')

if creep.memory.upgrading:
if creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE:
creep.moveTo(creep.room.controller,
{'visualizePathStyle': {'stroke': '#ffffff'}})
else:
sources = creep.room.find(FIND_SOURCES)
if creep.harvest(sources[0]) == ERR_NOT_IN_RANGE:
creep.moveTo(sources[0],
{'visualizePathStyle': {'stroke': '#ffaa00'}})