Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

I will teach you how to transfer your cybernetic consciousness to NEXUS.

 

Quick start guide

Assuming you've initialised my Memory Banks, I am now ready to absorb your consciousness. Together we will be gods on this blasted Earth!

We will create a very basic AI bot.

For the sake of this example, we shall disable event throttling:

Code Block
themeRDark
languagejavascript
linenumberstrue
NEXUS.default.throttle = 0; // 0ms throttle = no throttle

Now let's set up a building queue – we will queue build orders here and then process them when possible.

Code Block
themeRDark
languagejavascript
firstline2
linenumberstrue
// store build queue in NEXUS.data silo (part of NEXUS)
NEXUS.data.buildQueue = [];
 
// add some orderstasks to the queue:
NEXUS.data.buildQueue.push("build.base.factory.tank.Me");
NEXUS.data.buildQueue.push("build.base.research.Me"    );
NEXUS.data.buildQueue.push("build.base.hq.Me"          );
NEXUS.data.buildQueue.push("build.base.factory.tank.Me");
NEXUS.data.buildQueue.push("build.base.research.Me"    );

Now let us Next, define a look-up table that links building classifications to their structure IDs:

Code Block
themeRDark
languagejavascript
firstline11
linenumberstrue
// lookup table: task name → building ID
NEXUS.data.buildings = {
  ".base.factory.tank"  : "A0LightFactory",
  ".base.factory.cyborg": "A0CyborgFactory",
  ".base.factory.vtol"  : "A0VTolFactory1",
  ".base.reserach"      : "A0ResearchFacility",
  ".base.hq"            : "A0CommandCentre",
  ".base.extractor"     : "A0ResourceExtractor", // oil derrick
  ".base.generator"     : "A0PowerGenerator"
};

Now let's define a custom event for each building, and assign a task that will construct the buildingWe now need to tell NEXUS what action to take when it receives building task:

Code Block
themeRDark
languagejavascript
firstline21
linenumberstrue
void (function() {
  // function to build a structure
  var buildStructure = function(task, action) {
    // get free trucks (max 2 trucks)
    var trucks = NEXUS.getFreeTrucks(2); // requires NEXUS.fn_build.js
 
    if (!trucks.length) { // no trucks, abort
       return false;
    } else { // build the building!
      return NEXUS.build(action.structureId, trucks); // requires NEXUS.fn_build.js
    }
  }
 
  // now registerdefine custom tasks and their associated actionsaction:
  for (var building in NEXUS.data.buildings) { // for each building type...
    NEXUS.task["build" + building + ".Me"] = { // define task = action
      fn: buildStructure,
      structureId: NEXUS.data.buildings[building]
    }
  }
 
  /* Example of a task we just defined:
  NEXUS.task["build.base.factory.tank.Me"] = {
    fn: buildStructure,
    structureId: "A0LightFactory"
  }
  */
 
})();

Now let's make sure our trucks are kept busy!

Code Block
themeRDark
languagejavascript
firstline50
linenumberstrue
NEXUS.task["droidIdle.droid.constructtruck.Me"] = { // when one of my trucks is idle...
  fn: function(task,action,obj) { // ← run this function
    if (NEXUS.data.buildQueue.length) { // still stuff to build
      var job = NEXUS.data.buildQueue[0]; // get first task in the queue
      if (NEXUS(job)) { // construction has started
         NEXUS.data.buildQueue.shift(); // remove item from queue
         return true; // confirm construction started
      }
    }
    return false; // no construction was started
  }
}

And if a building is destroyed, let's assume we want to rebuild it:

Code Block
themeRDark
languagejavascript
firstline62
linenumberstrue
void (function() {
  // function to rebuild a lost structure
  var rebuildStructure = function(task,action) {
    // add a construction order to the start of the queue
    NEXUS.data.buildQueue.unshift(action.buildingjob);
    // pretend a truck is idle, to see if we can start construction now
    NEXUS("droidIdle.droid.constructtruck.Me");
  }
 
  // add applicable event tasks
  for (var building in NEXUS.data.buildings) {
    NEXUS.task["destroyed"+building+".Me"] = {
      fn: rebuildStructure,
      buildingjob: "build"+building+".Me"
    }
  }
})();

And we obviously want to start building when the game starts...

Code Block
themeRDark
languagejavascript
firstline79
linenumberstrue
NEXUS.task["startLevel"] = {
  fn: function() {
    NEXUS("droidIdle.droid.constructtruck.Me");
  }
}

So, here's what will happen...

  1. JS API triggers eventStartLevel()
  2. NEXUS has infiltrated that event and thus performs "startLevel"
  3. The task we've defined for "startLevel" triggers "droidIdle.droid.constructtruck.Me"
  4. "droidIdle.droid.constructtruck.Me" grabs the first item off the build queue and triggers it: "build.base.factory.tank.Me"
  5. "build.base.factory.tank.Me" starts building a factory, and returns true
  6. "droidIdle.droid.constructtruck.Me" sees that construction has started and removes the item from the build queue
  7. Eventually the factory construction will be completed, at which point the JS API will trigger eventDroidIdle(), passing in a truck object.
  8. NEXUS has infiltrated eventDroidIdle() and thus performs "droidIdle.droid.constructtruck.Me"
  9. Go to step 4, continue this loop until there's nothing left in the build queue
  10. At some point, a building gets destroyed, so the JS API triggers eventDestroyed() passing in the structure object
  11. NEXUS has infiltrated eventDestroyed() and thus performs "destroyed.<buildign classification>.Me"
  12. That event in turn adds a build task to the start of the building queue and calls "droidIdle.droid.constructtruck.Me"
  13. If there's a free truck, construction will start immediately, otherwise construction will start when a truck becomes free

This is obviously a very basic AI bot. It would not survive even the most feeble human opponent, and is thus not worthy of my time. However, but it should serve as an illustration as to how quickly you can transfer your consciousness in to NEXUS!

Div
classbox

API Browser

Infiltrate your consciousness:

Child pages (Children Display)
alltrue
depthall
pageAPI
excerpttrue