Coder Social home page Coder Social logo

creep-tasks's Introduction

creep-tasks npm version Build Status

creep-tasks is a plugin for your Screeps codebase which adds a concise and flexible creep.task property to your creeps. Tasks are persistent objects that generalize the concept of "do action X to thing Y until condition Z is met" and they can save a lot of convoluted and redundant code in creep logic. A Task object contains the necessary logic for traveling to a target, performing an action on the target, and realizing when a task is no longer sensible to continue.

creep-tasks has been adapted from the Overmind Screeps AI.

For an overview of how Tasks work and a full API reference, please refer to the creep-tasks Wiki.

Examples

Many Screeps bots use decision trees which run every tick to determine what a creep should be doing. Tasks streamline this process into two separate parts: task assignment and task execution. Since tasks are persistent, you only need to run decision tree logic when a creep is idle. Here's a very simple example of writing an upgrader role using tasks:

/* main.js */

let Tasks = require('creep-tasks');

// Upgraders will harvest to get energy, then upgrade the room controller
let roleUpgrader = {
    newTask: function(creep) { // task assignment logic
        if (creep.carry.energy > 0) {
            creep.task = Tasks.upgrade(creep.room.controller);
        } else {
            creep.task = Tasks.harvest(creep.room.find(FIND_SOURCES)[0])
        }
    }
};

module.exports.loop = function () {
    /* (Spawning logic would go here) */
    let upgraders = _.values(Game.creeps);
    for (let upgrader of upgraders) {
        if (upgrader.isIdle) { // obtain a new task if the creep is idle
            roleUpgrader.newTask(upgrader);
        }
        upgrader.run(); // run the assigned task
    }
};

This repository contains simple example bots built using creep-tasks written in JavaScript and in TypeScript. You can see more complex creep-tasks examples in the Overmind codebase.

Installation

There are several ways to install creep-tasks. I would recommend using npm, but depending on how your environment is set up and how much you'd like to modify the source, installing from binary or from source might work better for you.

(JavaScript/TypeScript) Install from npm:

Navigate to your project root and run npm install creep-tasks. The npm module comes with included typings if you are using TypeScript. Use var Tasks = require('creep-tasks') to import the Tasks module in JS or import Tasks from 'creep-tasks' to import in TS.

(JavaScript only) Install from compiled module:

  1. Download or copy/paste the code in bin/creep-tasks.js and put it in a new module.
  2. Add require('creep-tasks') to your main.js file outside of the main loop.
  3. Use var Tasks = require('creep-tasks') wherever you need to set creep tasks.

(TypeScript only) Install from source:

  1. Download this repository and copy the src/creep-tasks directory to somewhere in your codebase.
  2. Import the necessary prototypes in main.ts with import 'creep-tasks/prototypes' outside of the main loop.
  3. Use import {Tasks} from 'creep-tasks/Tasks' (path to Tasks.ts) whenever you need to set creep tasks.

Contributing

If you find an issue with creep-tasks or want to leave feedback, please feel free to submit an issue. If you'd like to contribute to creep-tasks, pull requests are also welcome!

Changelog

2018.8.1:

  • Version 1.3 released - adds chaining functionality with Tasks.chain(), adds TaskWithdrawAll, adds nextPos option, and fixes bugs

2018.6.29:

  • Version 1.2 released - adds TaskTransferAll, adds oneShot option, and fixes bugs

2018.5.1:

  • Version 1.1 released - repackaged for better npm support and now with included typings

2018.4.29:

  • Initial release

creep-tasks's People

Contributors

bencbartlett avatar cdm014 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

creep-tasks's Issues

No creep executing x!

Description of issue:

When a creep is switching from one task to another the new task is logging the error 'No creep executing taskname!'. Transfers and pickup are sometimes completely skipped by this causing the creep to only properly execute the goTo task.

It seems that the issue is caused because the creep name is undefined when the new task is executed.

Steps to reproduce:

