-
Notifications
You must be signed in to change notification settings - Fork 5
/
sta-initiative.js
162 lines (143 loc) · 4.95 KB
/
sta-initiative.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class STAInitiative {
static get MODULE_ID() { return "sta-initiative"; }
static shouldHideChatMessage(cm)
{
return cm?.flags?.core?.initiativeRoll;
}
static shouldCensorChatMessage(cm)
{
const speaker = cm.speaker;
if (!speaker)
return false;
const token = game.scenes.get(speaker.scene).tokens.get(speaker.token);
const result = !this.isTokenInfoVisible(token);
return result;
}
static isCombatantInfoVisible(combatant)
{
if (game.user.isGM)
return true;
else if (combatant._source.name)
return true; // manual display name override in CombatTracker
else
return this.isTokenInfoVisible(combatant.token);
}
static isTokenInfoVisible(token)
{
if (!token || game.user.isGM)
return true;
const mode = token.displayName;
if (mode === CONST.TOKEN_DISPLAY_MODES.NONE)
return false;
else if (mode === CONST.TOKEN_DISPLAY_MODES.CONTROL ||
mode === CONST.TOKEN_DISPLAY_MODES.OWNER_HOVER ||
mode === CONST.TOKEN_DISPLAY_MODES.OWNER)
return token.isOwner;
else
return true;
}
static requestRoll(combat)
{
if (this._queuedCombats.indexOf(combat) === -1)
{
this._queuedCombats.push(combat);
setTimeout(this._processRoll);
}
}
static _queuedCombats = [];
static _processRoll()
{
for (const combat of STAInitiative._queuedCombats)
{
combat.rollAll().then(() =>
{
if (!combat.started)
combat.startCombat();
});
}
STAInitiative._queuedCombats.length = 0;
}
}
// Hide or censor chat messages.
Hooks.on('renderChatMessage', (cm, jq) =>
{
if (STAInitiative.shouldHideChatMessage(cm))
{
jq[0].style.display = 'none';
}
else if (STAInitiative.shouldCensorChatMessage(cm))
{
const speaker = jq.find("header.message-header > h4.message-sender")[0];
if (speaker)
speaker.innerHTML = game.i18n.localize("COMBAT.UnknownCombatant");
const diceTarget = jq.find("div.message-content > div > div.dice-roll > div.dice-result > div.dice-formula > table.aim > tbody > tr > td:nth-child(2)")[0];
if (diceTarget)
diceTarget.innerHTML = "Target:?"; // appearently not localized in STA
}
});
Hooks.once('setup', () =>
{
// Fix formula for initiative to use the daring attribute, as stated in StarTrek Adventures core rules, rather than security discipline
CONFIG.Combat.initiative =
{
formula: '@attributes.daring.value',
decimals: 0
};
// Automatically reset activation and roll initiative for anyone joining the the encounter
libWrapper.register(STAInitiative.MODULE_ID, 'Combat.prototype._onCreateEmbeddedDocuments', function(wrapped, type, documents, result, options, userId)
{
wrapped(type, documents, result, options, userId);
if (game.user.isGM && type == "Combatant")
{
const module = CONFIG.LancerInitiative.module;
const updates = documents.map(c =>
{
// Ugly logic, but copy&paste from compressed/optimized LancerCombat.resetActivations to make sure it does the same
var _a, _b;
return {
_id: c.id,
[`flags.${module}.activations.value`]: this.settings.skipDefeated &&
(c.data.defeated ||
!!((_a = c.actor) === null || _a === void 0 ? void 0 : _a.effects.find(e => e.getFlag("core", "statusId") === CONFIG.Combat.defeatedStatusId)))
? 0
: (_b = c.activations.max) !== null && _b !== void 0 ? _b : 0
};
});
this.updateEmbeddedDocuments("Combatant", updates).then(() => STAInitiative.requestRoll(this));
}
}, 'WRAPPER');
// Eliminate new message notification icon, if the message is not visible.
libWrapper.register(STAInitiative.MODULE_ID, 'ChatLog.prototype.notify', function(wrapped, cm)
{
this._lastMessageTime = Date.now();
if (!STAInitiative.shouldHideChatMessage(cm))
wrapped(cm);
}, 'MIXED');
// Hide Combatant names in CombatTracker, if hidden in HUD
libWrapper.register(STAInitiative.MODULE_ID, 'Combatant.prototype.prepareDerivedData', function(wrapped)
{
wrapped();
if (!STAInitiative.isCombatantInfoVisible(this))
this.name = game.i18n.localize("COMBAT.UnknownCombatant");
}, 'WRAPPER');
// Hide Combatant initiative in CombatTracker, when not the GameMaster (works via CSS)
libWrapper.register(STAInitiative.MODULE_ID, 'CombatTracker.prototype.activateListeners', function(wrapped, html)
{
const tracker = html.find("#combat-tracker");
if (tracker.length == 1)
tracker[0].classList.add(game.user.isGM ? "sta-initiative-gm" : "sta-initiative-player");
return wrapped(html);
}, 'WRAPPER');
});
Hooks.once('ready', () =>
{
// Check dependencies
if (!game.modules.get('lib-wrapper')?.active && game.user.isGM)
{
ui.notifications.error("Module '" + STAInitiative.MODULE_ID + "' requires the 'libWrapper' module. Please install and activate it.");
}
if (!CONFIG?.LancerInitiative?.enable_initiative && game.user.isGM)
{
ui.notifications.error("Module '" + STAInitiative.MODULE_ID + "' requires the 'lancer-initiative' module to have its 'Enable Initative Rolling' option enabled. Please check settings of the 'lancer-initiative' module.");
}
});