Converted variables that track time to numbers

This commit is contained in:
thepaperpilot 2022-03-11 14:02:41 -06:00
parent b33de01a96
commit e56f34c13c
13 changed files with 44 additions and 44 deletions

View file

@ -9,7 +9,7 @@ import { createReset } from "features/reset";
import MainDisplay from "features/resources/MainDisplay.vue";
import { createResource } from "features/resources/resource";
import { createLayer } from "game/layers";
import { DecimalSource } from "lib/break_eternity";
import { DecimalSource } from "util/bignum";
import { render } from "util/vue";
import { createLayerTreeNode, createResetButton } from "../common";

View file

@ -5,8 +5,7 @@ import { branchedResetPropagation, createTree, GenericTree } from "features/tree
import { globalBus } from "game/events";
import { createLayer, GenericLayer } from "game/layers";
import player, { PlayerData } from "game/player";
import { DecimalSource } from "lib/break_eternity";
import Decimal, { format, formatTime } from "util/bignum";
import Decimal, { DecimalSource, format, formatTime } from "util/bignum";
import { render } from "util/vue";
import { computed, toRaw } from "vue";
import prestige from "./layers/prestige";

View file

@ -9,7 +9,7 @@ import {
StyleValue,
Visibility
} from "features/feature";
import { DecimalSource } from "lib/break_eternity";
import { DecimalSource } from "util/bignum";
import {
Computable,
GetComputableType,

View file

@ -11,7 +11,6 @@ import {
} from "features/feature";
import { globalBus } from "game/events";
import { State, Persistent, makePersistent, PersistentState } from "game/persistence";
import Decimal, { DecimalSource } from "lib/break_eternity";
import { isFunction } from "util/common";
import {
Computable,
@ -85,7 +84,7 @@ export interface NodeTypeOptions {
actionDistance?: NodeComputable<number>;
onClick?: (node: BoardNode) => void;
onDrop?: (node: BoardNode, otherNode: BoardNode) => void;
update?: (node: BoardNode, diff: DecimalSource) => void;
update?: (node: BoardNode, diff: number) => void;
}
export interface BaseNodeType {
@ -348,7 +347,7 @@ export function getUniqueNodeID(board: GenericBoard): number {
const listeners: Record<string, Unsubscribe | undefined> = {};
globalBus.on("addLayer", layer => {
const boards: GenericBoard[] = findFeatures(layer, BoardType) as GenericBoard[];
listeners[layer.id] = layer.on("postUpdate", (diff: Decimal) => {
listeners[layer.id] = layer.on("postUpdate", diff => {
boards.forEach(board => {
Object.values(board.types).forEach(type =>
type.nodes.value.forEach(node => type.update?.(node, diff))

View file

@ -206,7 +206,7 @@ export function setupPassiveGeneration(
conversion: GenericConversion,
rate: ProcessedComputable<DecimalSource> = 1
): void {
layer.on("preUpdate", (diff: Decimal) => {
layer.on("preUpdate", diff => {
const currRate = isRef(rate) ? rate.value : rate;
if (Decimal.neq(currRate, 0)) {
conversion.gainResource.value = Decimal.add(

View file

@ -8,7 +8,7 @@ import {
PersistentRef,
PersistentState
} from "game/persistence";
import Decimal from "lib/break_eternity";
import Decimal from "util/bignum";
import { Computable, GetComputableType, processComputable } from "util/computed";
import { createLazyProxy } from "util/proxies";
import { Unsubscribe } from "nanoevents";
@ -72,7 +72,7 @@ export function createReset<T extends ResetOptions>(
const listeners: Record<string, Unsubscribe | undefined> = {};
export function trackResetTime(layer: GenericLayer, reset: GenericReset): PersistentRef<Decimal> {
const resetTime = persistent<Decimal>(new Decimal(0));
listeners[layer.id] = layer.on("preUpdate", (diff: Decimal) => {
listeners[layer.id] = layer.on("preUpdate", diff => {
resetTime.value = Decimal.add(resetTime.value, diff);
});
globalBus.on("reset", currentReset => {

View file

@ -14,8 +14,7 @@ import { displayResource, Resource } from "features/resources/resource";
import { Tooltip } from "features/tooltip";
import TreeComponent from "features/trees/Tree.vue";
import { persistent } from "game/persistence";
import { DecimalSource, format } from "util/bignum";
import Decimal, { formatWhole } from "util/break_eternity";
import Decimal, { DecimalSource, format, formatWhole } from "util/bignum";
import {
Computable,
convertComputable,

View file

@ -31,7 +31,7 @@ import MarkNode from "components/MarkNode.vue";
import { jsx, StyleValue, Visibility } from "features/feature";
import { displayResource, Resource } from "features/resources/resource";
import { GenericUpgrade } from "features/upgrades/upgrade";
import { DecimalSource } from "lib/break_eternity";
import { DecimalSource } from "util/bignum";
import { coerceComponent, isCoercableComponent, processedPropType, unwrapRef } from "util/vue";
import {
Component,

View file

@ -1,5 +1,5 @@
import projInfo from "data/projInfo.json";
import Decimal, { DecimalSource } from "util/bignum";
import Decimal from "util/bignum";
import { createNanoEvents } from "nanoevents";
import { App, Ref } from "vue";
import { GenericLayer } from "./layers";
@ -10,7 +10,7 @@ import state from "./state";
export interface GlobalEvents {
addLayer: (layer: GenericLayer, saveData: Record<string, unknown>) => void;
removeLayer: (layer: GenericLayer) => void;
update: (diff: Decimal, trueDiff: number) => void;
update: (diff: number, trueDiff: number) => void;
loadSettings: (settings: Partial<Settings>) => void;
setupVue: (vue: App) => void;
}
@ -25,7 +25,7 @@ let hasWon: null | Ref<boolean> = null;
function update() {
const now = Date.now();
let diff: DecimalSource = (now - player.time) / 1e3;
let diff = (now - player.time) / 1e3;
player.time = now;
const trueDiff = diff;
@ -43,7 +43,7 @@ function update() {
return;
}
diff = new Decimal(diff).max(0);
diff = Math.max(diff, 0);
if (player.devSpeed === 0) {
return;
@ -52,14 +52,14 @@ function update() {
// Add offline time if any
if (player.offlineTime != undefined) {
if (Decimal.gt(player.offlineTime, projInfo.offlineLimit * 3600)) {
player.offlineTime = new Decimal(projInfo.offlineLimit * 3600);
player.offlineTime = projInfo.offlineLimit * 3600;
}
if (Decimal.gt(player.offlineTime, 0) && player.devSpeed !== 0) {
const offlineDiff = Decimal.div(player.offlineTime, 10).max(diff);
player.offlineTime = Decimal.sub(player.offlineTime, offlineDiff);
diff = diff.add(offlineDiff);
const offlineDiff = Math.max(player.offlineTime / 10, diff);
player.offlineTime = player.offlineTime - offlineDiff;
diff += offlineDiff;
} else if (player.devSpeed === 0) {
player.offlineTime = Decimal.add(player.offlineTime, diff);
player.offlineTime += diff;
}
if (!player.offlineProd || Decimal.lt(player.offlineTime, 0)) {
player.offlineTime = null;
@ -67,18 +67,26 @@ function update() {
}
// Cap at max tick length
diff = Decimal.min(diff, projInfo.maxTickLength);
diff = Math.min(diff, projInfo.maxTickLength);
// Apply dev speed
if (player.devSpeed != undefined) {
diff = diff.times(player.devSpeed);
diff *= player.devSpeed;
}
if (!Number.isFinite(diff)) {
diff = 1e308;
}
// Update
if (diff.eq(0)) {
if (Decimal.eq(diff, 0)) {
return;
}
player.timePlayed = Decimal.add(player.timePlayed, diff);
player.timePlayed += diff;
if (!Number.isFinite(player.timePlayed)) {
player.timePlayed = 1e308;
}
globalBus.emit("update", diff, trueDiff);
if (settings.unthrottled) {

View file

@ -8,7 +8,6 @@ import {
StyleValue
} from "features/feature";
import { Link } from "features/links";
import Decimal from "util/bignum";
import {
Computable,
GetComputableType,
@ -25,11 +24,11 @@ import player from "./player";
export interface LayerEvents {
// Generation
preUpdate: (diff: Decimal) => void;
preUpdate: (diff: number) => void;
// Actions (e.g. automation)
update: (diff: Decimal) => void;
update: (diff: number) => void;
// Effects (e.g. milestones)
postUpdate: (diff: Decimal) => void;
postUpdate: (diff: number) => void;
}
export const layers: Record<string, Readonly<GenericLayer> | undefined> = {};

View file

@ -1,4 +1,4 @@
import Decimal, { DecimalSource } from "util/bignum";
import Decimal from "util/bignum";
import { isPlainObject } from "util/common";
import { ProxiedWithState, ProxyPath, ProxyState } from "util/proxies";
import { reactive, unref } from "vue";
@ -6,14 +6,14 @@ import transientState from "./state";
export interface PlayerData {
id: string;
devSpeed: DecimalSource | null;
devSpeed: number | null;
name: string;
tabs: Array<string>;
time: number;
autosave: boolean;
offlineProd: boolean;
offlineTime: DecimalSource | null;
timePlayed: DecimalSource;
offlineTime: number | null;
timePlayed: number;
keepGoing: boolean;
modID: string;
modVersion: string;
@ -31,7 +31,7 @@ const state = reactive<PlayerData>({
autosave: true,
offlineProd: true,
offlineTime: null,
timePlayed: new Decimal(0),
timePlayed: 0,
keepGoing: false,
modID: "",
modVersion: "",

View file

@ -5,7 +5,7 @@ import { GenericLayer } from "./game/layers";
import { PlayerData } from "./game/player";
import { Settings } from "./game/settings";
import { Transient } from "./game/state";
import Decimal, { DecimalSource } from "./lib/break_eternity";
import Decimal, { DecimalSource } from "./util/bignum";
import { load } from "./util/save";
document.title = projInfo.title;

View file

@ -1,7 +1,6 @@
import projInfo from "data/projInfo.json";
import player, { Player, PlayerData, stringifySave } from "game/player";
import settings, { loadSettings } from "game/settings";
import Decimal from "./bignum";
import { ProxyState } from "./proxies";
export function setupInitialStore(player: Partial<PlayerData> = {}): Player {
@ -13,8 +12,8 @@ export function setupInitialStore(player: Partial<PlayerData> = {}): Player {
time: Date.now(),
autosave: true,
offlineProd: true,
offlineTime: new Decimal(0),
timePlayed: new Decimal(0),
offlineTime: 0,
timePlayed: 0,
keepGoing: false,
modID: projInfo.id,
modVersion: projInfo.versionNumber,
@ -85,11 +84,8 @@ export async function loadSave(playerObj: Partial<PlayerData>): Promise<void> {
playerObj = setupInitialStore(playerObj);
if (playerObj.offlineProd && playerObj.time) {
if (playerObj.offlineTime == undefined) playerObj.offlineTime = new Decimal(0);
playerObj.offlineTime = Decimal.add(
playerObj.offlineTime,
(Date.now() - playerObj.time) / 1000
);
if (playerObj.offlineTime == undefined) playerObj.offlineTime = 0;
playerObj.offlineTime += (Date.now() - playerObj.time) / 1000;
}
playerObj.time = Date.now();
if (playerObj.modVersion !== projInfo.versionNumber) {