Assign multiple tasks to a creep.task with Tasks.chain().

Error message:

N/A

No creep executing harvest!
No creep executing pickup!

Other information:

  • creep-tasks version: 1.3.0
  • Using creep-tasks with: TypeScript
  • Issue is with dependency: no

Getting the package using npm does not work

Description of issue:

I installed the package as described in README.md (by navigating into the root folder of my AI and running npm install creep-tasks). Afterwards, I require'd it in my scripts but Screeps said it could not find the module named creep-tasks.

Looking at the files installed by npm, only the type definitions were installed. The error might come from this line inside package.json , though I am not at all familiar with npm. I believe "files" within package.json should contain "src/**/*" as opposed to "dist/**/*".

Steps to reproduce:

npm install creep-tasks
Try to run AI using creep-tasks
Failure!

Error message:

Error: Unknown module 'creep-tasks'
    at Object.requireFn (<runtime>:30185:23)
    at role.patroller:1:49
    at role.patroller:15:3
    at Object.exports.evalCode (<runtime>:15727:76)
    at Object.requireFn (<runtime>:30203:28)
    at main:8:21
    at main:184:3
    at Object.exports.evalCode (<runtime>:15727:76)
    at Object.requireFn (<runtime>:30203:28)

Other information:

  • creep-tasks version: 1.2.0 {You can find this in package.json}
    • Commit hash: 7c49da6 {Please include the specific commit hash if known}
  • Using creep-tasks with: JavaScript
  • Issue is with dependency: No

Problem with tsconfig.json, types

Description of issue:

Hi, i have problems with types property in tsconfig.json while compiling, it throws error.

    "types": [
      "lodash",
      "typed-screeps"
    ]

If i remove theese lines, typings works just fine (using Intellij idea) and project compiles successfully.
They are also correctly installed in node_modules
(for the record, same problem with Overmind)

Steps to reproduce:

  • npm install
  • screeps.config.json (same as for Overmind)
  • npm run push-pserver

Error message:

rpt2: options error TS2688 Cannot find type definition file for 'typed-screeps'.
[!] (rpt2 plugin) Error: D:/git/creep-tasks/src/creep-tasks/prototypes.ts(6,23): semantic error TS2693 'Creep' only refers to a type, but is being used as a value here.


Other information:

  • creep-tasks version: 1.0.0
  • Using creep-tasks with: {TypeScript}
  • Issue is with dependency: {Yes}

doc: import via TS not working

In documentation it's said

import { Tasks } from 'creep-tasks'

For me it worked via

import { default as Tasks } from 'creep-tasks'

as you have defined:

const Tasks_1 = require("./creep-tasks/Tasks");
exports.default = Tasks_1.Tasks;

Need to update Screep-tasks.js in Example folder to 1.3 (or latest version)

Description of issue:

creep-tasks/examples/JavaScript/creep-tasks.js is still 1.0.0 rather than the latest version. This caused an exception because Task.chain is missing from 1.0.0

Steps to reproduce:

run the Example code as is and wait for it to create a Patroller

Error message:

// Console output:

    at Object.newTask (role.patroller:11:28)
    at Object.module.exports.loop (main:40:27)
    at __mainLoop:1:52
    at __mainLoop:2:3
    at Object.exports.evalCode (<isolated-vm>:15343:76)
    at Object.exports.run (<isolated-vm>:20768:24)



Question: Why is creep._task not set when originally setting the task?

https://github.com/bencbartlett/creep-tasks/blob/master/src/creep-tasks/prototypes.ts#L39

It looks like when you assign creep.task, the only thing that gets set is the creep's memory, which then needs to get read again and instantiate a new Task object. What's the reason for this?

Seems odd that the steps are essentially:

// Instantiate new Task object to assign
creep.task = new TaskHarvest();
// Under the hood, this sets
creep.memory.task = task.proto;
// Then when creep,task is read
creep._task = new TaskHarvest() // via the prototask

Why not just set creep._task = task in the initial setter?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.