BlueGit

timfa questward

Add an Autorun setting that runs when the path is long enough.

Client-only gait switching resumes after stamina recovers to half; following skips autorun.

timfa Tim Falken · pushed by timfa

Authored 2026-08-30 · 2 months ago

Hash
13cec3f4915c942b46dbbaa81dfc05923e354c0c
Parent
7c0f459a
4 files changed +125 −8
client/index.html +8 −0
@@ -20,6 +20,7 @@
20 20 <button id="leave" type="button">Leave world</button>
21 21 </div>
22 22 <button id="settings-toggle" type="button" hidden title="Settings" aria-label="Settings">⚙</button>
23 <div id="xp-gain-hud" hidden aria-live="polite"></div>
23 24 <aside id="settings-panel" hidden>
24 25 <header>
25 26 <h2>Settings</h2>
@@ -53,6 +54,13 @@
53 54 <button type="button" data-graphics-key="softTerrain" data-graphics-value="off">Off</button>
54 55 </div>
55 56 </section>
57 <section class="settings-group">
58 <h3>Autorun</h3>
59 <div class="settings-options">
60 <button type="button" data-graphics-key="autorun" data-graphics-value="on">On</button>
61 <button type="button" data-graphics-key="autorun" data-graphics-value="off">Off</button>
62 </div>
63 </section>
56 64 </aside>
57 65 <div id="toast-stack" aria-live="polite"></div>
58 66 <div id="world-tooltip" hidden></div>
@@ -20,6 +20,7 @@
20 <button id="leave" type="button">Leave world</button>
20 <button id="leave" type="button">Leave world</button>
21 </div>
21 </div>
22 <button id="settings-toggle" type="button" hidden title="Settings" aria-label="Settings">⚙</button>
22 <button id="settings-toggle" type="button" hidden title="Settings" aria-label="Settings">⚙</button>
23 <div id="xp-gain-hud" hidden aria-live="polite"></div>
23 <aside id="settings-panel" hidden>
24 <aside id="settings-panel" hidden>
24 <header>
25 <header>
25 <h2>Settings</h2>
26 <h2>Settings</h2>
@@ -53,6 +54,13 @@
53 <button type="button" data-graphics-key="softTerrain" data-graphics-value="off">Off</button>
54 <button type="button" data-graphics-key="softTerrain" data-graphics-value="off">Off</button>
54 </div>
55 </div>
55 </section>
56 </section>
57 <section class="settings-group">
58 <h3>Autorun</h3>
59 <div class="settings-options">
60 <button type="button" data-graphics-key="autorun" data-graphics-value="on">On</button>
61 <button type="button" data-graphics-key="autorun" data-graphics-value="off">Off</button>
62 </div>
63 </section>
56 </aside>
64 </aside>
57 <div id="toast-stack" aria-live="polite"></div>
65 <div id="toast-stack" aria-live="polite"></div>
58 <div id="world-tooltip" hidden></div>
66 <div id="world-tooltip" hidden></div>
client/src/play/graphicsSettings.js +1 −1
@@ -118,4 +118,4 @@ export function initGraphicsSettings ({ onChange, onSuppressWorldClick })
118 118 };
119 119 }
120 120
121 export { ViewDistance, LightingMode, BloomMode };
121 export { ViewDistance, LightingMode, BloomMode, AutorunMode };
@@ -118,4 +118,4 @@ export function initGraphicsSettings ({ onChange, onSuppressWorldClick })
118 };
118 };
119}
119}
120
120
121export { ViewDistance, LightingMode, BloomMode };
121export { ViewDistance, LightingMode, BloomMode, AutorunMode };
client/src/play/main.js +104 −5
@@ -95,9 +95,100 @@ let pendingInteract = null;
95 95 let uiFocus = null;
96 96 /** Last approach moveTo so we do not re-path every frame (resets server walkCarry). */
97 97 let approachMove = null;
98 /** Client-tracked follow target; server follow owns gait while this is set. */
99 let followTargetId = null;
100 /** Destination of the active click/approach moveTo for autorun distance checks. */
101 let moveDest = null;
102 /** After stamina hits 0 while autorunning, wait until ≥50% before running again. */
103 let autorunWaitingStamina = false;
98 104 /** Framework can report mousePressed for two frames; latch so one physical click is one command. */
99 105 let leftClickConsumed = false;
100 106
107 const AUTORUN_DISTANCE_TILES = 3;
108 const AUTORUN_STAMINA_RESUME_RATIO = 0.5;
109
110 function sendPlayerMoveTo (tileX, tileY)
111 {
112 followTargetId = null;
113 moveDest = { tileX: tileX | 0, tileY: tileY | 0 };
114 socket.send({ type: MessageType.moveTo, tileX: moveDest.tileX, tileY: moveDest.tileY });
115 syncAutorun();
116 }
117
118 function sendPlayerFollow (targetEntityId)
119 {
120 followTargetId = targetEntityId | 0;
121 moveDest = null;
122 autorunWaitingStamina = false;
123 socket.send({ type: MessageType.follow, targetEntityId: followTargetId });
124 }
125
126 function requestGaitIfNeeded (next)
127 {
128 if (next !== GAIT.walk && next !== GAIT.run)
129 { return; }
130 if (next === gait)
131 { return; }
132 if (next === GAIT.run && (staminaUi?.getCurrent?.() ?? 0) <= 0)
133 { return; }
134 socket.send({ type: MessageType.setGait, gait: next });
135 if (next === GAIT.walk)
136 {
137 gait = next;
138 const el = document.getElementById("gait");
139 if (el)
140 { el.textContent = gait; }
141 }
142 }
143
144 function syncAutorun ()
145 {
146 if (!playing || followTargetId != null)
147 { return; }
148 const settings = graphicsSettingsUi?.getSettings?.() ?? {};
149 if (settings.autorun !== "on")
150 { return; }
151 const local = view?.entities?.get(view.localEntityId);
152 if (!local || !moveDest)
153 { return; }
154 if (local.tileX === moveDest.tileX && local.tileY === moveDest.tileY)
155 {
156 moveDest = null;
157 autorunWaitingStamina = false;
158 requestGaitIfNeeded(GAIT.walk);
159 return;
160 }
161 const dist = chebyshev(local.tileX, local.tileY, moveDest.tileX, moveDest.tileY);
162 const wantRun = dist > AUTORUN_DISTANCE_TILES;
163 const current = staminaUi?.getCurrent?.() ?? 0;
164 const max = Math.max(1, staminaUi?.getMax?.() ?? 1);
165 if (gait === GAIT.run && current <= 0)
166 { autorunWaitingStamina = true; }
167 if (autorunWaitingStamina)
168 {
169 if (wantRun && current / max >= AUTORUN_STAMINA_RESUME_RATIO)
170 {
171 autorunWaitingStamina = false;
172 requestGaitIfNeeded(GAIT.run);
173 }
174 else
175 { requestGaitIfNeeded(GAIT.walk); }
176 return;
177 }
178 if (wantRun)
179 {
180 if (current <= 0)
181 {
182 autorunWaitingStamina = true;
183 requestGaitIfNeeded(GAIT.walk);
184 }
185 else
186 { requestGaitIfNeeded(GAIT.run); }
187 }
188 else
189 { requestGaitIfNeeded(GAIT.walk); }
190 }
191
101 192 function consumeWorldClick ()
102 193 {
103 194 leftClickConsumed = true;
@@ -185,6 +276,7 @@ function applyGraphicsSettings (settings)
185 276 viewDistance: settings.viewDistance
186 277 });
187 278 }
279 syncAutorun();
188 280 }
189 281
190 282 function clearPendingPickup ()
@@ -268,7 +360,7 @@ function approachAdjacentTile (tileX, tileY)
268 360 destX,
269 361 destY
270 362 };
271 socket.send({ type: MessageType.moveTo, tileX: destX, tileY: destY });
363 sendPlayerMoveTo(destX, destY);
272 364 return false;
273 365 }
274 366
@@ -723,7 +815,7 @@ function requestPickup (ground)
723 815 flushPendingPickup();
724 816 return;
725 817 }
726 socket.send({ type: MessageType.moveTo, tileX: ground.tileX, tileY: ground.tileY });
818 sendPlayerMoveTo(ground.tileX, ground.tileY);
727 819 }
728 820
729 821 function flushPendingPickup ()
@@ -852,7 +944,7 @@ function handleLeftClick (mouseX, mouseY)
852 944 else
853 945 {
854 946 clearAllPendingActions();
855 socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY });
947 sendPlayerMoveTo(tile.tileX, tile.tileY);
856 948 }
857 949 }
858 950
@@ -875,7 +967,7 @@ function handleRightClick (event, mouseX, mouseY)
875 967 onSelect: () =>
876 968 {
877 969 clearAllPendingActions();
878 socket.send({ type: MessageType.moveTo, tileX: walkTileX, tileY: walkTileY });
970 sendPlayerMoveTo(walkTileX, walkTileY);
879 971 }
880 972 });
881 973
@@ -889,7 +981,7 @@ function handleRightClick (event, mouseX, mouseY)
889 981 onSelect: () =>
890 982 {
891 983 clearAllPendingActions();
892 socket.send({ type: MessageType.follow, targetEntityId: entity.entityId });
984 sendPlayerFollow(entity.entityId);
893 985 }
894 986 });
895 987 if (entity.untargetable)
@@ -1202,6 +1294,7 @@ function startPlay (payload)
1202 1294 {
1203 1295 flushPendingPickup();
1204 1296 flushPendingInteract();
1297 syncAutorun();
1205 1298 social?.updateNametag(view, scene, view.localEntityId);
1206 1299 chat?.update(view, scene);
1207 1300 updateSpellTargetHighlight();
@@ -1644,7 +1737,10 @@ async function main ()
1644 1737 {
1645 1738 gait = message.gait;
1646 1739 document.getElementById("gait").textContent = gait;
1740 if (gait === GAIT.walk && (staminaUi?.getCurrent?.() ?? 0) <= 0)
1741 { autorunWaitingStamina = true; }
1647 1742 }
1743 syncAutorun();
1648 1744 break;
1649 1745 case MessageType.statusEffects:
1650 1746 view?.setLocalBlind(!!message.blind);
@@ -1909,6 +2005,9 @@ async function main ()
1909 2005 function returnToMenu (reason = "")
1910 2006 {
1911 2007 playing = false;
2008 followTargetId = null;
2009 moveDest = null;
2010 autorunWaitingStamina = false;
1912 2011 clearAllPendingActions();
1913 2012 clearUiFocus();
1914 2013 social?.reset();
@@ -95,9 +95,100 @@ let pendingInteract = null;
95let uiFocus = null;
95let uiFocus = null;
96/** Last approach moveTo so we do not re-path every frame (resets server walkCarry). */
96/** Last approach moveTo so we do not re-path every frame (resets server walkCarry). */
97let approachMove = null;
97let approachMove = null;
98/** Client-tracked follow target; server follow owns gait while this is set. */
99let followTargetId = null;
100/** Destination of the active click/approach moveTo for autorun distance checks. */
101let moveDest = null;
102/** After stamina hits 0 while autorunning, wait until ≥50% before running again. */
103let autorunWaitingStamina = false;
98/** Framework can report mousePressed for two frames; latch so one physical click is one command. */
104/** Framework can report mousePressed for two frames; latch so one physical click is one command. */
99let leftClickConsumed = false;
105let leftClickConsumed = false;
100
106
107const AUTORUN_DISTANCE_TILES = 3;
108const AUTORUN_STAMINA_RESUME_RATIO = 0.5;
109
110function sendPlayerMoveTo (tileX, tileY)
111{
112 followTargetId = null;
113 moveDest = { tileX: tileX | 0, tileY: tileY | 0 };
114 socket.send({ type: MessageType.moveTo, tileX: moveDest.tileX, tileY: moveDest.tileY });
115 syncAutorun();
116}
117
118function sendPlayerFollow (targetEntityId)
119{
120 followTargetId = targetEntityId | 0;
121 moveDest = null;
122 autorunWaitingStamina = false;
123 socket.send({ type: MessageType.follow, targetEntityId: followTargetId });
124}
125
126function requestGaitIfNeeded (next)
127{
128 if (next !== GAIT.walk && next !== GAIT.run)
129 { return; }
130 if (next === gait)
131 { return; }
132 if (next === GAIT.run && (staminaUi?.getCurrent?.() ?? 0) <= 0)
133 { return; }
134 socket.send({ type: MessageType.setGait, gait: next });
135 if (next === GAIT.walk)
136 {
137 gait = next;
138 const el = document.getElementById("gait");
139 if (el)
140 { el.textContent = gait; }
141 }
142}
143
144function syncAutorun ()
145{
146 if (!playing || followTargetId != null)
147 { return; }
148 const settings = graphicsSettingsUi?.getSettings?.() ?? {};
149 if (settings.autorun !== "on")
150 { return; }
151 const local = view?.entities?.get(view.localEntityId);
152 if (!local || !moveDest)
153 { return; }
154 if (local.tileX === moveDest.tileX && local.tileY === moveDest.tileY)
155 {
156 moveDest = null;
157 autorunWaitingStamina = false;
158 requestGaitIfNeeded(GAIT.walk);
159 return;
160 }
161 const dist = chebyshev(local.tileX, local.tileY, moveDest.tileX, moveDest.tileY);
162 const wantRun = dist > AUTORUN_DISTANCE_TILES;
163 const current = staminaUi?.getCurrent?.() ?? 0;
164 const max = Math.max(1, staminaUi?.getMax?.() ?? 1);
165 if (gait === GAIT.run && current <= 0)
166 { autorunWaitingStamina = true; }
167 if (autorunWaitingStamina)
168 {
169 if (wantRun && current / max >= AUTORUN_STAMINA_RESUME_RATIO)
170 {
171 autorunWaitingStamina = false;
172 requestGaitIfNeeded(GAIT.run);
173 }
174 else
175 { requestGaitIfNeeded(GAIT.walk); }
176 return;
177 }
178 if (wantRun)
179 {
180 if (current <= 0)
181 {
182 autorunWaitingStamina = true;
183 requestGaitIfNeeded(GAIT.walk);
184 }
185 else
186 { requestGaitIfNeeded(GAIT.run); }
187 }
188 else
189 { requestGaitIfNeeded(GAIT.walk); }
190}
191
101function consumeWorldClick ()
192function consumeWorldClick ()
102{
193{
103 leftClickConsumed = true;
194 leftClickConsumed = true;
@@ -185,6 +276,7 @@ function applyGraphicsSettings (settings)
185 viewDistance: settings.viewDistance
276 viewDistance: settings.viewDistance
186 });
277 });
187 }
278 }
279 syncAutorun();
188}
280}
189
281
190function clearPendingPickup ()
282function clearPendingPickup ()
@@ -268,7 +360,7 @@ function approachAdjacentTile (tileX, tileY)
268 destX,
360 destX,
269 destY
361 destY
270 };
362 };
271 socket.send({ type: MessageType.moveTo, tileX: destX, tileY: destY });
363 sendPlayerMoveTo(destX, destY);
272 return false;
364 return false;
273}
365}
274
366
@@ -723,7 +815,7 @@ function requestPickup (ground)
723 flushPendingPickup();
815 flushPendingPickup();
724 return;
816 return;
725 }
817 }
726 socket.send({ type: MessageType.moveTo, tileX: ground.tileX, tileY: ground.tileY });
818 sendPlayerMoveTo(ground.tileX, ground.tileY);
727}
819}
728
820
729function flushPendingPickup ()
821function flushPendingPickup ()
@@ -852,7 +944,7 @@ function handleLeftClick (mouseX, mouseY)
852 else
944 else
853 {
945 {
854 clearAllPendingActions();
946 clearAllPendingActions();
855 socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY });
947 sendPlayerMoveTo(tile.tileX, tile.tileY);
856 }
948 }
857}
949}
858
950
@@ -875,7 +967,7 @@ function handleRightClick (event, mouseX, mouseY)
875 onSelect: () =>
967 onSelect: () =>
876 {
968 {
877 clearAllPendingActions();
969 clearAllPendingActions();
878 socket.send({ type: MessageType.moveTo, tileX: walkTileX, tileY: walkTileY });
970 sendPlayerMoveTo(walkTileX, walkTileY);
879 }
971 }
880 });
972 });
881
973
@@ -889,7 +981,7 @@ function handleRightClick (event, mouseX, mouseY)
889 onSelect: () =>
981 onSelect: () =>
890 {
982 {
891 clearAllPendingActions();
983 clearAllPendingActions();
892 socket.send({ type: MessageType.follow, targetEntityId: entity.entityId });
984 sendPlayerFollow(entity.entityId);
893 }
985 }
894 });
986 });
895 if (entity.untargetable)
987 if (entity.untargetable)
@@ -1202,6 +1294,7 @@ function startPlay (payload)
1202 {
1294 {
1203 flushPendingPickup();
1295 flushPendingPickup();
1204 flushPendingInteract();
1296 flushPendingInteract();
1297 syncAutorun();
1205 social?.updateNametag(view, scene, view.localEntityId);
1298 social?.updateNametag(view, scene, view.localEntityId);
1206 chat?.update(view, scene);
1299 chat?.update(view, scene);
1207 updateSpellTargetHighlight();
1300 updateSpellTargetHighlight();
@@ -1644,7 +1737,10 @@ async function main ()
1644 {
1737 {
1645 gait = message.gait;
1738 gait = message.gait;
1646 document.getElementById("gait").textContent = gait;
1739 document.getElementById("gait").textContent = gait;
1740 if (gait === GAIT.walk && (staminaUi?.getCurrent?.() ?? 0) <= 0)
1741 { autorunWaitingStamina = true; }
1647 }
1742 }
1743 syncAutorun();
1648 break;
1744 break;
1649 case MessageType.statusEffects:
1745 case MessageType.statusEffects:
1650 view?.setLocalBlind(!!message.blind);
1746 view?.setLocalBlind(!!message.blind);
@@ -1909,6 +2005,9 @@ async function main ()
1909function returnToMenu (reason = "")
2005function returnToMenu (reason = "")
1910{
2006{
1911 playing = false;
2007 playing = false;
2008 followTargetId = null;
2009 moveDest = null;
2010 autorunWaitingStamina = false;
1912 clearAllPendingActions();
2011 clearAllPendingActions();
1913 clearUiFocus();
2012 clearUiFocus();
1914 social?.reset();
2013 social?.reset();
shared/graphics/settings.js +12 −2
@@ -21,11 +21,18 @@ export const SoftTerrainMode = Object.freeze({
21 21 off: "off"
22 22 });
23 23
24 /** Auto walk/run by path distance (client-only; ignored while following). */
25 export const AutorunMode = Object.freeze({
26 on: "on",
27 off: "off"
28 });
29
24 30 export const DEFAULT_GRAPHICS_SETTINGS = Object.freeze({
25 31 viewDistance: ViewDistance.long,
26 32 lighting: LightingMode.soft,
27 33 bloom: BloomMode.on,
28 softTerrain: SoftTerrainMode.on
34 softTerrain: SoftTerrainMode.on,
35 autorun: AutorunMode.off
29 36 });
30 37
31 38 const STORAGE_KEY = "questward-graphics-settings";
@@ -45,7 +52,10 @@ export function normalizeGraphicsSettings (value)
45 52 const softTerrain = raw.softTerrain === SoftTerrainMode.off
46 53 ? SoftTerrainMode.off
47 54 : SoftTerrainMode.on;
48 return { viewDistance, lighting, bloom, softTerrain };
55 const autorun = raw.autorun === AutorunMode.on
56 ? AutorunMode.on
57 : AutorunMode.off;
58 return { viewDistance, lighting, bloom, softTerrain, autorun };
49 59 }
50 60
51 61 export function loadGraphicsSettings ()
@@ -21,11 +21,18 @@ export const SoftTerrainMode = Object.freeze({
21 off: "off"
21 off: "off"
22});
22});
23
23
24/** Auto walk/run by path distance (client-only; ignored while following). */
25export const AutorunMode = Object.freeze({
26 on: "on",
27 off: "off"
28});
29
24export const DEFAULT_GRAPHICS_SETTINGS = Object.freeze({
30export const DEFAULT_GRAPHICS_SETTINGS = Object.freeze({
25 viewDistance: ViewDistance.long,
31 viewDistance: ViewDistance.long,
26 lighting: LightingMode.soft,
32 lighting: LightingMode.soft,
27 bloom: BloomMode.on,
33 bloom: BloomMode.on,
28 softTerrain: SoftTerrainMode.on
34 softTerrain: SoftTerrainMode.on,
35 autorun: AutorunMode.off
29});
36});
30
37
31const STORAGE_KEY = "questward-graphics-settings";
38const STORAGE_KEY = "questward-graphics-settings";
@@ -45,7 +52,10 @@ export function normalizeGraphicsSettings (value)
45 const softTerrain = raw.softTerrain === SoftTerrainMode.off
52 const softTerrain = raw.softTerrain === SoftTerrainMode.off
46 ? SoftTerrainMode.off
53 ? SoftTerrainMode.off
47 : SoftTerrainMode.on;
54 : SoftTerrainMode.on;
48 return { viewDistance, lighting, bloom, softTerrain };
55 const autorun = raw.autorun === AutorunMode.on
56 ? AutorunMode.on
57 : AutorunMode.off;
58 return { viewDistance, lighting, bloom, softTerrain, autorun };
49}
59}
50
60
51export function loadGraphicsSettings ()
61export function loadGraphicsSettings ()