|
| 1 | +-- Change the duration of the "Updating World" timeskip preceding the start of a new game. |
| 2 | +-- Author: Atomic Chicken |
| 3 | + |
| 4 | +local usage = [====[ |
| 5 | +
|
| 6 | +set-timeskip-duration |
| 7 | +===================== |
| 8 | +Starting a new fortress/adventurer session is preceded by |
| 9 | +an "Updating World" process which is normally 2 weeks long. |
| 10 | +This script allows you to modify the duration of this timeskip, |
| 11 | +enabling you to jump into the game earlier or later than usual. |
| 12 | +
|
| 13 | +You can use this at any point before the timeskip begins |
| 14 | +(for example, while still at the "Start Playing" menu). |
| 15 | +
|
| 16 | +It is also possible to run the script while the world is updating, |
| 17 | +which can be useful if you decide to end the process earlier |
| 18 | +or later than initially planned. |
| 19 | +
|
| 20 | +Note that the change in timeskip duration will persist until either |
| 21 | + the game is closed, |
| 22 | + the "-clear" argument is used (see below), |
| 23 | + or it is overwritten by setting a new duration. |
| 24 | +
|
| 25 | +The timeskip duration can be specified using any combination of |
| 26 | +the following arguments. |
| 27 | +
|
| 28 | +Usage:: |
| 29 | +
|
| 30 | + -ticks X |
| 31 | + Replace "X" with a positive integer (or 0) |
| 32 | + Adds the specified number of ticks to the timeskip duration |
| 33 | + The following conversions may help you calculate this: |
| 34 | + 1 tick = 72 seconds = 1 minute 12 seconds |
| 35 | + 50 ticks = 60 minutes = 1 hour |
| 36 | + 1200 ticks = 24 hours = 1 day |
| 37 | + 8400 ticks = 7 days = 1 week |
| 38 | + 33600 ticks = 4 weeks = 1 month |
| 39 | + 403200 ticks = 12 months = 1 year |
| 40 | +
|
| 41 | + -years X |
| 42 | + Replace "X" with a positive integer (or 0) |
| 43 | + Adds (403200 multiplied by X) ticks to the timeskip duration |
| 44 | +
|
| 45 | + -months X |
| 46 | + Replace "X" with a positive integer (or 0) |
| 47 | + Adds (33600 multiplied by X) ticks to the timeskip duration |
| 48 | +
|
| 49 | + -days X |
| 50 | + Replace "X" with a positive integer (or 0) |
| 51 | + Adds (1200 multiplied by X) ticks to the timeskip duration |
| 52 | +
|
| 53 | + -hours X |
| 54 | + Replace "X" with a positive integer (or 0) |
| 55 | + Adds (50 multiplied by X) ticks to the timeskip duration |
| 56 | +
|
| 57 | + -clear |
| 58 | + This resets the timeskip duration to its default value. |
| 59 | + Note that it won't affect timeskips which have already begun. |
| 60 | +
|
| 61 | +Example:: |
| 62 | +
|
| 63 | + set-timeskip-duration -ticks 851249 |
| 64 | + Sets the end of the timeskip to |
| 65 | + 2 years, 1 month, 9 days, 8 hours, 58 minutes, 48 seconds |
| 66 | + from the current date. |
| 67 | +
|
| 68 | + set-timeskip-duration -years 2 -months 1 -days 9 -hours 8 -ticks 49 |
| 69 | + Does the same thing as the previous example |
| 70 | +
|
| 71 | +]====] |
| 72 | + |
| 73 | +local utils = require 'utils' |
| 74 | + |
| 75 | +function printTimeskipCalendarDuration(ticks) |
| 76 | + print("Timeskip duration set to " .. ticks .. " tick" .. (ticks == 1 and "." or "s.")) |
| 77 | + if ticks > 0 then |
| 78 | + print("This is equivalent to:") |
| 79 | + local years = math.floor(ticks/403200) |
| 80 | + ticks = ticks%403200 |
| 81 | + if years > 0 then |
| 82 | + print(" " .. tostring(years) .. " year" .. (years == 1 and "" or "s")) |
| 83 | + end |
| 84 | + local months = math.floor(ticks/33600) |
| 85 | + ticks = ticks%33600 |
| 86 | + if months > 0 then |
| 87 | + print(" " .. tostring(months) .. " month" .. (months == 1 and "" or "s")) |
| 88 | + end |
| 89 | + local days = math.floor(ticks/1200) |
| 90 | + ticks = ticks%1200 |
| 91 | + if days > 0 then |
| 92 | + print(" " .. tostring(days) .. " day" .. (days == 1 and "" or "s")) |
| 93 | + end |
| 94 | + local hours = math.floor(ticks/50) |
| 95 | + ticks = ticks%50 |
| 96 | + if hours > 0 then |
| 97 | + print(" " .. tostring(hours) .. " hour" .. (hours == 1 and "" or "s")) |
| 98 | + end |
| 99 | + local minutes = math.floor(6*ticks/5) |
| 100 | + ticks = ticks - (minutes*5)/6 |
| 101 | + if minutes > 0 then |
| 102 | + print(" " .. tostring(minutes) .. " minute" .. (minutes == 1 and "" or "s")) |
| 103 | + end |
| 104 | + local seconds = math.ceil(72*ticks) |
| 105 | + if seconds > 0 then |
| 106 | + print(" " .. tostring(seconds) .. " second" .. (seconds == 1 and "" or "s")) |
| 107 | + end |
| 108 | + end |
| 109 | +end |
| 110 | + |
| 111 | +function getTargetDate(ticks) |
| 112 | + local targetYear = tonumber(df.global.cur_year) |
| 113 | + local targetTick = tonumber(df.global.cur_year_tick) |
| 114 | + if targetTick + ticks >= 403200 then |
| 115 | + ticks = ticks - (403200 - targetTick) |
| 116 | + targetTick = 0 |
| 117 | + targetYear = targetYear + 1 |
| 118 | + end |
| 119 | + targetYear = targetYear + math.floor(ticks/403200) |
| 120 | + targetTick = targetTick + (ticks%403200) |
| 121 | + return targetYear, targetTick |
| 122 | +end |
| 123 | + |
| 124 | +function setTargetDate(scr, ticks) -- df.viewscreen_update_regionst |
| 125 | + local targetYear, targetTick = getTargetDate(ticks) |
| 126 | + scr.year = targetYear |
| 127 | + scr.year_tick = targetTick |
| 128 | +end |
| 129 | + |
| 130 | +local validArgs = utils.invert({ |
| 131 | + 'ticks', |
| 132 | + 'years', |
| 133 | + 'months', |
| 134 | + 'days', |
| 135 | + 'hours', |
| 136 | + 'clear', |
| 137 | + 'help' |
| 138 | +}) |
| 139 | +local args = utils.processArgs({...}, validArgs) |
| 140 | + |
| 141 | +if args.help then |
| 142 | + print(usage) |
| 143 | + return |
| 144 | +end |
| 145 | + |
| 146 | +if args.clear then |
| 147 | + dfhack.onStateChange.TimeskipMonitor = nil |
| 148 | + print("Timeskip duration reset to default.") |
| 149 | + return |
| 150 | +end |
| 151 | + |
| 152 | +local ticks |
| 153 | + |
| 154 | +if args.ticks then |
| 155 | + if tonumber(args.ticks) and tonumber(args.ticks) >= 0 then |
| 156 | + ticks = (ticks or 0) + tonumber(args.ticks) |
| 157 | + else |
| 158 | + qerror("Invalid -ticks duration: " .. tostring(args.ticks)) |
| 159 | + end |
| 160 | +end |
| 161 | + |
| 162 | +if args.years then |
| 163 | + if tonumber(args.years) and tonumber(args.years) >= 0 then |
| 164 | + ticks = (ticks or 0) + tonumber(args.years)*403200 |
| 165 | + else |
| 166 | + qerror("Invalid -years duration: " .. tostring(args.years)) |
| 167 | + end |
| 168 | +end |
| 169 | + |
| 170 | +if args.months then |
| 171 | + if tonumber(args.months) and tonumber(args.months) >= 0 then |
| 172 | + ticks = (ticks or 0) + tonumber(args.months)*33600 |
| 173 | + else |
| 174 | + qerror("Invalid -months duration: " .. tostring(args.months)) |
| 175 | + end |
| 176 | +end |
| 177 | + |
| 178 | +if args.days then |
| 179 | + if tonumber(args.days) and tonumber(args.days) >= 0 then |
| 180 | + ticks = (ticks or 0) + tonumber(args.days)*1200 |
| 181 | + else |
| 182 | + qerror("Invalid -days duration: " .. tostring(args.days)) |
| 183 | + end |
| 184 | +end |
| 185 | + |
| 186 | +if args.hours then |
| 187 | + if tonumber(args.hours) and tonumber(args.hours) >= 0 then |
| 188 | + ticks = (ticks or 0) + tonumber(args.hours)*50 |
| 189 | + else |
| 190 | + qerror("Invalid -hours duration: " .. tostring(args.hours)) |
| 191 | + end |
| 192 | +end |
| 193 | + |
| 194 | +if not ticks then |
| 195 | + qerror("Duration not specified! Enter \"set-timeskip-duration -help\" for more information.") |
| 196 | +end |
| 197 | + |
| 198 | +ticks = math.floor(ticks) -- get rid of decimals the user may have inputted |
| 199 | + |
| 200 | +printTimeskipCalendarDuration(ticks) |
| 201 | + |
| 202 | +local scr = dfhack.gui.getCurViewscreen() |
| 203 | +if scr._type == df.viewscreen_update_regionst then |
| 204 | + setTargetDate(scr, ticks) |
| 205 | +end |
| 206 | + |
| 207 | +dfhack.onStateChange.TimeskipMonitor = function(event) |
| 208 | + if event == SC_VIEWSCREEN_CHANGED then |
| 209 | + local scr = dfhack.gui.getCurViewscreen() |
| 210 | + if scr._type == df.viewscreen_update_regionst then |
| 211 | + setTargetDate(scr, ticks) |
| 212 | + end |
| 213 | + end |
| 214 | +end |
0 commit comments