Skip to content

Commit

Permalink
Merge pull request #16 from DanielBoettner/12-bug-quantity-formula
Browse files Browse the repository at this point in the history
12 bug quantity formula
  • Loading branch information
DanielBoettner authored Oct 19, 2021
2 parents 72bfd39 + 74661bb commit db83ae0
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 202 deletions.
7 changes: 4 additions & 3 deletions hooks/onCreateToken.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { LootPopulator } from '../scripts/populateLoot.js';

export let initHooks = () => {
Hooks.on('createToken', (token, createData, options, userId) => {

game.LootPopulator = new LootPopulator();

Hooks.on('createToken', (token, createData, options, userId) => {
// only act on tokens dropped by the GM
if (!game.user.isGM) return token;
if (!game.settings.get("lootpopulatornpc5e", "autoPopulateTokens")) return token;
Expand All @@ -19,7 +21,6 @@ export let initHooks = () => {
return token;
}

let lootPopulator = new LootPopulator(token);
lootPopulator.populateToken();
game.LootPopulator.populate(token);
});
};
29 changes: 26 additions & 3 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
"name": "lootpopulatornpc5e",
"title": "Loot populater NPC | 5e",
"description": "A module to automatically populate loot on monsters and npcs when placed on the map.",
"version": "0.2.2",
"version": "0.3.1",
"minimumCoreVersion": "0.8.5",
"compatibleCoreVersion": "0.8.8",
"author": "Daniel Böttner",
"compatibleCoreVersion": "0.8.9",
"authors":[
{
"name": "Daniel Böttner | JackPrince#0494",
"url": "https://github.com/DanielBoettner/"
}
],
"systems": ["dnd5e"],
"dependencies": [
{
"name": "dnd5e",
"type": "system"
},
{
"name": "lootsheetnpc5e"
}
],
"includes": [
"./hooks/**",
"./scripts/**"
Expand All @@ -16,6 +30,15 @@
],
"styles": [],
"socket": false,
"packs": [
{
"name": "loot-populator-macros",
"label": "Loot Populator Macros",
"system": "dnd5e",
"path": "./packs/macros.db",
"entity": "Macro"
}
],
"url": "https://github.com/DanielBoettner/fvtt-loot-populator-npc-5e",
"manifest": "https://raw.githubusercontent.com/DanielBoettner/fvtt-loot-populator-npc-5e/master/module.json",
"download": "https://github.com/DanielBoettner/fvtt-loot-populator-npc-5e/archive/master.zip"
Expand Down
3 changes: 3 additions & 0 deletions packs/macros.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"name":"toggleTokenAutoPopulation","type":"script","author":"rKaPA9z0omV5p2Qt","img":"icons/commodities/tech/lever.webp","scope":"global","command":"/**\n * Simple Macro to toggle the state of a boolean setting.\n **/\nlet module = 'lootpopulatornpc5e',\n key = 'autoPopulateTokens';\n \ngame.settings.set(module,key,!game.settings.get(module,key));\nui.notifications.info('The setting <b>' + key + '</b> was changed to: <b>' + !game.settings.get(module,key) + '</b>');","folder":null,"sort":0,"permission":{"default":0,"rKaPA9z0omV5p2Qt":3},"flags":{"combat-utility-belt":{"macroTrigger":""},"advanced-macros":{"runAsGM":false},"core":{"sourceId":"Macro.KnGdlkChHPPNClhx"}},"_id":"IfSKfuB5qqPUiTaf"}
{"name":"populateLoot","type":"script","author":"rKaPA9z0omV5p2Qt","img":"icons/skills/trades/farming-plant-seeds-gray.webp","scope":"global","command":"// no token given defaults to currently selected tokens\ngame.LootPopulator.populate();","folder":null,"sort":0,"permission":{"default":0,"rKaPA9z0omV5p2Qt":3},"flags":{"combat-utility-belt":{"macroTrigger":""},"advanced-macros":{"runAsGM":false},"core":{"sourceId":"Macro.xAhxp90zTM54uT9z"}},"_id":"Ab23KwPq9VD4C2yn"}
{"name":"setLightOnTokens","type":"script","author":"rKaPA9z0omV5p2Qt","img":"icons/commodities/tech/levers-colored.webp","scope":"global","command":"let presets = {\n darkvision: {\n vision: true,\n dimSight: 60,\n brightSight: 0,\n dimLight: 0,\n brightLight: 0,\n lightAngle: 360,\n lockRotation: false,\n lightColor: \"\",\n lightAlpha: 0.2,\n },\n light_cantrip: {\n vision: true,\n dimSight: 1,\n brightSight: 0,\n dimLight: 40,\n brightLight: 20,\n lightAngle: 360,\n lockRotation: false,\n lightAnimation: {type: \"torch\", intensity: 1, speed: 1},\n lightColor: \"#bbbbff\",\n lightAlpha: 0.2,\n },\n acetylene_lantern_spotlight: {\n vision: true,\n dimSight: 1,\n brightSight: 0,\n dimLight: 120,\n brightLight: 60,\n lightAngle: 55,\n lockRotation: false,\n lightAnimation: {type: \"pulse\", intensity: 2, speed: 3},\n lightColor: \"#ffee99\",\n lightAlpha: 0.2,\n },\n acetylene_lantern_narrow: {\n vision: true,\n dimSight: 1,\n brightSight: 0,\n dimLight: 90,\n brightLight: 45,\n lightAngle: 100,\n lockRotation: false,\n lightAnimation: {type: \"pulse\", intensity: 2, speed: 3},\n lightColor: \"#ffee99\",\n lightAlpha: 0.2,\n },\n acetylene_lantern_wide: {\n vision: true,\n dimSight: 1,\n brightSight: 0,\n dimLight: 60,\n brightLight: 30,\n lightAngle: 360,\n lockRotation: false,\n lightAnimation: {type: \"pulse\", intensity: 2, speed: 3},\n lightColor: \"#ffee99\",\n lightAlpha: 0.2,\n },\n acetylene_lantern_dim: {\n vision: true,\n dimSight: 1,\n brightSight: 0,\n dimLight: 30,\n brightLight: 15,\n lightAngle: 360,\n lockRotation: false,\n lightAnimation: {type: \"pulse\", intensity: 2, speed: 3},\n lightColor: \"#ffee99\",\n lightAlpha: 0.2,\n },\n torch: {\n vision: true,\n dimSight: 1,\n brightSight: 0,\n dimLight: 20,\n brightLight: 10,\n lightAngle: 360,\n lockRotation: false,\n lightAnimation: {type: \"torch\", intensity: 5, speed: 5},\n lightColor: \"#ffbb33\",\n lightAlpha: 0.2,\n },\n normal_vision: {\n vision: true,\n dimSight: 5,\n brightSight: 0,\n dimLight: 0,\n brightLight: 0,\n lightAngle: 360,\n lockRotation: false,\n lightColor: \"\",\n lightAlpha: 0.2,\n },\n no_vision: {\n vision: false,\n }\n};\n\nlet vision_buttons = [];\nfor (let key in presets) {\n vision_buttons.push({\n label: key.replaceAll('_',' '),\n callback: async function() {\n updateTokens(presets[key]);\n }\n });\n}\n\nnew Dialog({\n title: `Select a vision option`,\n content: ``,\n buttons: vision_buttons,\n //default: \"yes\",\n close: html => {}\n}).render(true);\n\nasync function updateTokens(preset){\n const updates = canvas.tokens.controlled.map(t => mergeObject({_id: t.id}, preset));\n await canvas.scene.updateEmbeddedDocuments(\"Token\", updates);\n}","folder":null,"sort":0,"permission":{"default":0,"rKaPA9z0omV5p2Qt":3},"flags":{"combat-utility-belt":{"macroTrigger":""},"advanced-macros":{"runAsGM":false},"core":{"sourceId":"Macro.PBzvJUvRseFBUDAL"}},"_id":"ZWRIblJrmgW5qv9l"}
44 changes: 28 additions & 16 deletions scripts/modules/currency.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ class Currency {
* @param useBTR
*/
static async _handleCurrency(
actor,
token,
flags
) {
if (flags.generateCurrency && flags.lootCurrencyDefault) {
let lootCurrencyString = flags.lootCurrencyDefault;

if (flags.useBetterRolltables) {
lootCurrencyString = flags.brt_rt_tcs || lootCurrencyString;
}
/**
* If we use betterRolltable and the currencyString of BRT is not empty
* the currency was already populated.
*/
if (
flags.useBetterRolltables &&
(flags.brt_rt_tcs || flags.brt_rt_tcs.length > 0 )
){
return;
}

await Currency.addCurrenciesToActor(
actor,
await Currency.addCurrenciesToToken(
token,
Currency._generateCurrency(lootCurrencyString),
flags.adjustCurrency
);
Expand Down Expand Up @@ -58,28 +65,33 @@ class Currency {

/**
* Expects and actor and an array
* @param {Actor} actor
* @param {Actor} token
* @param {Array|string} lootCurrency
* @param {boolean} adjutsByCR
*/
static async addCurrenciesToActor(actor, lootCurrency, adjutsByCR = false) {
let currencyData = duplicate(actor.data.data.currency),
cr = actor.data.data.details.cr || 0,
amount = 0;
static async addCurrenciesToToken(token, lootCurrency, adjutsByCR = false) {
const currencyDataInitial = {cp: 0, ep: 0, gp: 0, pp: 0,sp: 0};
let currencyData = currencyDataInitial,
amount = 0;

if (token.data.actorData.data?.currency) {
currencyData = duplicate(token.data.actorData.data.currency);
}

for (var key in lootCurrency) {
for (let key in lootCurrency) {
if (currencyData.hasOwnProperty(key)) {
if(adjutsByCR){
amount = Number(currencyData[key].value || 0) + Math.ceil(cr) + Number(lootCurrency[key]);
let cr = game.actors.find(actor => actor._id === token.data.actorId).data.data.details.cr || 0;
amount = Number(currencyData[key] || 0) + Number(Math.ceil(cr * lootCurrency[key]));
} else {
amount = Number(currencyData[key].value || 0) + Number(lootCurrency[key]);
amount = Number(currencyData[key] || 0) + Number(lootCurrency[key]);
}

currencyData[key] = {"value": amount.toString()};
currencyData[key] = amount;
}
}

await actor.update({"data.currency": currencyData});
await token.update({ 'actorData.data.currency': currencyData });
}

/**
Expand Down
Loading

0 comments on commit db83ae0

Please sign in to comment.