Skip to content

Commit

Permalink
refactor of accelerated time calculation
Browse files Browse the repository at this point in the history
The `global.settings.at` variable was turned into units of milliseconds
and the hardcoded factor of 2.5 all over the place was
removed. `global.settings.gameSpeed` and `global.settings.atMultiplier`
were introduced and the timers for the game loops were added to
`webWorker` to calculate the correct factor where needed.

`atrack` variable was removed since it was equivalent to
`global.settings.at` and therefore redundant.

Gamespeed was added to topbar and removed from tooltips.

The `atMultiplier-1` is necessary in the topbar display as well as in
`longLoop()` in `src/main.js` because while the game runs at n-times
speed, only (n-1)-times are actually the bonus speed and the remaining
1-times is the normal game which should not count against the bonus
time.
  • Loading branch information
Daxtorim committed Jul 16, 2023
1 parent 05285c0 commit 83306d0
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 68 deletions.
2 changes: 1 addition & 1 deletion evolve/main.js

Large diffs are not rendered by default.

69 changes: 28 additions & 41 deletions src/functions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { global, save, message_logs, message_filters, webWorker, keyMultiplier, intervals, resizeGame, atrack } from './vars.js';
import { global, save, message_logs, message_filters, webWorker, keyMultiplier, intervals, resizeGame } from './vars.js';
import { loc } from './locale.js';
import { races, traits, genus_traits, traitSkin, fathomCheck } from './races.js';
import { actions, actionDesc } from './actions.js';
Expand Down Expand Up @@ -118,54 +118,46 @@ export function gameLoop(act){
clearInterval(intervals['mid_loop']);
clearInterval(intervals['long_loop']);
}
if (global.settings.at > 0){
global.settings.at = atrack.t;
}
webWorker.s = false;
}
break;
case 'start':
{
let main_timer = 250;
let mid_timer = 1000;
let long_timer = 5000;
webWorker.resetTimers()
global.settings.gameSpeed = 1;

calcATime();
if (global.settings.at > 0){
global.settings.gameSpeed *= global.settings.atMultiplier;
}
if (global.race['slow']){
let slow = 1 + (traits.slow.vars()[0] / 100);
main_timer = Math.floor(main_timer * slow);
mid_timer = Math.floor(mid_timer * slow);
long_timer = Math.floor(long_timer * slow);
const slow = 1 - (traits.slow.vars()[0] / 100);
global.settings.gameSpeed *= slow;
}
if (global.race['hyper']){
let fast = 1 - (traits.hyper.vars()[0] / 100);
main_timer = Math.floor(main_timer * fast);
mid_timer = Math.floor(mid_timer * fast);
long_timer = Math.floor(long_timer * fast);
const fast = 1 + (traits.hyper.vars()[0] / 100);
global.settings.gameSpeed *= fast;
}
webWorker.mt = main_timer;

calcATime();

if (atrack.t > 0){
main_timer = Math.ceil(main_timer * 0.5);
mid_timer = Math.ceil(mid_timer * 0.5);
long_timer = Math.ceil(long_timer * 0.5);
for (let key in webWorker.timers) {
webWorker.timers[key] /= global.settings.gameSpeed;
}

if (webWorker.w){
webWorker.w.postMessage({ loop: 'short', period: main_timer });
webWorker.w.postMessage({ loop: 'mid', period: mid_timer });
webWorker.w.postMessage({ loop: 'long', period: long_timer });
webWorker.w.postMessage({ loop: 'short', period: webWorker.timers.main });
webWorker.w.postMessage({ loop: 'mid', period: webWorker.timers.mid });
webWorker.w.postMessage({ loop: 'long', period: webWorker.timers.long });
}
else {
intervals['main_loop'] = setInterval(function(){
fastLoop();
}, main_timer);
}, webWorker.timers.main);
intervals['mid_loop'] = setInterval(function(){
midLoop();
}, mid_timer);
}, webWorker.timers.mid);
intervals['long_loop'] = setInterval(function(){
longLoop();
}, long_timer);
}, webWorker.timers.long);
}

webWorker.s = true;
Expand All @@ -174,19 +166,14 @@ export function gameLoop(act){
}

