Skip to content

Instantly share code, notes, and snippets.

@jjtParadox
Last active January 22, 2022 19:01
Show Gist options
  • Save jjtParadox/5038bd2088bdcbfbea5d57c6153d577e to your computer and use it in GitHub Desktop.
Save jjtParadox/5038bd2088bdcbfbea5d57c6153d577e to your computer and use it in GitHub Desktop.
Reload Slug Weapon Macro for SW5E 1.5.2.R1-A9
// Copyright 2022 jjtParadox
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
function reload() {
let target = undefined;
if (canvas.tokens.controlled.length > 0) target = canvas.tokens.controlled[0].actor;
else if (game.user.character) target = game.user.character;
else if (canvas.tokens.ownedTokens.length === 1) target = canvas.tokens.ownedTokens[0].actor;
if (!target) {
ui.notifications.error("Must select one token to use");
return;
}
let reloadReg = /[rR]eload (\d+)/;
let weapons = target.items.filter(i => i.type === "weapon" && reloadReg.test(i.data.data.description.value) && i.data.data.consume?.type !== "charges");
let loadedAmmo = weapons.map(i => target.items.get(i.data.data.consume.target));
let ammoItems = target.items.filter(i => i.type === "consumable" && (i.data.data.consumableType === "ammo" || i.data.data.consumableType === "ammunition") && !loadedAmmo.some(j => j && j.data._id === i.data._id));
if (weapons.length === 0) {
ui.notifications.error("No reloadable weapons found in inventory for " + target.name);
return;
}
if (ammoItems.length === 0) {
ui.notifications.error("No unloaded ammo found in inventory for " + target.name);
return;
}
let weaponMax = weapons.map(i => parseInt(reloadReg.exec(i.data.data.description.value)[1]));
let weaponSelect = "";
for (let i = 0; i < weapons.length; i++) {
weaponSelect += `<option value="${i}">${weapons[i].name} (${loadedAmmo[i] ? loadedAmmo[i].data.data.quantity : 0}/${weaponMax[i]})</option>`;
}
let ammoSelect = "";
for (let i = 0; i < ammoItems.length; i++) {
ammoSelect += `<option value="${i}">${ammoItems[i].name} (${ammoItems[i].data.data.quantity})</option>`;
}
let htmlSelection = `
<form>
<div class="form-group">
<label>Weapon</label>
<select id="weapon-selector" name="weapon-selector">
${weaponSelect}
</select>
</div>
<div class="form-group">
<label>Ammo</label>
<select id="ammo-selector" name="ammo-selector">
${ammoSelect}
</select>
</div>`;
// Uncomment the following to enable reloading power cells. Weapons must not be configured to consume item charges
/*
htmlSelection += `
<div class="form-group">
<label class="checkbox">
<input type="checkbox" name="oneForAll" />One ammo item fully reloads weapon
</label>
</div
`;
*/
htmlSelection += `</form>`;
function reloadWeapon(weaponIdx, ammoIdx, oneForAll) {
let weaponSelected = weapons[weaponIdx];
let ammoSelected = ammoItems[ammoIdx];
let loadedAmmo = target.items.get(weaponSelected.data.data.consume.target);
if (oneForAll) {
ammoSelected.update({"data.quantity": ammoSelected.data.data.quantity - 1});
loadedAmmo.update({"data.quantity": weaponMax[weaponIdx]});
ChatMessage.create({content: `Fully reloaded ${weaponSelected.name} using one ${ammoSelected.name}.`, speaker: ChatMessage.getSpeaker({actor: target})});
} else {
let reloadAmnt = Math.min(ammoSelected.data.data.quantity, weaponMax[weaponIdx] - loadedAmmo.data.data.quantity);
ammoSelected.update({"data.quantity": ammoSelected.data.data.quantity - reloadAmnt});
loadedAmmo.update({"data.quantity": loadedAmmo.data.data.quantity + reloadAmnt});
ChatMessage.create({content: `Reloaded ${weaponSelected.name} using ${reloadAmnt} ${ammoSelected.name}.`, speaker: ChatMessage.getSpeaker({actor: target})});
}
}
new Dialog({
title: `${target.name}: Reload Weapon`,
content: htmlSelection,
buttons: {
confirm: {
icon: "<i class='fas fa-check'></i>",
label: `Reload`,
callback: htmlSelection => {
let weaponIdx = htmlSelection.find('[name="weapon-selector"]')[0].value;
let ammoIdx = htmlSelection.find('[name="ammo-selector"]')[0].value;
let oneForAll = false;
let findOneForAll = htmlSelection.find('[name="oneForAll"]');
if (findOneForAll.length > 0) oneForAll = findOneForAll[0].checked;
let weaponSelected = weapons[weaponIdx];
let loadedAmmoSel = loadedAmmo[weaponIdx];
let ammoSelected = ammoItems[ammoIdx];
if (ammoSelected.data.data.quantity <= 0) {
ui.notifications.error("Selected ammo item is empty");
return;
}
if (!loadedAmmoSel) {
if (weaponSelected.data.data.consume.type !== "ammo" && weaponSelected.data.data.consume.type !== "") {
ui.notifications.error("Weapon is configured to consume a different resource");
return;
}
let dataCopy = {consumableType: ammoSelected.data.data.consumableType};
if (!oneForAll) dataCopy = JSON.parse(JSON.stringify(ammoSelected.data.data));
dataCopy.quantity = 0;
target.createEmbeddedDocuments("Item", [{data: dataCopy, img: ammoSelected.data.img, name: `Loaded ${weaponSelected.name} Ammo (${ammoSelected.name})`, type: ammoSelected.data.type }]).then(item => {
weaponSelected.update({"data.consume": {amount: 1, target: item[0].id, type: "ammo"} }).then(() => reloadWeapon(weaponIdx, ammoIdx, oneForAll));
});
return;
}
if (loadedAmmoSel.type !== "consumable" || (loadedAmmoSel.data.data.consumableType !== "ammo" && loadedAmmoSel.data.data.consumableType !== "ammunition")) {
ui.notifications.error("Weapon is configured to consume a different resource");
return;
}
if (loadedAmmoSel.data.data.quantity >= weaponMax[weaponIdx]) {
ui.notifications.info("Weapon is already fully loaded");
return;
}
reloadWeapon(weaponIdx, ammoIdx, oneForAll);
}
}
}
}).render(true);
}
reload();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment