Skip to content

Generate hex, npc, and weather macros not working on v12 #59

Description

@fmccready

First, thanks for making this awesome module! The character sheets are amazing!

The generate hex, npc, and weather macros are not working on Foundry v12. I have fixed the macros but I am not sure the best way to update them in this repo because they are stored in the compendium database files. I can make the compendium updates and commit the changes to the database files but that might not be easy to review so I am looking for a little guidance on how to contribute the fixes.

Here are the updated scripts that I am using for myself. I did not write any of this code I only made a couple of small edits to make it work with Foundry V12.

Generate Hex

//Generate Hex

/*
source: https://raw.githubusercontent.com/brunocalado/mestre-digital/master/Foundry%20VTT/Macros/Sistemas%20Diversos/Mausritter-Hex.js
*/

const version = '1.0';
const compendium_label = 'Tables';

(async () => {

  let hexTypeList = ['Random', 'Countryside', 'Forest', 'River', 'Human Town'];
  let selectList = "";

  hexTypeList.forEach(option => selectList += "<option value='" + option + "'>" + option + "</option>")

  let d = new Dialog({
    title: "Select Type",
    content: "<h2> Select or Roll Hex Type </h2> <select style='margin-bottom:10px;'name='stat' id='stat'> " + selectList + "</select> <br/>",
    buttons: {
      roll: {
        icon: '<i class="fas fa-check"></i>',
        label: "Roll",
        callback: (html) => hexType(html.find('[id=\"stat\"]')[0].value)
      },
      cancel: {
        icon: '<i class="fas fa-times"></i>',
        label: "Cancel",
        callback: () => { }
      }
    },
    default: "roll",
    close: () => { }
  });
  d.render(true);

})()

async function hexType(hexType) {
  let hexContents = "";
  let hexDetails = "";
  let buffer = '';
  let randomHex = ['Countryside', 'Forest', 'River', 'Human Town'];

  if (hexType == "Random") {
    const randomElement =
      hexType = randomHex[Math.floor(Math.random() * randomHex.length)];
  }

  buffer = await drawFromTable("Hex - " + hexType);
  buffer = await buffer.roll();
  hexContents = buffer.results[0].text;

  buffer = await drawFromTable("Hex - Landmark Details");
  buffer = await buffer.roll();
  hexDetails = buffer.results[0].text;


  let message = "\
  <h2> "+ hexType + ": </h2>\
  <b> Landmark: </b>"+ hexContents + "</br>\
  <b> Details: </b><i>"+ hexDetails + "</i></br>";

  let chatData = {
    content: message,
    whisper: ChatMessage.getWhisperRecipients("GM")
  };

  ChatMessage.create(chatData);

}

/* Functions */
async function drawFromTable(tableName) {
  let list_compendium = await game.packs.filter(p => p.documentName == 'RollTable');
  let inside = await list_compendium.filter(p => p.metadata.label == compendium_label)[0].getDocuments();
  let table = await inside.filter(p => p.name == tableName)[0];

  if (!table) {
    ui.notifications.warn(`Table ${tableName} not found.`, {});
    return;
  }
  return await table;
}

Generate NPC

/* Instant NPC - v0.3

Source: https://raw.githubusercontent.com/brunocalado/mestre-digital/master/Foundry%20VTT/Macros/Sistemas%20Diversos/Mausritter-InstantNPC.js
Icon: systems/mausritter/images/sample/Portrait_Rat.png
*/
//test stuff: console.log(canvas.tokens.controlled[0].actor);

const compendium_label = 'Tables';

