Skip to content

Commit 649a4c5

Browse files
Fix chapter time resetting to zero during save and quit
1 parent 7445931 commit 649a4c5

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

SwiftSplit/CelesteScanner.swift

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ struct AutoSplitterData: Equatable {
280280
}
281281

282282
enum ChapterMode : Equatable {
283+
case Menu
283284
case Normal
284285
case BSide
285286
case CSide
@@ -331,6 +332,8 @@ class AutoSplitterInfo {
331332

332333
self.chapter = Int(data.chapter)
333334
switch data.mode {
335+
case -1:
336+
self.mode = .Menu
334337
case 0:
335338
self.mode = .Normal
336339
case 1:
@@ -353,22 +356,4 @@ class AutoSplitterInfo {
353356
self.fileCassettes = Int(data.fileCassettes)
354357
self.fileHearts = Int(data.fileHearts)
355358
}
356-
357-
func stableStateEquals(other: AutoSplitterInfo) -> Bool {
358-
return self.chapter == other.chapter &&
359-
self.mode == other.mode &&
360-
self.level == other.level &&
361-
self.timerActive == other.timerActive &&
362-
self.chapterStarted == other.chapterStarted &&
363-
self.chapterComplete == other.chapterComplete &&
364-
// self.chapterTime == other.chapterTime &&
365-
self.chapterStrawberries == other.chapterStrawberries &&
366-
self.chapterCassette == other.chapterCassette &&
367-
self.chapterHeart == other.chapterHeart &&
368-
// self.fileTime == other.fileTime &&
369-
self.fileStrawberries == other.fileStrawberries &&
370-
self.fileCassettes == other.fileCassettes &&
371-
self.fileHearts == other.fileHearts
372-
}
373-
374359
}

SwiftSplit/CelesteSplitter.swift

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class CelesteSplitter {
5959
}
6060
}
6161

62+
private var time: Double = 0.0
6263
private var gameTimeRunning = false
6364
private(set) var nextEventIndex = 0
6465

@@ -73,12 +74,13 @@ class CelesteSplitter {
7374
return []
7475
}
7576
let events = getEvents(from: autoSplitterInfo, to: info)
76-
// logStateChange(from: autoSplitterInfo, to: info)
77+
logStateChange(from: autoSplitterInfo, to: info)
7778
autoSplitterInfo = info
7879

79-
let time = routeConfig.useFileTime ? autoSplitterInfo.fileTime : autoSplitterInfo.chapterTime
80+
if autoSplitterInfo.mode != .Menu { // save and quit resets the chapter timer to zero, but we don't want to reset it for QTM strats
81+
time = routeConfig.useFileTime ? autoSplitterInfo.fileTime : autoSplitterInfo.chapterTime
82+
}
8083
server.setGameTime(seconds: time)
81-
8284
processEvents(events)
8385

8486
// when using the chapter time, `timerActive` will be true before the chapter time starts ticking.
@@ -94,15 +96,29 @@ class CelesteSplitter {
9496
var lastStateTime = DispatchTime.now()
9597

9698
func logStateChange(from old: AutoSplitterInfo, to new: AutoSplitterInfo) {
97-
if !old.stableStateEquals(other: new) {
99+
let comparisons = [
100+
(name: "chapter", old: "\(old.chapter)", new: "\(new.chapter)"),
101+
(name: "mode", old: "\(old.mode)", new: "\(new.mode)"),
102+
(name: "level", old: "\(old.level)", new: "\(new.level)"),
103+
(name: "timerActive", old: "\(old.timerActive)", new: "\(new.timerActive)"),
104+
(name: "chapterStarted", old: "\(old.chapterStarted)", new: "\(new.chapterStarted)"),
105+
(name: "chapterComplete", old: "\(old.chapterComplete)", new: "\(new.chapterComplete)"),
106+
// (name: "chapterTime", old: "\(old.chapterTime)", new: "\(new.chapterTime)"),
107+
(name: "chapterStrawberries", old: "\(old.chapterStrawberries)", new: "\(new.chapterStrawberries)"),
108+
(name: "chapterCassette", old: "\(old.chapterCassette)", new: "\(new.chapterCassette)"),
109+
(name: "chapterHeart", old: "\(old.chapterHeart)", new: "\(new.chapterHeart)"),
110+
// (name: "fileTime", old: "\(old.fileTime)", new: "\(new.fileTime)"),
111+
(name: "fileStrawberries", old: "\(old.fileStrawberries)", new: "\(new.fileStrawberries)"),
112+
(name: "fileCassettes", old: "\(old.fileCassettes)", new: "\(new.fileCassettes)"),
113+
(name: "fileHearts", old: "\(old.fileHearts)", new: "\(new.fileHearts)"),
114+
]
115+
let changes = comparisons.filter { $0.old != $0.new }
116+
117+
if !changes.isEmpty {
98118
let currentTime = DispatchTime.now()
99119
let delta = Double(currentTime.uptimeNanoseconds - lastStateTime.uptimeNanoseconds) / 1_000_000_000
100120
print(
101-
"[\(delta)] " +
102-
"chapter: \(new.chapter), mode: \(new.mode), level: \(new.level), timerActive: \(new.timerActive), " +
103-
"chapterStarted: \(new.chapterStarted), chapterComplete: \(new.chapterComplete), chapterTime: \(new.chapterTime), " +
104-
"chapterStrawberries: \(new.chapterStrawberries), chapterCassette: \(new.chapterCassette), chapterHeart: \(new.chapterHeart), fileTime: \(new.fileTime), " +
105-
"fileStrawberries: \(new.fileStrawberries), fileCassettes: \(new.fileCassettes), fileHearts: \(new.fileHearts)"
121+
"[real delta: \(delta), chapter: \(new.chapterTime), file: \(new.fileTime)] " + changes.map { "\($0.name): \($0.old) -> \($0.new)" }.joined(separator: ", ")
106122
)
107123
lastStateTime = currentTime
108124
}
@@ -154,13 +170,37 @@ class CelesteSplitter {
154170
events.append(["\(old.level) > \(new.level)"])
155171
}
156172
if new.chapterCassette && !old.chapterCassette {
157-
events.append(["cassette", "chapter \(new.chapter) cassette", "\(new.fileCassettes) total cassettes"])
173+
events.append([
174+
"collect cassette",
175+
"collect chapter \(new.chapter) cassette",
176+
"collect \(new.fileCassettes) total cassettes",
177+
// compat:
178+
"cassette",
179+
"chapter \(new.chapter) cassette",
180+
"\(new.fileCassettes) total cassettes"
181+
])
158182
}
159183
if new.chapterHeart && !old.chapterHeart {
160-
events.append(["heart", "chapter \(new.chapter) heart", "\(new.fileHearts) total hearts"])
184+
events.append([
185+
"collect heart",
186+
"collect chapter \(new.chapter) heart",
187+
"collect \(new.fileHearts) total hearts",
188+
// compat:
189+
"heart",
190+
"chapter \(new.chapter) heart",
191+
"\(new.fileHearts) total hearts"
192+
])
161193
}
162194
if new.chapterStrawberries > old.chapterStrawberries {
163-
events.append(["strawberry", "\(new.chapterStrawberries) chapter strawberries", "\(new.fileStrawberries) file strawberries"])
195+
events.append([
196+
"collect strawberry",
197+
"collect \(new.chapterStrawberries) chapter strawberries",
198+
"collect \(new.fileStrawberries) file strawberries",
199+
// compat:
200+
"strawberry",
201+
"\(new.chapterStrawberries) chapter strawberries",
202+
"\(new.fileStrawberries) file strawberries"
203+
])
164204
}
165205
return events
166206
}

0 commit comments

Comments
 (0)