
I am trying to modify a script that tracks hp, but not being familiar enough with everything, I am lost. (a script The Aaron posted here originally) Why I am trying to modify the script? I am running Pathfinder 1e, in which a character is disabled at 0 hp, dying in negative hp, and dead at a number of negative hit points equal to the character's Constitution score. Trying to do the following specifically with the script. (using roll20 character sheet for pathfinder 1e) dead at hp = negative constitution (with dead condition) (or at 0 hp if creature has no constitution score) dying at negative hp (with dying condition) disabled at 0 hp (with disabled condition) /* global TokenMod, ChatSetAttr */
on('ready', () => {
// Configuration parameters
const HPBarNum = 3;
const TempHPMarker = 'red';
const DeadMarker = 'dead';
const TempHPAttributeName = 'hp_temp';
// new constants
const DisableMarker = 'half-heart';
const DyingMarker = 'skull';
const ConScore = 'constitution';
//end new constants
/////////////////////////////////////////////
const bar = `bar${HPBarNum}_value`;
const lnk = `bar${HPBarNum}_link`;
const max = `bar${HPBarNum}_max`;
const mrk = `status_${TempHPMarker}`;
const ded = `status_${DeadMarker}`;
const die = `status_${DyingMarker}`; //new const
const dis = `status_${DisableMarker}`; //new const
const checkTempHP = (obj) => {
let v = parseFloat(obj.get('current'));
findObjs({
type: 'graphic',
represents: obj.get('characterid')
})
.filter( (t) => t.get(lnk) !== '')
.forEach((g)=>{
g.set(mrk,(v>0 ? (v>9 ? true : v) : false) );
});
};
const assureTempHPMarkers = () => {
let queue = findObjs({
type: 'attribute',
name: TempHPAttributeName
});
const burndownQueue = ()=>{
if(queue.length){
let attr = queue.shift();
checkTempHP(attr);
setTimeout(burndownQueue,0);
}
};
burndownQueue();
};
const temporalTempHPCache = {};
const accountForHPBarChange = (obj,prev) => {
// 1. did hp change and is it a scale
const hpMax = parseInt(obj.get(max),10);
// new code - set death point of negative Constitution score
let DeadPoint = parseInt(0 - obj.get(ConScore),10);
// back to original code.
let hp = parseInt(obj.get(bar),10);
const diff = hp-parseFloat(prev[bar]);
if( !isNaN(hpMax) && diff !== 0 ) {
let changes = {};
// 2. does it represent a character
// 3. does the hp bar represent an attribute
const character = getObj('character',obj.get('represents'));
if( diff < 0 && character && obj.get(lnk)!=='' ){
// 4. is there temp hp
const temp_hp = findObjs({
type: 'attribute',
characterid: character.id,
name: TempHPAttributeName
})[0];
if( temp_hp ) {
const now = Date.now();
// 5. have we accounted for it.
if( !temporalTempHPCache.hasOwnProperty(character.id) || (now-temporalTempHPCache[character.id].when)>300 ) {
// calculate necessary change
const tempHP = parseFloat(temp_hp.get('current'))||0;
const newTmpHP = Math.max((tempHP+diff),0);
const toHeal = tempHP - newTmpHP;
temporalTempHPCache[character.id]={
when: now,
toHeal: toHeal
};
temp_hp.set('current', newTmpHP);
checkTempHP(temp_hp);
}
hp += temporalTempHPCache[character.id].toHeal;
changes[bar] = hp;
}
}
if(hp > hpMax) {
hp = hpMax;
changes[bar] = hp;
changes[dis] = false; //new line for new conditions
changes[die] = false; //new line for new conditions
changes[ded] = false;
// original code
/* } else if(hp <= 0) {
changes[bar] = hp;
changes[ded] = true;
}
*/
// start new code for neg hp in pathfinder
} else if(hp=0) {
changes[bar] = hp;
changes[dis] = true;
changes[die] = false;
changes[ded] = false;
} else if( (hp <= 0) && (hp > DeadPoint) ) {
changes[bar] = hp;
changes[dis] = false;
changes[die] = true;
changes[ded] = false;
} else if(hp <= DeadPoint) {
hp = DeadPoint;
changes[bar] = hp;
changes[dis] = false;
changes[die] = false;
changes[ded] = true;
// end new code
} else {
changes[dis] = false; // new line for new conditions
changes[die] = false; // new line for new conditions
changes[ded] = false;
}
obj.set(changes);
}
};
const onAttributeChange = (obj) => {
if(obj.get('name') === TempHPAttributeName){
checkTempHP(obj);
}
};
on("change:attribute", onAttributeChange);
on("change:token", accountForHPBarChange);
if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){
TokenMod.ObserveTokenChange(accountForHPBarChange);
}
if('undefined' !== typeof ChatSetAttr && ChatSetAttr.registerObserver){
ChatSetAttr.registerObserver('change',onAttributeChange);
}
assureTempHPMarkers();
}); A second minor issue I have encountered with this script is the updating of the temp hit points with the condition on the token. Everything reports correctly on the character sheet, but the condition number (on the token) does not report correctly. Thanks for any help in advance.