This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtorch.js
347 lines (326 loc) · 13.2 KB
/
torch.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <shurd@FreeBSD.ORG> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Stephen Hurd
* ----------------------------------------------------------------------------
*/
class Torch {
static async addTorchButton(app, html, data) {
async function createDancingLights() {
let tkn = canvas.tokens.get(app.object.id);
let voff = tkn.h;
let hoff = tkn.w;
let c = tkn.center;
let v = game.settings.get("torch", "dancingLightVision")
await canvas.scene.createEmbeddedEntity("Token", [
{"actorData":{}, "actorId":tkn.actor._id, "actorLink":false, "bar1":{"attribute":""}, "bar2":{"attribute":""}, "brightLight":0, "brightSight":0, "dimLight":10, "dimSight":0, "displayBars":CONST.TOKEN_DISPLAY_MODES.NONE, "displayName":CONST.TOKEN_DISPLAY_MODES.HOVER, "disposition":CONST.TOKEN_DISPOSITIONS.FRIENDLY, "flags":{}, "height":1, "hidden":false, "img":"systems/dnd5e/icons/spells/light-air-fire-1.jpg", "lightAlpha":1, "lightAngle":360, "lockRotation":false, "mirrorX":false, "name":"Dancing Light", "randomimg":false, "rotation":0, "scale":0.25, "sightAngle":360, "vision":v, "width":1, "x":c.x - hoff, "y":c.y - voff},
{"actorData":{}, "actorId":tkn.actor._id, "actorLink":false, "bar1":{"attribute":""}, "bar2":{"attribute":""}, "brightLight":0, "brightSight":0, "dimLight":10, "dimSight":0, "displayBars":CONST.TOKEN_DISPLAY_MODES.NONE, "displayName":CONST.TOKEN_DISPLAY_MODES.HOVER, "disposition":CONST.TOKEN_DISPOSITIONS.FRIENDLY, "flags":{}, "height":1, "hidden":false, "img":"systems/dnd5e/icons/spells/light-air-fire-1.jpg", "lightAlpha":1, "lightAngle":360, "lockRotation":false, "mirrorX":false, "name":"Dancing Light", "randomimg":false, "rotation":0, "scale":0.25, "sightAngle":360, "vision":v, "width":1, "x":c.x, "y":c.y - voff},
{"actorData":{}, "actorId":tkn.actor._id, "actorLink":false, "bar1":{"attribute":""}, "bar2":{"attribute":""}, "brightLight":0, "brightSight":0, "dimLight":10, "dimSight":0, "displayBars":CONST.TOKEN_DISPLAY_MODES.NONE, "displayName":CONST.TOKEN_DISPLAY_MODES.HOVER, "disposition":CONST.TOKEN_DISPOSITIONS.FRIENDLY, "flags":{}, "height":1, "hidden":false, "img":"systems/dnd5e/icons/spells/light-air-fire-1.jpg", "lightAlpha":1, "lightAngle":360, "lockRotation":false, "mirrorX":false, "name":"Dancing Light", "randomimg":false, "rotation":0, "scale":0.25, "sightAngle":360, "vision":v, "width":1, "x":c.x - hoff, "y":c.y},
{"actorData":{}, "actorId":tkn.actor._id, "actorLink":false, "bar1":{"attribute":""}, "bar2":{"attribute":""}, "brightLight":0, "brightSight":0, "dimLight":10, "dimSight":0, "displayBars":CONST.TOKEN_DISPLAY_MODES.NONE, "displayName":CONST.TOKEN_DISPLAY_MODES.HOVER, "disposition":CONST.TOKEN_DISPOSITIONS.FRIENDLY, "flags":{}, "height":1, "hidden":false, "img":"systems/dnd5e/icons/spells/light-air-fire-1.jpg", "lightAlpha":1, "lightAngle":360, "lockRotation":false, "mirrorX":false, "name":"Dancing Light", "randomimg":false, "rotation":0, "scale":0.25, "sightAngle":360, "vision":v, "width":1, "x":c.x, "y":c.y}],
{"temporary":false, "renderSheet":false});
}
/*
* Returns the first GM id.
*/
function firstGM() {
let i;
for (i=0; i<game.users.entities.length; i++) {
if (game.users.entities[i].data.role >= 4 && game.users.entities[i].active)
return game.users.entities[i].data._id;
}
ui.notifications.error("No GM available for Dancing Lights!");
}
async function sendRequest(req) {
req.sceneId = canvas.scene._id
req.tokenId = app.object.id;
if (!data.isGM) {
req.addressTo = firstGM();
game.socket.emit("module.torch", req);
}
else {
Torch.handleSocketRequest(req);
}
}
/*
* Returns true if a torch can be used... ie:
* 1) If the user is the GM.
* 2) If the system is not dnd5e, and the playerTorches setting is enabled.
* 3) If a dnd5e player knows the Light spell.
* 4) if a dnd5e player has at least one torch in inventory
*/
function hasTorch() {
let torches = null;
if (game.system.id !== 'dnd5e') {
if (game.settings.get("torch", "playerTorches"))
torches = 'Player';
if (data.isGM)
torches = 'GM';
}
else {
let actor = game.actors.get(data.actorId);
if (actor === undefined)
return false;
actor.data.items.forEach(item => {
if (item.type === 'spell') {
if (item.name === 'Light') {
torches = 'Light';
return;
}
if (item.name === 'Dancing Lights') {
torches = 'Dancing Lights';
return;
}
}
else {
if (torches === null) {
var itemToCheck = game.settings.get("torch", "gmInventoryItemName");
if (item.name.toLowerCase() === itemToCheck.toLowerCase()) {
if (item.data.quantity > 0) {
torches = itemToCheck;
return;
}
}
}
}
});
if (torches === null && data.isGM)
torches = 'GM';
}
return torches;
}
/*
* Performs inventory tracking for torch uses. Deducts one
* torch from inventory if all of the following are true:
* 1) The system is dnd5e.
* 2) The player doesn't know the Light spell.
* 3) The player has at least one torch.
* 4) The user is not the GM or the gmUsesInventory setting is enabled.
*/
async function useTorch() {
let torch = -1;
if (data.isGM && !game.settings.get("torch", "gmUsesInventory"))
return;
if (game.system.id !== 'dnd5e')
return;
let actor = game.actors.get(data.actorId);
if (actor === undefined)
return;
// First, check for the light cantrip...
actor.data.items.forEach((item, offset) => {
if (item.type === 'spell') {
if (item.name === 'Light') {
torch = -2;
return;
}
if (item.name === 'Dancing Lights') {
torch = -3;
return;
}
}
else {
var itemToCheck = game.settings.get("torch", "gmInventoryItemName");
if (torch === -1 && item.name.toLowerCase() === itemToCheck.toLowerCase() && item.data.quantity > 0) {
torch = offset;
}
}
});
if (torch < 0)
return;
// Now, remove a torch from inventory...
await actor.updateOwnedItem({"_id": actor.data.items[torch]._id, "data.quantity": actor.data.items[torch].data.quantity - 1});
}
// Don't let Dancing Lights have/use torches. :D
if (data.name === 'Dancing Light' &&
data.dimLight === 20 &&
data.brightLight === 10) {
return;
}
if (data.isGM === true || game.settings.get("torch", "playerTorches") === true) {
let dimRadius = game.settings.get("torch", "dimRadius");
let brightRadius = game.settings.get("torch", "brightRadius");
let tbutton = $(`<div class="control-icon torch"><i class="fas fa-fire"></i></div>`);
let allowEvent = true;
let ht = hasTorch();
let oldTorch = app.object.getFlag("torch", "oldValue");
let newTorch = app.object.getFlag("torch", "newValue");
// Clear torch flags if light has been changed somehow.
if (newTorch !== undefined && newTorch !== null && newTorch !== 'Dancing Lights' && (newTorch !== data.brightLight + '/' + data.dimLight)) {
await app.object.setFlag("torch", "oldValue", null);
await app.object.setFlag("torch", "newValue", null);
oldTorch = null;
newTorch = null;
}
if (newTorch !== undefined && newTorch !== null) {
// If newTorch is still set, light hasn't changed.
tbutton.addClass("active");
}
else if ((data.brightLight >= brightRadius && data.dimLight >= dimRadius && ht !== 'Dancing Lights') || ht === null) {
/*
* If you don't have a torch, *or* you're already emitting more light than a torch,
* disallow the torch button
*/
let disabledIcon = $(`<i class="fas fa-slash" style="position: absolute; color: tomato"></i>`);
tbutton.addClass("fa-stack");
tbutton.find('i').addClass('fa-stack-1x');
disabledIcon.addClass('fa-stack-1x');
tbutton.append(disabledIcon);
allowEvent = false;
}
html.find('.col.left').prepend(tbutton);
if (allowEvent) {
tbutton.find('i').click(async (ev) => {
let btn = $(ev.currentTarget.parentElement);
let dimRadius = game.settings.get("torch", "dimRadius");
let brightRadius = game.settings.get("torch", "brightRadius");
let oldTorch = app.object.getFlag("torch", "oldValue");
let newTorch = app.object.getFlag("torch", "newValue");
ev.preventDefault();
ev.stopPropagation();
if (ev.ctrlKey) { // Forcing light off...
data.brightLight = game.settings.get("torch", "offBrightRadius");
data.dimLight = game.settings.get("torch", "offDimRadius");
await app.object.setFlag("torch", "oldValue", null);
await app.object.setFlag("torch", "newValue", null);
await sendRequest({"requestType": "removeDancingLights"});
btn.removeClass("active");
}
else if (oldTorch === null || oldTorch === undefined) { // Turning light on...
await app.object.setFlag("torch", "oldValue", data.brightLight + '/' + data.dimLight);
if (ht === 'Dancing Lights') {
await createDancingLights();
await app.object.setFlag("torch", "newValue", 'Dancing Lights');
}
else {
if (brightRadius > data.brightLight)
data.brightLight = brightRadius;
if (dimRadius > data.dimLight)
data.dimLight = dimRadius;
await app.object.setFlag("torch", "newValue", data.brightLight + '/' + data.dimLight);
}
btn.addClass("active");
useTorch();
}
else { // Turning light off...
if (newTorch === 'Dancing Lights') {
await sendRequest({"requestType": "removeDancingLights"});
}
else {
let thereBeLight = oldTorch.split('/');
data.brightLight = parseFloat(thereBeLight[0]);
data.dimLight = parseFloat(thereBeLight[1]);
}
await app.object.setFlag("torch", "newValue", null);
await app.object.setFlag("torch", "oldValue", null);
btn.removeClass("active");
}
await app.object.update({brightLight: data.brightLight, dimLight: data.dimLight});
});
}
}
}
static async handleSocketRequest(req) {
if (req.addressTo === undefined || req.addressTo === game.user._id) {
let scn = game.scenes.get(req.sceneId);
let tkn = scn.data.tokens.find(({_id}) => _id === req.tokenId);
let dltoks=[];
switch(req.requestType) {
case 'removeDancingLights':
scn.data.tokens.forEach(tok => {
if (tok.actorId === tkn.actorId &&
tok.name === 'Dancing Light' &&
tok.dimLight === 20 &&
tok.brightLight === 10) {
//let dltok = canvas.tokens.get(tok._id);
dltoks.push(scn.getEmbeddedEntity("Token", tok._id)._id);
}
});
await scn.deleteEmbeddedEntity("Token", dltoks);
break;
}
}
}
}
Hooks.on('ready', () => {
Hooks.on('renderTokenHUD', (app, html, data) => { Torch.addTorchButton(app, html, data) });
Hooks.on('renderControlsReference', (app, html, data) => {
html.find('div').first().append('<h3>Torch</h3><ol class="hotkey-list"><li><h4>'+
game.i18n.localize("torch.turnOffAllLights")+
'</h4><div class="keys">'+
game.i18n.localize("torch.holdCtrlOnClick")+
'</div></li></ol>');
});
game.socket.on("module.torch", request => {
Torch.handleSocketRequest(request);
});
});
Hooks.once("init", () => {
game.settings.register("torch", "playerTorches", {
name: game.i18n.localize("torch.playerTorches.name"),
hint: game.i18n.localize("torch.playerTorches.hint"),
scope: "world",
config: true,
default: true,
type: Boolean
});
if (game.system.id === 'dnd5e') {
game.settings.register("torch", "gmUsesInventory", {
name: game.i18n.localize("torch.gmUsesInventory.name"),
hint: game.i18n.localize("torch.gmUsesInventory.hint"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("torch", "gmInventoryItemName", {
name: game.i18n.localize("torch.gmInventoryItemName.name"),
hint: game.i18n.localize("torch.gmInventoryItemName.hint"),
scope: "world",
config: true,
default: "torch",
type: String
});
}
game.settings.register("torch", "brightRadius", {
name: game.i18n.localize("LIGHT.LightBright"),
hint: game.i18n.localize("torch.brightRadius.hint"),
scope: "world",
config: true,
default: 20,
type: Number
});
game.settings.register("torch", "dimRadius", {
name: game.i18n.localize("LIGHT.LightDim"),
hint: game.i18n.localize("torch.dimRadius.hint"),
scope: "world",
config: true,
default: 40,
type: Number
});
game.settings.register("torch", "offBrightRadius", {
name: game.i18n.localize("torch.offBrightRadius.name"),
hint: game.i18n.localize("torch.offBrightRadius.hint"),
scope: "world",
config: true,
default: 0,
type: Number
});
game.settings.register("torch", "offDimRadius", {
name: game.i18n.localize("torch.offDimRadius.name"),
hint: game.i18n.localize("torch.offDimRadius.hint"),
scope: "world",
config: true,
default: 0,
type: Number
});
game.settings.register("torch", "dancingLightVision", {
name: game.i18n.localize("torch.dancingLightVision.name"),
hint: game.i18n.localize("torch.dancingLightVision.hint"),
scope: "world",
config: true,
default: false,
type: Boolean
});
});
console.log("--- Flame on!");