function calcATime(){
let dt = Date.now();
let timeDiff = dt - global.stats.current;
if (global.stats.hasOwnProperty('current') && (timeDiff >= 120000 || global.settings.at > 0)){
if (global.settings.at > 11520){
global.settings.at = 0;
}
if (timeDiff >= 120000){
global.settings.at += Math.floor(timeDiff / 3333);
}
if (global.settings.at > 11520){
global.settings.at = 11520;
}
atrack.t = global.settings.at;
if (!global.stats.hasOwnProperty("current")) return;

const dt = Date.now();
const timeDiff = dt - global.stats.current;
if (timeDiff >= 120000){
const newATime = global.settings.at + (timeDiff * 2/3);
const maxATime = 8*60*60*1000; // milliseconds
global.settings.at = Math.min(newATime, maxATime);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ export function index(){
<span class="day">${loc('day')} <span class="has-text-warning">{{ city.calendar.day }}</span></span>
<b-tooltip :label="weather()" :aria-label="weather()" position="is-bottom" size="is-small" multilined animated><i id="weather" class="weather wi"></i></b-tooltip>
<b-tooltip :label="temp()" :aria-label="temp()" position="is-bottom" size="is-small" multilined animated><i id="temp" class="temp wi"></i></b-tooltip>
<b-tooltip :label="atRemain()" v-show="s.at" :aria-label="atRemain()" position="is-bottom" size="is-small" multilined animated><span class="atime has-text-caution">{{ s.at | remain }}</span></b-tooltip>
<b-tooltip :label="atRemain()" v-show="s.at" :aria-label="atRemain()" position="is-bottom" size="is-small" multilined animated><span class="atime has-text-caution">{{ s.at / (s.atMultiplier - 1) | remain }}</span></b-tooltip>
<span id="pausegame" class="atime" role="button" @click="pause" :aria-label="pausedesc()"></span>
</span>
</span>
Expand Down
20 changes: 7 additions & 13 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { global, save, seededRandom, webWorker, intervals, keyMap, atrack, resizeGame, breakdown, sizeApproximation, keyMultiplier, power_generated, p_on, support_on, int_on, gal_on, spire_on, set_qlevel, quantum_level } from './vars.js';
import { global, save, seededRandom, webWorker, intervals, keyMap, resizeGame, breakdown, sizeApproximation, keyMultiplier, power_generated, p_on, support_on, int_on, gal_on, spire_on, set_qlevel, quantum_level } from './vars.js';
import { loc } from './locale.js';
import { unlockAchieve, checkAchievements, drawAchieve, alevel, universeAffix, challengeIcon, unlockFeat } from './achieve.js';
import { gameLoop, vBind, popover, clearPopper, flib, tagEvent, timeCheck, arpaTimeCheck, timeFormat, powerModifier, modRes, initMessageQueue, messageQueue, calc_mastery, calcPillar, darkEffect, calcQueueMax, calcRQueueMax, buildQueue, shrineBonusActive, getShrineBonus, eventActive, easterEggBind, trickOrTreatBind, powerGrid, deepClone } from './functions.js';
Expand Down Expand Up @@ -436,13 +436,8 @@ vBind({
return universe === 'standard' || universe === 'bigbang' ? '' : universe_types[universe].name;
},
remain(at){
let minutes = Math.ceil(at * 2.5 / 60);
if (minutes > 0){
let hours = Math.floor(minutes / 60);
minutes -= hours * 60;
return `${hours}:${minutes.toString().padStart(2,'0')}`;
}
return;
const f = timeFormat(at / 1000)
return `${global.settings.atMultiplier}x Speed [${f}]`
}
}
});
Expand Down Expand Up @@ -7041,7 +7036,7 @@ function fastLoop(){
// main resource delta tracking
Object.keys(global.resource).forEach(function (res) {
if (global['resource'][res].rate > 0 || (global['resource'][res].rate === 0 && global['resource'][res].max === -1)){
diffCalc(res,webWorker.mt);
diffCalc(res, webWorker.timers.main);
}
});

Expand Down Expand Up @@ -11178,10 +11173,9 @@ function longLoop(){
if (global.settings.pause && webWorker.s){
gameLoop('stop');
}
if (atrack.t > 0){
atrack.t--;
global.settings.at--;
if (global.settings.at <= 0 || atrack.t <= 0){
if (global.settings.at > 0){
global.settings.at -= webWorker.timers.long * (global.settings.atMultiplier - 1);
if (global.settings.at <= 0){
global.settings.at = 0;
gameLoop('stop');
gameLoop('start');
Expand Down
13 changes: 11 additions & 2 deletions src/vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export var spire_on = {};
export var quantum_level = 0;
export var achieve_level = 0;
export var universe_level = 0;
export var atrack = {t:0};
export function set_qlevel(q_level){
quantum_level = q_level;
}
Expand Down Expand Up @@ -1664,6 +1663,9 @@ $('html').addClass(global.settings.theme);
if (!global.settings['at']){
global.settings['at'] = 0;
}
if (!global.settings['atMultiplier']){
global.settings['atMultiplier'] = 2;
}

if (!global.city['morale']){
global.city['morale'] = {
Expand Down Expand Up @@ -2131,7 +2133,14 @@ window.soft_reset = function reset(source){
window.location.reload();
}

export var webWorker = { w: false, s: false, mt: 250 };
export var webWorker = {
w: false,
s: false,
timers: {},
resetTimers() {
this.timers = {main: 250, mid: 1000, long: 5000}
}
}
export var intervals = {};

export function clearSavedMessages(){
Expand Down
4 changes: 2 additions & 2 deletions strings/strings.im-PL.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"key_mappings": "Eykay Appingsmay",
"tab_mappings": "Abtay Appingsmay",
"multiplier": "%0X Ultipliermay",
"accelerated_time": "Acceleratedway (2x) Imetay Emainingray",
"accelerated_time": "Acceleratedway Imetay Emainingray",
"c_cat": "Itycay Ategorizationcay",
"t_cat": "Esearchray Ategorizationcay",
"q_key": "Ueueqay Eykay",
Expand Down Expand Up @@ -8971,4 +8971,4 @@
"wiki_events_xmas_para5": "Ethay 2020 iftgay ontainedcay: 100 + Esetsray + Admay Esetsray Asmidsplay, Ioseedsbay + Ataclysmscay Agephay, Ifetimelay oldiersay eathsday Oalcay, 0-4 Arkday Energyway, 0-5 Armonyhay Ystalscray, 0-25 Oodblay Onesstay, andway 0-5 Artifactsway.",
"wiki_events_xmas_para6": "Ethay 2021 iftgay ontainedcay: 100 + Esetsray + Admay Esetsray Asmidsplay, Ioseedsbay + Ataclysmscay Agephay, Ifetimelay oldiersay eathsday Oalcay, 0-4 Arkday Energyway, 0-5 Armonyhay Ystalscray, 0-25 Oodblay Onesstay, 0-5 Artifactsway, andway 0-5 AIWAY Orescay.",
"wiki_events_xmas_condition_para1": "Occursway onway Ecemberday 25th"
}
}
2 changes: 1 addition & 1 deletion strings/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"key_mappings": "Key Mappings",
"tab_mappings": "Tab Mappings",
"multiplier": "%0X Multiplier",
"accelerated_time": "Accelerated (2x) Time Remaining",
"accelerated_time": "Accelerated Time Remaining",
"c_cat": "City Categorization",
"t_cat": "Research Categorization",
"q_key": "Queue Key",
Expand Down
2 changes: 1 addition & 1 deletion strings/strings.ko-KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"key_mappings": "단축키 설정",
"tab_mappings": "탭 매핑",
"multiplier": "%0X 배수",
"accelerated_time": "잔여 가속 (2x) 시간",
"accelerated_time": "잔여 가속 시간",
"c_cat": "건물 분류",
"t_cat": "기술 카테고리 표시",
"q_key": "대기열 키",
Expand Down
2 changes: 1 addition & 1 deletion strings/strings.pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"key_mappings": "Mapeamento de Botões",
"tab_mappings": "Mapeamento de Abas",
"multiplier": "%0X Multiplicador",
"accelerated_time": "Tempo Acelerado (2x) Restante",
"accelerated_time": "Tempo Acelerado Restante",
"c_cat": "Categorizar Cidade",
"t_cat": "Categorizar Pesquisas",
"q_key": "Botão de Fila",
Expand Down
4 changes: 2 additions & 2 deletions strings/strings.ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"key_mappings": "Назначение горячих клавиш",
"tab_mappings": "Горячие клавиши вкладок",
"multiplier": "Повторить %0 раз",
"accelerated_time": "Осталось ускоренного (2x) времени",
"accelerated_time": "Осталось ускоренного времени",
"c_cat": "Городское деление",
"t_cat": "Группы исследований",
"q_key": "Клавиша очереди",
Expand Down Expand Up @@ -8568,4 +8568,4 @@
"wiki_events_xmas_para5": "В 2020 году в подарке было: (100 + кол-во Всех Сбросов + Сбросов Взаимного уничтожения) Плазмид, (Панспермий + Катаклизмов) Фагов, (Всех погибших Солдат) Угля, 0-4 Темной Энергии, 0-5 Кристаллов Гармонии, 0-25 Камней Крови и 0-5 Артефактов.",
"wiki_events_xmas_para6": "В 2021 году в подарке было: (100 + кол-во Всех Сбросов + Сбросов Взаимного уничтожения) Плазмид, (Панспермий + Катаклизмов) Фагов, (Всех погибших Солдат) Угля, 0-4 Темной Энергии, 0-5 Кристаллов Гармонии, 0-25 Камней Крови, 0-5 Артефактов и 0-5 Ядер ИИ.",
"wiki_events_xmas_condition_para1": "Проходит 25 декабря."
}
}
2 changes: 1 addition & 1 deletion strings/strings.zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"key_mappings": "键盘映射",
"tab_mappings": "面板映射",
"multiplier": "%0倍率",
"accelerated_time": "加速(2倍速)时间剩余",
"accelerated_time": "加速时间剩余",
"c_cat": "建筑分类",
"t_cat": "科研分类",
"q_key": "队列快捷键",
Expand Down
2 changes: 1 addition & 1 deletion strings/strings.zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"key_mappings": "鍵盤映射",
"tab_mappings": "面板映射",
"multiplier": "%0倍率",
"accelerated_time": "加速(2倍速)時間剩餘",
"accelerated_time": "加速時間剩餘",
"c_cat": "建築分類",
"t_cat": "科研分類",
"q_key": "隊列快捷鍵",
Expand Down
2 changes: 1 addition & 1 deletion wiki/wiki.js

Large diffs are not rendered by default.

0 comments on commit 83306d0

Please sign in to comment.