Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.03 KB | None | 0 0
  1. let figureWounds = function(attributes,setObj){
  2.     let hpPercent = attributes.hp/attributes.hp_max,
  3.         woundsObj = {};
  4.     if(hpPercent > 0.5){
  5.         woundsObj.inconscient = 0;
  6.         woundsObj.blessure = 0;
  7.         woundsObj.blessure_grave = 0;
  8.     }else if(hpPercent > 0.25){
  9.         woundsObj.inconscient = 0;
  10.         woundsObj.blessure = 1;
  11.         woundsObj.blessure_grave = 0;
  12.     }else if(hpPercent > 0){
  13.         woundsObj.inconscient = 0;
  14.         woundsObj.blessure = 1;
  15.         woundsObj.blessure_grave = 1;
  16.     }else{
  17.         woundsObj.inconscient = 1;
  18.         woundsObj.blessure = 1;
  19.         woundsObj.blessure_grave = 1
  20.     }
  21.     Object.assign(setObj,woundsObj);//Because objects are passed as a reference in javascript. Any changes made in this function will affect the original version of setObj, so we don't need to return anything.
  22. };
  23.  
  24. on('change:body change:mass_scale change:mind change:will change:agility',(event)=>{
  25.     getAttrs(['body','mass_scale','mind','will','agility','hp'],(attributes)=>{
  26.         let setObj = {
  27.                 hp_max:(10*attributes.body||10) + (10*attributes.mass_scale||10) + (attributes.body*1||1) + (attributes.mind*1||1) + (attributes.will*1||1) + (attributes.agility*1||1)
  28.             };
  29.         attributes.hp_max = setObj.hp_max;
  30.         figureWounds(attributes,setObj);
  31.         setAttrs(setObj,{silent:true});
  32.     });
  33. });
  34. on('change:hp change:hp_max',(event)=>{
  35.     getAttrs(['hp','hp_max'],(attributes)=>{
  36.         let setObj = {};
  37.         figureWounds(attributes,setObj);
  38.         setAttrs(setObj,{silent:true});
  39.     });
  40. });
  41.  
  42. const woundlvls = {blessure: 1, blessure_grave: 2, inconscient: 9};
  43. const woundnames = Object.keys(woundlvls);
  44. const woundPenalty = wounds => {
  45.     const penalty = wounds.reduce((total, current, index) => {
  46.             return total + current * woundlvls[woundnames[index]];
  47.         },0 );
  48.     return penalty >= 9 ? 0: (penalty > 2 ? 2 : penalty);
  49. };
  50.  
  51. // this one updates wound penalties when you manually click a wound box
  52. on(woundnames.map(wound => `change:${wound}`).join(' '), function() {
  53.     getAttrs(woundnames, function(v) {
  54.         let wounds = woundnames.map(lvl => +v[lvl] || 0);
  55.         let penalty = woundPenalty(wounds);
  56.         setAttrs({
  57.             hurtlvl: penalty
  58.         }, {silent: true});
  59.     });
  60. });
  61. on('change:woundlvl', function(event) {
  62.     getAttrs([...woundnames, 'woundlvl'], function(v) {
  63.         // iterate through hurt levels, build an array of 0s and 1s to define whether that wound level is checked
  64.         let wounds = woundnames.map(lvl => +v[lvl] || 0);
  65.        
  66.         // ==== GET VALUE OF NEW WOUND
  67.         let newWound = parseInt(v.woundlvl) || 0;
  68.         // if its 2+, need to add 2 to its value, to match the arrays above.
  69.         if(newWound > 1) newWound += 2;
  70.         // if its 0, routine ends, otherwise reduce value by 1, again to match above arrays.
  71.         if(newWound < 1) {
  72.            setAttrs({woundlvl: 0},{silent:true});
  73.            return;
  74.        }
  75.        newWound -= 1;
  76.  
  77.        // ==== ASSIGN WOUND
  78.        // need to check whether rolled wound is currently 1, and if not, check upwards till a 0 is found
  79.        let woundAssigned = -1;
  80.        for(let i = newWound; i < woundnames.length; i++) {
  81.            if(wounds[i] === 0) {
  82.                wounds[i] = 1;
  83.                woundAssigned = i;
  84.                break;
  85.            }
  86.        }
  87.        // this marks one wound, then exits the loop
  88.        // if woundAssigned is still -1, no wound was assigned - every wound slot is filled.
  89.  
  90.        // ========= PREPARE OUTPUT
  91.        const settings = {};
  92.        // resetthe wound box to 0.
  93.        settings.woundlvl = 0;
  94.  
  95.        //calculate wound penalty
  96.        settings['hurtlvl'] = woundPenalty(wounds);
  97.        
  98.        // update the wound boxes on the character sheet
  99.        settings[woundAssigned < 0 ? "unconscious" : woundnames[woundAssigned]] = 1;
  100.        
  101.        //update the character sheet
  102.        setAttrs(settings, {silent:true});
  103.        
  104.    });
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement