-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPaladin Aura.js
106 lines (98 loc) · 3.37 KB
/
Paladin Aura.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/// this will search for a token with the feature "Aura of Protection" and create/update active effects on all PC class actors within 10ft of that token
//run on startup/refresh
//ONLY RUN ONCE
ui.notifications.info("Aura of Protection Active");
function createPlayerTable() {
let playerTable = [];
let playerCount = 0;
for (let i = 0; i < canvas.tokens.placeables.length; i++) {
let token = canvas.tokens.placeables[i];
let actor = game.actors.get(token.actor.data._id);
if (actor.isPC == true) {
playerTable.push([]);
playerTable[playerCount].push(actor);
playerCount++;
}
}
return playerTable;
}
function auraCheck(playerTable, index) {
var auraName = "Aura of Protection";
for (let j = 0; j < playerTable[index][0].data.items.length; j++) {
var abilityName = playerTable[index][0].data.items[j].name;
var result = auraName.localeCompare(abilityName);
if (result == 0)
return true;
}
return false;
}
function inRange(token1, token2) {
let gs = canvas.grid.size;
let d1 = Math.abs((token1[0]._validPosition.x - token2[0]._validPosition.x) / gs);
let d2 = Math.abs((token1[0]._validPosition.y - token2[0]._validPosition.y) / gs);
let dist = Math.max(d1, d2);
dist = dist * canvas.scene.data.gridDistance;
if (dist <= 10)
return true;
return false;
}
function newSave(actor2, playerTable) {
let auraTable = [];
let saveBonus = 0;
for (let i = 0; i < playerTable.length; i++) {
let actor1 = playerTable[i][0];
let actorHasAura = auraCheck(playerTable, i);
if (actorHasAura == true) {
let auraSource = game.actors.get(actor1._id);
auraTable.push(auraSource);
}
}
let token2 = game.actors.get(actor2._id).getActiveTokens();
for (let k = 0; k < auraTable.length; k++) {
if (inRange(auraTable[k].getActiveTokens(), token2) == true) {
if (auraTable[k].data.data.abilities.cha.mod >= saveBonus) {
saveBonus = auraTable[k].data.data.abilities.cha.mod;
}
}
}
return saveBonus;
}
function createActiveEffect(actor, newSave) {
console.log(newSave)
let aura = actor.effects.find(i => i.data.label === "Aura of Protection");
if (aura !== null) {
let changes = aura.data.changes;
changes[0].value = newSave
aura.update({ changes });
} else {
let effectData = {
label: "Aura of Protection",
icon: "",
changes: [{
"key": "data.bonuses.abilities.save",
"mode": 2,
"value": newSave,
"priority": "20"
}]
}
actor.createEmbeddedEntity("ActiveEffect", effectData);
}
}
function updateActor() {
let playerTable = createPlayerTable();
for (let i = 0; i < playerTable.length; i++) {
let actor2 = playerTable[i][0];
let token2 = playerTable[i][1];
let save = newSave(actor2, playerTable)
let actor = game.actors.get(actor2._id)
console.log("Aura update")
createActiveEffect(actor, save)
}
return;
}
Hooks.on("updateToken", (scene, token, update, flags, id) => {
let movement = getProperty(update, "x") || getProperty(update, "y");
if (movement !== undefined) {
updateActor();
}
})