Skip to content

Commit

Permalink
Fix chapter time resetting to zero during save and quit
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodewarrior committed Jun 16, 2021
1 parent 7445931 commit 649a4c5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
21 changes: 3 additions & 18 deletions SwiftSplit/CelesteScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ struct AutoSplitterData: Equatable {
}

enum ChapterMode : Equatable {
case Menu
case Normal
case BSide
case CSide
Expand Down Expand Up @@ -331,6 +332,8 @@ class AutoSplitterInfo {

self.chapter = Int(data.chapter)
switch data.mode {
case -1:
self.mode = .Menu
case 0:
self.mode = .Normal
case 1:
Expand All @@ -353,22 +356,4 @@ class AutoSplitterInfo {
self.fileCassettes = Int(data.fileCassettes)
self.fileHearts = Int(data.fileHearts)
}

func stableStateEquals(other: AutoSplitterInfo) -> Bool {
return self.chapter == other.chapter &&
self.mode == other.mode &&
self.level == other.level &&
self.timerActive == other.timerActive &&
self.chapterStarted == other.chapterStarted &&
self.chapterComplete == other.chapterComplete &&
// self.chapterTime == other.chapterTime &&
self.chapterStrawberries == other.chapterStrawberries &&
self.chapterCassette == other.chapterCassette &&
self.chapterHeart == other.chapterHeart &&
// self.fileTime == other.fileTime &&
self.fileStrawberries == other.fileStrawberries &&
self.fileCassettes == other.fileCassettes &&
self.fileHearts == other.fileHearts
}

}
64 changes: 52 additions & 12 deletions SwiftSplit/CelesteSplitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class CelesteSplitter {
}
}

private var time: Double = 0.0
private var gameTimeRunning = false
private(set) var nextEventIndex = 0

Expand All @@ -73,12 +74,13 @@ class CelesteSplitter {
return []
}
let events = getEvents(from: autoSplitterInfo, to: info)
// logStateChange(from: autoSplitterInfo, to: info)
logStateChange(from: autoSplitterInfo, to: info)
autoSplitterInfo = info

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

processEvents(events)

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

func logStateChange(from old: AutoSplitterInfo, to new: AutoSplitterInfo) {
if !old.stableStateEquals(other: new) {
let comparisons = [
(name: "chapter", old: "\(old.chapter)", new: "\(new.chapter)"),
(name: "mode", old: "\(old.mode)", new: "\(new.mode)"),
(name: "level", old: "\(old.level)", new: "\(new.level)"),
(name: "timerActive", old: "\(old.timerActive)", new: "\(new.timerActive)"),
(name: "chapterStarted", old: "\(old.chapterStarted)", new: "\(new.chapterStarted)"),
(name: "chapterComplete", old: "\(old.chapterComplete)", new: "\(new.chapterComplete)"),
// (name: "chapterTime", old: "\(old.chapterTime)", new: "\(new.chapterTime)"),
(name: "chapterStrawberries", old: "\(old.chapterStrawberries)", new: "\(new.chapterStrawberries)"),
(name: "chapterCassette", old: "\(old.chapterCassette)", new: "\(new.chapterCassette)"),
(name: "chapterHeart", old: "\(old.chapterHeart)", new: "\(new.chapterHeart)"),
// (name: "fileTime", old: "\(old.fileTime)", new: "\(new.fileTime)"),
(name: "fileStrawberries", old: "\(old.fileStrawberries)", new: "\(new.fileStrawberries)"),
(name: "fileCassettes", old: "\(old.fileCassettes)", new: "\(new.fileCassettes)"),
(name: "fileHearts", old: "\(old.fileHearts)", new: "\(new.fileHearts)"),
]
let changes = comparisons.filter { $0.old != $0.new }

if !changes.isEmpty {
let currentTime = DispatchTime.now()
let delta = Double(currentTime.uptimeNanoseconds - lastStateTime.uptimeNanoseconds) / 1_000_000_000
print(
"[\(delta)] " +
"chapter: \(new.chapter), mode: \(new.mode), level: \(new.level), timerActive: \(new.timerActive), " +
"chapterStarted: \(new.chapterStarted), chapterComplete: \(new.chapterComplete), chapterTime: \(new.chapterTime), " +
"chapterStrawberries: \(new.chapterStrawberries), chapterCassette: \(new.chapterCassette), chapterHeart: \(new.chapterHeart), fileTime: \(new.fileTime), " +
"fileStrawberries: \(new.fileStrawberries), fileCassettes: \(new.fileCassettes), fileHearts: \(new.fileHearts)"
"[real delta: \(delta), chapter: \(new.chapterTime), file: \(new.fileTime)] " + changes.map { "\($0.name): \($0.old) -> \($0.new)" }.joined(separator: ", ")
)
lastStateTime = currentTime
}
Expand Down Expand Up @@ -154,13 +170,37 @@ class CelesteSplitter {
events.append(["\(old.level) > \(new.level)"])
}
if new.chapterCassette && !old.chapterCassette {
events.append(["cassette", "chapter \(new.chapter) cassette", "\(new.fileCassettes) total cassettes"])
events.append([
"collect cassette",
"collect chapter \(new.chapter) cassette",
"collect \(new.fileCassettes) total cassettes",
// compat:
"cassette",
"chapter \(new.chapter) cassette",
"\(new.fileCassettes) total cassettes"
])
}
if new.chapterHeart && !old.chapterHeart {
events.append(["heart", "chapter \(new.chapter) heart", "\(new.fileHearts) total hearts"])
events.append([
"collect heart",
"collect chapter \(new.chapter) heart",
"collect \(new.fileHearts) total hearts",
// compat:
"heart",
"chapter \(new.chapter) heart",
"\(new.fileHearts) total hearts"
])
}
if new.chapterStrawberries > old.chapterStrawberries {
events.append(["strawberry", "\(new.chapterStrawberries) chapter strawberries", "\(new.fileStrawberries) file strawberries"])
events.append([
"collect strawberry",
"collect \(new.chapterStrawberries) chapter strawberries",
"collect \(new.fileStrawberries) file strawberries",
// compat:
"strawberry",
"\(new.chapterStrawberries) chapter strawberries",
"\(new.fileStrawberries) file strawberries"
])
}
return events
}
Expand Down

0 comments on commit 649a4c5

Please sign in to comment.