Skip to content

Commit

Permalink
Update for 0.8.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dwonderley committed Aug 22, 2021
1 parent 1ec598a commit fb1712e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
6 changes: 3 additions & 3 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"title": "mookAI - An AI for your mooks",
"description": "<p>The mook is characterizied by a lack of ambition: they must be told where to go, what to do, and when to do it. This module automates those decisions on their behalf, freeing you of managing their doomed efforts.</p><p>The scope of this module does not include full automation. It is merely a tool to handle the movement and choices of low-intelligence, low-utility, high-quantity enemies (e.g. goblins, wild animals, barbarians) so that the game master may focus on the Heroes and their true adversaries.</p><p>This module adds a hotkey ('g') that, when pressed, triggers this automation. The module finds the token with the current initiative, checks its vision for possible targets, chooses one to attack (or explores if there are none), plans a collission-free path, moves the token into range, attacks, and ends the turn. Mooks will repeat these steps to account for changes in vision and will run until they attack or run out of resources.</p><p>In a future release, rules for taking a turn will be customized from a token's actor's character sheet, but for now, settings apply to all tokens.</p><p>Please read the documentation.</p>",
"author": "Dave Of Wonders",
"version": "0.1.4",
"minimumCoreVersion": "0.7.6",
"compatibleCoreVersion": "0.7.8",
"version": "0.2.0",
"minimumCoreVersion": "0.8.7",
"compatibleCoreVersion": "0.8.7",
"dependencies": [
{
"name": "lib-find-the-path"
Expand Down
20 changes: 10 additions & 10 deletions scripts/behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
export const MookTypes = Object.freeze ({
// Uses mele attacks first, attacks closest
EAGER_BEAVER: 1,
// Uses ranged attacks first, attacks furthest
// Uses ranged attacks first, minimizes movement, attacks furtherst
WARY_GARY: 2,
// Attacks character with lowest health
NELSON: 3,
NERVOUS_NELSON: 3,
// Attacks previous target, otherwise attacks closest
CARL: 4,
PERSISTENT_PAUL: 4,
// Attacks a random target in range
SHIA: 5,
SHIA_SURPRISE: 5,
// Attacks last token to attack mook
OLIVIA: 6,
REPRISING_OLIVIA: 6,
// Attacks first token to attack mook
CANDICE: 7,
VENGEFUL_CANDICE: 7,
// Attacks bigest source of personal damage
// Attacks bigest source of total damage
// Attacks token that last defeated an ally
// Attacks token that has defeated the most allies
MICHAEL: 12,
AVENGING_MICHAEL: 12,
// Pack tactics
A_REALLY_GOOD_DOG: 13,
GOOD_BOI: 13,
// Attacks next in turn order
ALLISON: 14,
ANALYTICAL_ALLISON: 14,

// Secret GM tech (don't use too many of these, or they'll catch on):
// Attacks healthiest
VEGETA: 9001,
Expand Down
27 changes: 15 additions & 12 deletions scripts/mook.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class Mook
{
this._token = token_;

if (! this._token)
throw new Abort(`Token with id ${token_.id} was not found`);

// Used to create Point objects
this._pointFactory = new PointFactory (metric_);
// Manages the mook's attempts at path planning
Expand Down Expand Up @@ -76,10 +79,11 @@ export class Mook
this.pathManager.clearAll ();

this._visibleTargets = game.combat.combatants.filter (combatant => {
const id = combatant.data.tokenId;
// Even mooks won't target themselves on purpose
if (combatant.tokenId === this.token.id) return false;
if (id === this.token.id) return false;

const token = canvas.tokens.get (combatant.tokenId);
const token = canvas.tokens.get (id);

// todo: add "factions" to allow targeting of npcs
if (! this.isPC (token)) return false;
Expand All @@ -91,7 +95,7 @@ export class Mook
if (this.mookModel.hasVision && ! this.canSee (token.id)) return false;

return true;
}).map (c => { return canvas.tokens.get (c.tokenId); });
}).map (c => { return canvas.tokens.get (c.data.tokenId); });

// Todo: compute paths between tokens when one moves and then select paths here.
for (let t of this.visibleTargets)
Expand Down Expand Up @@ -233,16 +237,14 @@ export class Mook
case (ActionType.MOVE):
if (this.debug)
console.log ("Moving from (%f, %f) to (%f, %f)",
this.point.x, this.point.y, action.data.x, action.data.y);
this.point.x, this.point.y, action.data.x, action.data.y);
await this.move (action.data);
break;
// todo? Find a use for this
// Open doors?
case (ActionType.EXPLORE):
if (this.isExploreDisabled)
this.handleFailure (new Abort ("Not taking turn. Mook found no targets and exploration is disabled."));

if (this.debug) console.log ("Exploring!?");
if (this.debug) console.log ("Exploring");

if (! this._isExplorer)
{
Expand Down Expand Up @@ -358,7 +360,7 @@ export class Mook
this.time -= action.cost ? action.cost : 0;
}

let str = "Unknown failure";
let str = "mookAI | Unknown failure";

if (tries <= 0)
str = "mookAI | Planning failure: forced exit after too many loops.";
Expand Down Expand Up @@ -416,7 +418,7 @@ export class Mook
return;
}

await this.token.update ({ rotation: (this.rotation + dTheta_) % 360 });
await this.tokenDoc.update ({ rotation: (this.rotation + dTheta_) % 360 });
await new Promise (resolve => setTimeout (resolve, this.rotationDelay));
}