(async () => {
  const Appearance = await drawFromTable('Non-player mice - Appearance');
  const Birthsign = await drawFromTable('Non-player mice - Birthsign and Disposition');
  const Quirk = await drawFromTable('Non-player mice - Quirk');
  const Social = await drawFromTable('Non-player mice - Social position and Payment for service');
  const Wants = await drawFromTable('Non-player mice - Wants');
  const Relationship = await drawFromTable('Non-player mice - Relationship');

  const Birthname = await drawFromTable('Mousy Names - Birthname');
  const Matriname = await drawFromTable('Mousy Names - Matriname');

  const dexterity = await attrRoll();
  const strength = await attrRoll();
  const will = await attrRoll();
  const health = await healthRoll();

  let msg = '';
  msg += `<p><b>Social position and Payment for service:</b> ${Social}</p>`;
  msg += `<p><b>Appearance:</b> ${Appearance}</p>`;
  msg += `<p><b>Quirk:</b> ${Quirk}</p>`;
  msg += `<p><b>Wants:</b> ${Wants}</p>`;
  msg += `<p><b>Relationship:</b> ${Relationship}</p>`;

  //let npchp = randomHP(4,10);
  let instantNPC = await Actor.create({
    name: Birthname + " " + Matriname,
    type: "hireling",
    system: {
      description: {
        disposition: Birthsign
      },
      notes: msg,
      stats: {
        dexterity: {
          max: dexterity,
          value: dexterity
        },
        strength: {
          max: strength,
          value: strength
        },
        will: {
          max: will,
          value: will
        }
      },
      health: {
        max: health,
        value: health
      }
    }
  });

  await instantNPC.sheet.render(true);

})()

/* Functions */
async function drawFromTable(tableName) {
  let list_compendium = await game.packs.filter(p => p.documentName == 'RollTable');
  let inside = await list_compendium.filter(p => p.metadata.label == compendium_label)[0].getDocuments();
  let table = await inside.filter(p => p.name == tableName)[0];

  if (!table) {
    ui.notifications.warn(`Table ${tableName} not found.`, {});
    return;
  }

  let buffer = await table.roll();

  return buffer.results[0].text;
}

function treasureCoins(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

async function attrRoll() {
  const roll = new Roll('2d6')
  await roll.evaluate()
  return roll.total;
}

async function healthRoll() {
  const roll = new Roll('1d6')
  await roll.evaluate()
  return roll.total;
}

function randomHP(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

Generate Weather

//Generate Weather

/*
source: https://raw.githubusercontent.com/brunocalado/mestre-digital/master/Foundry%20VTT/Macros/Sistemas%20Diversos/Mausritter-Weather.js
*/

const version = '1.1';
const compendium_label = 'Tables';

(async () => {

    // const summer = await drawFromTable('Weather Summer');
    // const autumn = await drawFromTable('Weather Autumn');
    // const winter = await drawFromTable('Weather Winter');
    // const spring = await drawFromTable('Weather Spring');
    
    let seasonList = ['Summer', 'Autumn', 'Winter', 'Spring'];
    let selectList = "";

    seasonList.forEach(option => selectList += "<option value='" + option + "'>" + option + "</option>")

    let d = new Dialog({
        title: "Select Season",
        content: "<h2> Season </h2> <select style='margin-bottom:10px;'name='stat' id='stat'> " + selectList + "</select> <br/>",
        buttons: {
            roll: {
                icon: '<i class="fas fa-check"></i>',
                label: "Roll",
                callback: (html) => drawFromTable('Weather '+html.find('[id=\"stat\"]')[0].value)
            },
            cancel: {
                icon: '<i class="fas fa-times"></i>',
                label: "Cancel",
                callback: () => { }
            }
        },
        default: "roll",
        close: () => { }
    });
    d.render(true);

})()

/* Functions */
async function drawFromTable(tableName) {
  let list_compendium = await game.packs.filter(p=>p.documentName=='RollTable');      
  let inside = await list_compendium.filter( p=>p.metadata.label==compendium_label)[0].getDocuments();      
  let table = await inside.filter( p=>p.name==tableName )[0];
  
  if (!table) {
    ui.notifications.warn(`Table ${tableName} not found.`, {});
    return;
  }
  
  let weather = await table.roll(); 
  weather = weather.results[0].text;  
  let message = "<h2> Today's Weather: </h2><b style='font-size:120%;'>"+weather+"</br>";
  
  let chatData = {
    content: message,
    whisper : ChatMessage.getWhisperRecipients("GM")
  };  

  ChatMessage.create(chatData);  
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions