SHARE
TWEET

Twine 2/SugarCube 2 - Time and Date functions

a guest Apr 22nd, 2018 543 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Code for handling time, date, flags, and alarms in Twine 2 / SugarCube 2:
  2.  
  3. First, put this in your StoryInit:
  4.  
  5. <<set $ShowTime = 0>>  /* 0 = no, 1 = show colon, 2 = hide colon */
  6. <<set $GameDay = 1>>
  7. <<set $CurHr = 0>>
  8. <<set $CurMn = 0>>
  9. <<set $CurSc = 0>>
  10. <<set $Alarms = {}>>
  11. <<set $Flags = {}>>
  12.  
  13. ---
  14. Change $GameDay above to the day of the year.  So February 1st is $GameDay = 32.
  15.  
  16. Next, put this in your StoryCaption:
  17.  
  18. <span id="CurTime" style="padding-left: 50px;"></span>
  19. <<silently>><<timed 5ms>><<TimeDisplay>><</timed>><<repeat 1000ms>><<TimeDisplay>><</repeat>><</silently>>
  20.  
  21. ---
  22. That will display the time and date.
  23.  
  24. Then put this in your story JavaScript section:
  25.  
  26. // Fresh is true if you didn't come to the passage from Inventory or Credits
  27. window.Fresh = function () {
  28.     return ((lastVisited("Inventory") != 1) && (lastVisited("Credits") != 1));
  29. };
  30.  
  31. // If flag exists then return value, else return false
  32. window.Flag = function (Fnam) {
  33.     if (State.variables.Flags[Fnam.toLowerCase()] !== undefined) {
  34.         return State.variables.Flags[Fnam.toLowerCase()];
  35.     }
  36.     return false;
  37. };
  38.  
  39. /*  ConvertTime : Converts hour and minute into a standard time string with optional blinking colon.  */
  40. window.ConvertTime = function (Hour, Minute, Blink) {
  41.     var TimeStr = "";
  42.     if (Hour < 13) {
  43.         if (Blink && (Hour < 10)) {
  44.             TimeStr = "@@color:#222;1@@" + Hour;
  45.         } else {
  46.             TimeStr = Hour;
  47.         }
  48.     } else {
  49.         TimeStr = (Hour - 12);
  50.     }
  51.     if (Blink) {
  52.         if (State.variables.ShowTime == 1) {
  53.             TimeStr += ":";
  54.             State.variables.ShowTime = 2;
  55.         } else if (State.variables.ShowTime == 2) {
  56.             TimeStr += "@@color:#222;:@@";
  57.             State.variables.ShowTime = 1;
  58.         }
  59.     } else {
  60.         TimeStr += ":";
  61.     }
  62.     if (Minute < 10) {
  63.         TimeStr += "0" + Minute;
  64.     } else {
  65.         TimeStr += Minute;
  66.     }
  67.     if (Hour < 12) {
  68.         TimeStr += " AM";
  69.     } else {
  70.         TimeStr += " PM";
  71.     }
  72.     return TimeStr;
  73. };
  74.  
  75. // AlarmList: Returns a list of all alarm names
  76. // EXAMPLE: <<set AlarmNames = AlarmList()>>
  77. window.AlarmList = function () {
  78.     return Object.keys(State.variables.Alarms);
  79. };
  80.  
  81. // AlarmExists: Returns whether AlarmName exists or not
  82. // EXAMPLE: <<if AlarmExists("AlarmName")>>
  83. window.AlarmExists = function (AlarmName) {
  84.     return AlarmList().includes(AlarmName);
  85. };
  86.  
  87. // NextAlarm: Returns oldest AlarmName or "" if no alarms are set
  88. // EXAMPLE: <<set AName = NextAlarm()>>
  89. window.NextAlarm = function () {
  90.     if (Object.keys(State.variables.Alarms).length === 0) {
  91.         return "";
  92.     } else {
  93.         return AlarmList()[0];
  94.     }
  95. };
  96.  
  97. // TimeTill: Returns minutes left til time given
  98. // EXAMPLE: <<set Min = TimeTill(17, 30)>>
  99. window.TimeTill = function (Hour, Minute, Day) {
  100.     var MinLeft = 0;
  101.     if (Day !== undefined) {
  102.         MinLeft = (Day - State.variables.GameDay) * 1440;
  103.     }
  104.     MinLeft += (Hour - State.variables.CurHr) * 60;
  105.     MinLeft += Minute - State.variables.CurMn;
  106.     return MinLeft;
  107. };
  108.  
  109. // TimeFromNow: Returns current day and time plus N minutes
  110. // EXAMPLE: <<set DayAndTime = TimeFromNow(15)>>
  111. window.TimeFromNow = function (Minutes) {
  112.     var Day = State.variables.GameDay, Hour = State.variables.CurHr, Minute = State.variables.CurMn + Minutes;
  113.     if (Minute > 60) {
  114.         Minute -= 60;
  115.         Hour += 1;
  116.         if (Hour > 24) {
  117.             Hour -= 24;
  118.             Day += 1;
  119.         }
  120.     }
  121.     var DayAndTime = { "Day" : Day, "Hour" : Hour, "Minute" : Minute };
  122.     return DayAndTime;
  123. };
  124.  
  125. // TimeTillAlarm: Returns minutes left til AlarmName or null if there are no alarms
  126. // EXAMPLE: <<set Min = TimeTillAlarm("AlarmName")>>
  127. window.TimeTillAlarm = function (AlarmName) {
  128.     if (AlarmExists(AlarmName)) {
  129.         return TimeTill(State.variables.Alarms[AlarmName].Hour, State.variables.Alarms[AlarmName].Minute, State.variables.Alarms[AlarmName].Day);
  130.     } else {
  131.         return null;
  132.     }
  133. };
  134.  
  135. // TimeTillNextAlarm: Returns minutes left til AlarmName or null if there are no alarms
  136. // EXAMPLE: <<set Min = TimeTillNextAlarm()>>
  137. window.TimeTillNextAlarm = function () {
  138.     var Name = NextAlarm();
  139.     if (Name === "") {
  140.         return null;
  141.     } else {
  142.         return TimeTillAlarm(Name);
  143.     }
  144. };
  145.  
  146. // ClearAlarm: Deletes alarm AlarmName
  147. // EXAMPLE: <<run ClearAlarm("AlarmName")>>
  148. window.ClearAlarm = function (AlarmName) {
  149.     if (AlarmExists(AlarmName)) {
  150.         delete State.variables.Alarms[AlarmName];
  151.     }
  152. };
  153.  
  154. // AlarmRinging: Returns true if the current time >= AlarmName's time, otherwise it returns false
  155. // EXAMPLE: <<if AlarmRinging("AlarmName")>>
  156. window.AlarmRinging = function (AlarmName) {
  157.     if (AlarmExists(AlarmName)) {
  158.         if (TimeTillAlarm(AlarmName) <= 0) {
  159.             return true;
  160.         } else {
  161.             return false;
  162.         }
  163.     }
  164. };
  165.  
  166. // SortAlarms: Sorts alarms from oldest to furthest in the future
  167. // EXAMPLE: <<run SortAlarms()>>
  168. window.SortAlarms = function () {
  169.  
  170.     var SortFn = function(DayAndTime1, DayAndTime2) {
  171.         if (DayAndTime1.Day < DayAndTime2.Day) {
  172.             return -1;
  173.         } else if (DayAndTime1.Day > DayAndTime2.Day) {
  174.             return 1;
  175.         } else {
  176.             if (DayAndTime1.Hour < DayAndTime2.Hour) {
  177.                 return -1;
  178.             } else if (DayAndTime1.Hour > DayAndTime2.Hour) {
  179.                 return 1;
  180.             } else {
  181.                 if (DayAndTime1.Minute < DayAndTime2.Minute) {
  182.                     return -1;
  183.                 } else if (DayAndTime1.Minute > DayAndTime2.Minute) {
  184.                     return 1;
  185.                 } else {
  186.                     return 0;
  187.                 }
  188.             }
  189.         }
  190.     };
  191.    
  192.     if (AlarmList.length > 1) {
  193.         State.variables.Alarms.sort(SortFn);
  194.     }
  195. };
  196.  
  197. // SetAlarm: Sets alarm in X minutes, or at Min+Hr today, or for Min+Hr+Day
  198. // EXAMPLE: <<run SetAlarm("Dinner Time", 17, 0)>>
  199. window.SetAlarm = function (AlarmName, Hour, Minute, Day) {
  200.     if (AlarmExists(AlarmName)) {
  201.        
  202.         // *** add code to throw error here due to duplicate AlarmName ***
  203.        
  204.     } else {
  205.         var DayAndTime;
  206.         if (Minute === undefined) {
  207.             // Set alarm in "Hour" minutes
  208.             DayAndTime = TimeFromNow(Hour);
  209.         } else {
  210.             // Set alarm for Hour + Minute [+ Day]
  211.             if (Day === undefined) {
  212.                 DayAndTime = { "Day" : State.variables.GameDay, "Hour" : Hour, "Minute" : Minute };
  213.             } else {
  214.                 DayAndTime = { "Day" : Day, "Hour" : Hour, "Minute" : Minute };
  215.             }
  216.         }
  217.         State.variables.Alarms[AlarmName] = clone(DayAndTime);
  218.         SortAlarms();
  219.     }
  220. };
  221.  
  222. ---
  223. And finally, create a passage for the following widgets, add the tags "widget" and "nobr", and put this in it:
  224.  
  225. /*  SetFlag : Set Flag X to value Y (Y defaults to True).  Flags are not case sensitive.  */
  226. /*  EXAMPLE: <<SetFlag "Mentor" "Harmony">>  */
  227. /*  EXAMPLE: <<SetFlag "TrialMed">>  = sets TrialMed flag to True  */
  228. <<widget "SetFlag">>
  229. <<set _Fnam = $args[0].toLowerCase()>>
  230. <<if def $args[1]>>
  231.     <<if $args[1] == false>>
  232.         <<if def $Flags[_Fnam]>>
  233.             <<script>>
  234.                 delete State.variables.Flags[State.temporary.Fnam];
  235.             <</script>>
  236.         <</if>>
  237.     <<else>>
  238.         <<set $Flags[_Fnam] = $args[1]>>
  239.     <</if>>
  240. <<else>>
  241.     <<set $Flags[_Fnam] = true>>
  242. <</if>>
  243. /* Event flags: */
  244. <</widget>>
  245.  
  246. /*  TimeDisplay : Display the current time and show schedule on hover.  */
  247. /*  EXAMPLE: <<TimeDisplay>>  */
  248. <<widget "TimeDisplay">>
  249. <<if $ShowTime>>
  250.     <<replace "#CurTime">>
  251.         <<print ConvertTime($CurHr, $CurMn, true)>>
  252.     <</replace>>
  253. <</if>>
  254. <</widget>>
  255.  
  256. /*  SetDay : Set variables related to that day  */
  257. /*  EXAMPLE: <<SetDay 1>>  */
  258. <<widget "SetDay">>
  259. <<switch $args[0] % 7>>
  260.     <<case 0>>
  261.         <<set $DateDescription = "Sunday, ">>
  262.     <<case 1>>
  263.         <<set $DateDescription = "Monday, ">>
  264.     <<case 2>>
  265.         <<set $DateDescription = "Tuesday, ">>
  266.     <<case 3>>
  267.         <<set $DateDescription = "Wednesday, ">>
  268.     <<case 4>>
  269.         <<set $DateDescription = "Thursday, ">>
  270.     <<case 5>>
  271.         <<set $DateDescription = "Friday, ">>
  272.     <<case 6>>
  273.         <<set $DateDescription = "Saturday, ">>
  274. <</switch>>
  275. <<set $SunriseHour = 6>>
  276. <<set $SunriseMinute = 0>>
  277. <<set $SunsetHour = 18>>
  278. <<set $SunsetMinute = 0>>
  279. <<if $args[0] lt 32>>
  280.     <<set $DateDescription = $DateDescription + "Jan. ">>
  281.     <<set _curDay = $args[0]>>
  282. <<elseif $args[0] lt 60>>
  283.     <<set $DateDescription = $DateDescription + "Feb. ">>
  284.     <<set _curDay = $args[0] - 31>>
  285. <<elseif $args[0] lt 91>>
  286.     <<set $DateDescription = $DateDescription + "Mar. ">>
  287.     <<set _curDay = $args[0] - 59>>
  288. <<elseif $args[0] lt 121>>
  289.     <<set $DateDescription = $DateDescription + "Apr. ">>
  290.     <<set _curDay = $args[0] - 90>>
  291. <<elseif $args[0] lt 152>>
  292.     <<set $DateDescription = $DateDescription + "May ">>
  293.     <<set _curDay = $args[0] - 120>>
  294. <<elseif $args[0] lt 182>>
  295.     <<set $DateDescription = $DateDescription + "June ">>
  296.     <<set _curDay = $args[0] - 151>>
  297. <<elseif $args[0] lt 213>>
  298.     <<set $DateDescription = $DateDescription + "July ">>
  299.     <<set _curDay = $args[0] - 181>>
  300. <<elseif $args[0] lt 244>>
  301.     <<set $DateDescription = $DateDescription + "Aug. ">>
  302.     <<set _curDay = $args[0] - 212>>
  303. <<elseif $args[0] lt 274>>
  304.     <<set $DateDescription = $DateDescription + "Sept. ">>
  305.     <<set _curDay = $args[0] - 243>>
  306. <<elseif $args[0] lt 305>>
  307.     <<set $DateDescription = $DateDescription + "Oct. ">>
  308.     <<set _curDay = $args[0] - 273>>
  309. <<elseif $args[0] lt 335>>
  310.     <<set $DateDescription = $DateDescription + "Nov. ">>
  311.     <<set _curDay = $args[0] - 304>>
  312. <<elseif $args[0] lt 366>>
  313.     <<set $DateDescription = $DateDescription + "Dec. ">>
  314.     <<set _curDay = $args[0] - 334>>
  315. <</if>>
  316. <<switch _curDay>>
  317.     <<case 1 21 31>>
  318.         <<set $DateDescription = $DateDescription + _curDay + "st">>
  319.     <<case 2 22>>
  320.         <<set $DateDescription = $DateDescription + _curDay + "nd">>
  321.     <<case 3 23>>
  322.         <<set $DateDescription = $DateDescription + _curDay + "rd">>
  323.     <<default>>
  324.         <<set $DateDescription = $DateDescription + _curDay + "th">>
  325. <</switch>>
  326. <</widget>>
  327.  
  328. /*  IncGameDay : Modify the value of $GameDay safely.  Defaults to +1 day.  */
  329. /*  EXAMPLE: <<IncGameDay>>  */
  330. /*  EXAMPLE: <<IncGameDay 2>>  */
  331. <<widget "IncGameDay">>
  332. <<if Fresh()>>
  333.     <<if def $args[0]>>
  334.         <<set $GameDay += $args[0]>>
  335.     <<else>>
  336.         <<set $GameDay += 1>>
  337.     <</if>>
  338.     <<if $GameDay === 366>>
  339.         <<set $GameDay = 1>>
  340.     <</if>>
  341.     <<SetDay $GameDay>>
  342. <</if>>
  343. <</widget>>
  344.  
  345. /*  IncTime : Modify the time by X minutes safely.  */
  346. /*  EXAMPLE: <<IncTime 15>>   = +15 min      */
  347. /*  EXAMPLE: <<IncTime 0.5>>  = +30 seconds  */
  348. <<widget "IncTime">>
  349. <<if Fresh() and !Flag("LockTime")>>
  350.     <<if $args[0] != Math.trunc($args[0])>>
  351.         <<set $CurSc += ($args[0] - Math.trunc($args[0])) * 100>>
  352.         <<if $CurSc >= 100>>
  353.             <<set $CurSc -= 100>>
  354.             <<set $CurMn += Math.trunc($args[0]) + 1>>
  355.         <<else>>
  356.             <<set $CurMn += Math.trunc($args[0])>>
  357.         <</if>>
  358.     <<else>>
  359.         <<set $CurMn += $args[0]>>
  360.     <</if>>
  361.     <<if $CurMn > 60>>
  362.         <<for ; $CurMn >= 60; $CurMn -= 60>>
  363.             <<set $CurHr += 1>>
  364.         <</for>>
  365.     <</if>>
  366.     <<if $CurHr > 23>>
  367.         <<set $CurHr -= 24>>
  368.         <<IncGameDay>>
  369.     <</if>>
  370. <</if>>
  371. <</widget>>
  372.  
  373. ---
  374. Yeah, it's a lot of stuff.  And that's the simplified version.
  375.  
  376. You'll have to advance time yourself by using "<<IncTime minutes>>" whenever you need to advance the time.  You can also use "<<IncGameDay>>" to increase the day by one day.  You can also set the time and date directly using the $CurHr, $CurMn, $CurSc, and $GameDay variables, but you should call "<<SetDay $GameDay>>" if you change the $GameDay variable.
  377.  
  378. I also tossed in the Flag and Alarm functions which you can use to keep track of event flags or to set "alarms" so you can check to see if the alarm has gone off yet.  Most of the functions have comments in front of them explaining them.
  379.  
  380. Enjoy!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top