Expand Down Expand Up @@ -459,7 +461,7 @@ export class Mook
let error = false;

await this.rotate (this.segment.radialDistToSegment (segment_, this.token.data.rotation, AngleTypes.DEG));
await this.token.update ({ x: segment_.point.px, y: segment_.point.py }).catch (err => {
await this.tokenDoc.update ({ x: segment_.point.px, y: segment_.point.py }).catch (err => {
ui.notifications.warn (err);
error = true;
});
Expand Down Expand Up @@ -507,7 +509,7 @@ export class Mook
if (this.tokenLocked === true)
return;

await this.token.update ({ lockRotation: true });
await this.tokenDoc.update ({ lockRotation: true });
this._disabledRotation = true;
}

Expand All @@ -516,7 +518,7 @@ export class Mook
if (! this._disabledRotation)
return;

await this.token.update ({ lockRotation: false });
await this.tokenDoc.update ({ lockRotation: false });
this._disabledRotation = false;
}

Expand Down Expand Up @@ -588,6 +590,7 @@ export class Mook
set time (speed_) { this._time = speed_; }

get token () { return this._token; }
get tokenDoc () { return game.scenes.active.tokens.get(this._token.id) }

get tokenLocked () { token.data.lockRotation; }

Expand Down
34 changes: 21 additions & 13 deletions scripts/mookAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,16 @@ export function initAI ()
});

Hooks.on ("ready", () => {
if (! mookAI.ready ())
mookAI = {};
try
{
if (! mookAI.ready ())
mookAI = {};
}
catch (e_)
{
console.log ("mookAI | Failed to initialize:")
console.log (e_)
}
});
}

Expand All @@ -183,23 +191,23 @@ export class MookAI
return false;
}

Hooks.on ("updateToken", (scene_, token_, changes_, diff_, sceneID_) => {
if (! diff_)
Hooks.on ("updateToken", (token_, changes_, diff_, sceneID_) => {
if (! diff_?.diff)
return;

this.updateTokens (changes_);
});

Hooks.on ("createCombatant", (combat_, combatant_, obj_, id_) => {
this.addCombatant (combat_.id, combatant_.tokenId);
Hooks.on ("createCombatant", (combatant_, config_, id_) => {
this.addCombatant (id_, combatant_.data.tokenId);
});
Hooks.on ("deleteCombatant", (combat_, combatant_, obj_, id_) => {
this.deleteCombatant (combat_, combatant_.tokenId);
Hooks.on ("deleteCombatant", (combatant_, config_, id_) => {
this.deleteCombatant (id_, combatant_.data.tokenId);
});
Hooks.on ("createCombat", (combat_, obj_, id_) => {
Hooks.on ("createCombat", (combat_, config_, id_) => {
this.combatStart (combat_);
});
Hooks.on ("deleteCombat", (combat_, obj_, id_) => {
Hooks.on ("deleteCombat", (combat_, config_, id_) => {
this.combatEnd (combat_);
});
Hooks.on ("updateScene", (...args) => { this.handleSceneChange () });
Expand Down Expand Up @@ -317,13 +325,13 @@ export class MookAI

let newMooks = new Map ();

combat_.combatants.forEach (element => {
const newToken = canvas.tokens.get (element.tokenId);
combat_.combatants.forEach (combatant => {
const newToken = canvas.tokens.get (combatant.data.tokenId);

if (! newToken)
return;

newMooks.set (element.tokenId, new Mook (newToken, this.metric));
newMooks.set (combatant.data.tokenId, new Mook (newToken, this.metric));
});

this._combats.set (combat_.id, newMooks);
Expand Down

0 comments on commit fb1712e

Please sign in to comment.