Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Macro to transform seconds to hours, minutes and seconds - For Traveller game - I want to optimize it.

1618655353

Edited 1618656001
In Traveler the following formula is used to calculate the travel time between two points of a star system. time = 2x square root (distance / acceleration). If the distance is measured in m and the acceleration in m/s 2 the time gives a result in seconds . For short distances it is suitable, but not for long ones. Here is the macro giving the total travel time in units of seconds (the most exact) in units of minutes (rounding), in units of hours (rounding) and in days (rounding) &{template:default} {{name=Calculation of time }}{{to travel ?{distance_Km} Km}}{{at ?{acceleration_G} G}}{{Time on seconds [[round(2*((?{distance_Km}/?{acceleration_G}*100)**0.5))]] }}{{Time on minutes [[round(2*((?{distance_Km}/?{acceleration_G}*100)**0.5)/60)]] }}{{Time on hours [[round(2*((?{distance_Km}/?{acceleration_G}*100)**0.5)/60/60)]] }}{{Time on days [[round(2*((?{distance_Km}/?{acceleration_G}*100)**0.5)/60/60/24)]]}} So I decided to make a macro that would take a value of seconds and change it to exactly how many hours, minutes and seconds the trip lasts, without doing any rounding. Here is the macro [[floor(?{seconds}/3600)]] h [[floor(?{seconds}/60-floor(?{seconds}/3600)*60)]] min [[?{seconds}-floor(?{seconds}/3600)*3600-floor(?{seconds}/60-floor(?{seconds}/3600)*60)*60]] s Now I am left to make the output of the first macro (only taking the value of the seconds) is the input of the equation of the second macro. For this I wanted to use an intermediate variable, so that I did not have to put the entire first formula in each variable of the second formula. But just this is what I did not find how to do. How do you assign a result to a variable that will later be used in another formula? Here I leave the final macro, which works fine, but is very confusing. &{template:default} {{name=Calculation of time }}{{to travel ?{distance_Km} Km}}{{at ?{acceleration_G} G}}{{Time [[floor((2*((?{distance_Km}/?{acceleration_G}*100)**0.5))/3600)]] h [[floor((2*((?{distance_Km}/?{acceleration_G}*100)**0.5))/60-floor((2*((?{distance_Km}/?{acceleration_G}*100)**0.5))/3600)*60)]] min [[round((2*((?{distance_Km}/?{acceleration_G}*100)**0.5))-floor((2*((?{distance_Km}/?{acceleration_G}*100)**0.5))/3600)*3600-floor((2*((?{distance_Km}/?{acceleration_G}*100)**0.5))/60-floor((2*((?{distance_Km}/?{acceleration_G}*100)**0.5))/3600)*60)*60)]] s}} Yes I used 10 m/s 2 for the gravity accelaration. Thanks a lot
1618658731
David M.
Pro
API Scripter
AFAIK, there's no way to store "variables" without api access (requiring a Pro subscription). Putting all the calculations into a single macro is going to be the way to go.
David M. said: AFAIK, there's no way to store "variables" without api access (requiring a Pro subscription). Putting all the calculations into a single macro is going to be the way to go. Ok Thanks a lot. Now the macro works, is ugly, but works.
David M. said: AFAIK, there's no way to store "variables" without api access (requiring a Pro subscription). Putting all the calculations into a single macro is going to be the way to go. And if it were Pro with api. What would the code be? Thank you.
1618838684

Edited 1618858614
The Aaron
Roll20 Production Team
API Scripter
With minimal formatting, it looks something like this: !dist 123409875 1234.87 Distance:   123,409,875 m Acceleration:   1234.87 m/s² Time:   0 D  0 H  10 M  33 S Code: on('ready',()=>{ const DayInSeconds = 86400; const HourInSeconds = 3600; const MinuteInSeconds = 60; const processInlinerollsNative = (msg) => { if(msg.hasOwnProperty('inlinerolls')){ return msg.inlinerolls .reduce((m,v,k) => { let ti=v.results.rolls.reduce((m2,v2) => { if(v2.hasOwnProperty('table')){ m2.push(v2.results.reduce((m3,v3) => [...m3,v3.tableItem.name],[]).join(", ")); } return m2; },[]).join(', '); return [...m,{k:`$[[${k}]]`, v:(ti.length && ti) || v.results.total || 0}]; },[]) .reduce((m,o) => m.replace(o.k,o.v), msg.content); } else { return msg.content; } }; const commaNum = (n) => n.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); const f = { box: (...c)=>`<div style="border: 1px solid black; border-radius:0.25em; background-color: #ccc;">${c.join('')}</div>`, row: (l,d) =>`<div>${f.lbl(l)} ${f.b(d)}</div>`, lbl: (l) =>`<span style="display: inline-block;background-color: #000; color: #fff; padding: .1em .5em; width: 7em; text-align: right">${l}:</span>`, b: (...c) => `<bold>${c.join('')}</bold>`, c: (...c) => `<code>${c.join('')}</code>` }; on('chat:message',msg=>{ if('api'===msg.type && /^!dist(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname'); let args = processInlinerollsNative(msg).split(/\s+/).slice(1); let dist = parseFloat(args[0]) || 0; let acceleration = parseFloat(args[1]) || 0; if(dist) { if(acceleration){ let Ss = 2*Math.sqrt(dist/acceleration); let Ds = Math.floor(Ss/DayInSeconds); Ss %= DayInSeconds; let Hs = Math.floor(Ss/HourInSeconds); Ss %= HourInSeconds; let Ms = Math.floor(Ss/MinuteInSeconds); Ss = Math.ceil( Ss % MinuteInSeconds ); sendChat("",f.box( f.row('Distance',`${f.c(commaNum(dist))}m`), f.row('Acceleration',`${f.c(acceleration)}m/s²`), f.row('Time',`${f.c(Ds)}D ${f.c(Hs)}H ${f.c(Ms)}M ${f.c(Ss)}S`) )); } else { sendChat("",`/w "${who}" You'll never get there with no acceleration.`); } } else { sendChat("",`/w "${who}" You're already there.`); } } }); });
1618858282

Edited 1618858357
Thanks a lot. In summer time I'll go PRO. One detail, acceleration units are m/s²
1618858628
The Aaron
Roll20 Production Team
API Scripter
Fixed! =D