File size: 1,735 Bytes
c039cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* patch_movement.mjs — ROOT CAUSE FIX: "không điều khiển được nhân vật 3D".
   Trong vòng lặp Movement (app.js), nx/nz được khai báo bằng `let` BÊN TRONG
   khối `if (moving) {...}` nhưng lại được tham chiếu BÊN NGOÀI khối đó trong
   điều kiện gửi Net.update(...):

       let nx = me.pos.x + ...
       let nz = me.pos.z + ...   // scope chỉ trong if (moving)

       const p = last.current
       if (!p.pos || Math.abs(nx - p.pos.x) > 0.01 || Math.abs(nz - p.pos.z) > 0.01 ...)
                      // ^^ ReferenceError: nx is not defined khi đứng yên

   Khi nhân vật đứng yên (mới vào hang), luồng rAF văng ReferenceError ngay
   frame thứ 2 -> loop chết -> KHÔNG điều khiển được nhân vật. Fix: dùng
   me.pos.x / me.pos.z thay cho nx/nz (luôn tồn tại), độc lập với việc đang
   di chuyển hay đứng yên. */
import fs from 'node:fs'

const path = 'frontend/app.js'
let s = fs.readFileSync(path, 'utf8')

const marker =
  "        if (!p.pos || Math.abs(nx - p.pos.x) > 0.01 || Math.abs(nz - p.pos.z) > 0.01 || p.rot !== rot || p.anim !== anim) {"

const count = s.split(marker).length - 1
if (count !== 1) {
  console.error(`PATCH MOVEMENT FAILED: marker not unique/found (count=${count})`)
  process.exit(1)
}

const fixed =
  "        // dùng me.pos thay nx/nz (nx/nz chỉ có scope trong if (moving) -> chống ReferenceError khi đứng yên)\n" +
  "        if (!p.pos || Math.abs(me.pos.x - p.pos.x) > 0.01 || Math.abs(me.pos.z - p.pos.z) > 0.01 || p.rot !== rot || p.anim !== anim) {"

s = s.replace(marker, fixed)
fs.writeFileSync(path, s)
console.log('patched app.js ok (MOVEMENT root-cause fix: nx/nz scope)')