-- World's Scariest Police Chases (Europe), SLES-03425. -- PCSX-Redux: interpreter CPU and debugger enabled. -- Load using dofile('C:/.../Worlds-Scariest-Freecam.lua') in Debug > Lua console. local ffi = require('ffi') local mem = PCSX.getMemPtr() local function u32(a) return tonumber(ffi.cast('uint32_t*',mem + bit.band(a,0x1fffff))[0]) end assert(u32(0x800186ac)==0x8e22f138, 'Unsupported game revision, or game executable not loaded. Enter Free Patrol first.') assert(u32(0x8004f574)==0x3c02800f, 'Unsupported vehicle physics routine.') assert(not PCSX.settings.emulator.Dynarec, 'Choose interpreter CPU and reboot PCSX-Redux first.') PCSX.settings.emulator.Debug.Debug=true if WSPCFreecam then WSPCFreecam.stop() end if freecamBp then freecamBp:remove();freecamBp=nil end local F={enabled=false,keys={},speed=144,turnSpeed=360,reset=false,follow=false,followMaxStep=800} WSPCFreecam=F local function transform() local o=u32(0x800ef138) if o<0x80010000 or o>=0x80200000 then return end local t=u32(o+0x14) if t<0x80010000 or t>=0x801fff90 or t%4~=0 then return end return ffi.cast('int32_t*',mem+bit.band(t,0x1fffff)) end local function capture(p) local h=ffi.cast('int16_t*',p) F.x=tonumber(p[4]);F.y=tonumber(p[5]);F.z=tonumber(p[6]) F.pitch=tonumber(h[0x42/2]);F.yaw=tonumber(h[0x56/2]);F.roll=tonumber(h[0x58/2]) F.reset=false end -- Move the canonical physics body during the game's normal integration loop. -- No guest-function calls or alternate emulated stack are used. local function ramPointer(a) return a>=0x80010000 and a<0x801fff00 and a%4==0 end local function playerPhysics() local c=u32(0x800f4910);if not ramPointer(c) then return end c=u32(c);if not ramPointer(c) then return end local car=u32(c+0x6c);if not ramPointer(car) then return end local data=u32(car+0x14);if not ramPointer(data) then return end return data,car end function F.toggleFollow() if not F.enabled then print('Enable freecam with C before enabling car follow.');return end if F.follow then F.follow=false;print('Car follow OFF. Car remains at its new position.');return end local p=transform();if not p then return end if F.reset or not F.x then capture(p) end local data=playerPhysics();if not data then print('Car follow unavailable: player vehicle not found.');return end local body=ffi.cast('int32_t*',mem+bit.band(data,0x1fffff)) F.followOffsetX=tonumber(body[3])*25/64-F.x F.followOffsetY=tonumber(body[4])*25/64-F.y F.followOffsetZ=tonumber(body[5])*25/64-F.z F.playerData=data;F.follow=true print("Car follow ON. V toggles follow; C returns to driving at the car's new location.") end function F.followPhysics() if not F.enabled or not F.follow or not F.x then return end local r=PCSX.getRegisters() if tonumber(r.GPR.n.s0)~=F.playerData then return end local data=playerPhysics() if data~=F.playerData then F.follow=false;print('Car follow stopped: player vehicle changed.');return end local index=tonumber(r.GPR.n.s8) if index<0 or index>=56 then F.follow=false;return end local p=ffi.cast('int32_t*',mem+bit.band(data,0x1fffff)) local x,y,z=tonumber(p[3])*25/64,tonumber(p[4])*25/64,tonumber(p[5])*25/64 local dx,dy,dz=F.x+F.followOffsetX-x,F.y+F.followOffsetY-y,F.z+F.followOffsetZ-z local distance=math.sqrt(dx*dx+dy*dy+dz*dz) local ratio=math.min(1,F.followMaxStep/math.max(1,distance)) ffi.cast('uint32_t*',mem+0xf2cb8)[index]=1 p[3]=math.floor((x+dx*ratio)*64/25) p[4]=math.floor((y+dy*ratio)*64/25) p[5]=math.floor((z+dz*ratio)*64/25) for _,o in ipairs({0x28,0x2c,0x30,0x34,0x38,0x3c,0xa0,0xa4,0xa8,0xac,0xb0,0xb4}) do p[o/4]=0 end F.followSteps=(F.followSteps or 0)+1 end local function angle(v) return (v+32768)%65536-32768 end local originalBindings={} local bindingsChanged=false local function gameBindings(disable) local pad=PCSX.settings.pads[1] if disable and not bindingsChanged then for name,value in pairs(pad) do if type(name)=='string' and name:sub(1,9)=='Keyboard_' and (value==26 or value==22 or value==4 or value==7 or value==20 or value==29) then originalBindings[name]=value;pad[name]=0 end end bindingsChanged=true elseif not disable and bindingsChanged then for name,value in pairs(originalBindings) do pad[name]=value end originalBindings={};bindingsChanged=false end PCSX.SIO0.slots[1].pads[1].map() end function F.toggle() F.enabled=not F.enabled;F.reset=true;F.keys={};if not F.enabled then F.follow=false end;gameBindings(F.enabled) print('Freecam '..(F.enabled and 'ON: W/S forward/back, A/D turn, Q/Z tilt, J/L strafe, U/O up/down; Shift fast; Home reset; C freecam; V car follow.' or 'OFF: normal chase camera.')) end function F.handleKey(e) local s=e.scancode if s==6 and e.action==1 and not F.keys[s] then F.toggle() end if s==25 and e.action==1 and not F.keys[s] then F.toggleFollow() end if s==74 and e.action==1 then F.reset=true end -- SDL repeat is action 2: it is still a held key, not a release. F.keys[s]=(e.action~=0) end F.keyboard=PCSX.Events.createEventListener('Keyboard',F.handleKey) function F.update() F.frames=(F.frames or 0)+1 if not F.enabled then return end local p=transform();if not p then return end if F.reset or not F.x then capture(p) end local k=F.keys local speed=F.speed*((k[225] or k[229]) and 5 or 1) local turn=F.turnSpeed -- SDL scancodes: keypad 4=92,6=94,8=96,2=90. if k[4] or k[92] then F.yaw=angle(F.yaw-turn) end if k[7] or k[94] then F.yaw=angle(F.yaw+turn) end if k[20] or k[96] then F.pitch=math.min(15000,F.pitch+turn) end if k[29] or k[90] then F.pitch=math.max(-15000,F.pitch-turn) end local yaw=F.yaw*math.pi/32768 local pitch=F.pitch*math.pi/32768 local forward=(k[26] and 1 or 0)-(k[22] and 1 or 0) local side=(k[15] and 1 or 0)-(k[13] and 1 or 0) F.x=F.x+(math.sin(yaw)*math.cos(pitch)*forward+math.cos(yaw)*side)*speed F.z=F.z+(math.cos(yaw)*math.cos(pitch)*forward-math.sin(yaw)*side)*speed F.y=F.y-math.sin(pitch)*forward*speed+((k[18] and 1 or 0)-(k[24] and 1 or 0))*speed p[1]=F.x;p[2]=F.y;p[3]=F.z;p[4]=F.x;p[5]=F.y;p[6]=F.z local h=ffi.cast('int16_t*',p) h[0x42/2]=F.pitch;h[0x56/2]=F.yaw;h[0x58/2]=F.roll p[0]=bit.bor(p[0],0x03000000) end function F.install() if F.stopped then return end PCSX.settings.emulator.Debug.Debug=true if F.bp then F.bp:remove() end if F.followBp then F.followBp:remove() end F.followBp=PCSX.addBreakpoint(0x8004f574,'Exec',4,'WSPC player-car follow',F.followPhysics) F.bp=PCSX.addBreakpoint(0x800186ac,'Exec',4,'WSPC European free camera',F.update) F.reset=true;F.keys={};F.follow=false end F.install() F.resetListener=PCSX.Events.createEventListener('ExecutionFlow::SaveStateLoaded',function() if not F.stopped then PCSX.nextTick(F.install) end end) function F.stop() F.stopped=true;F.enabled=false;F.keys={};gameBindings(false) if F.bp then F.bp:remove() end if F.followBp then F.followBp:remove() end if F.keyboard then F.keyboard:remove() end if F.resetListener then F.resetListener:remove() end end print('WSPC freecam loaded. Press C in the game window to toggle.')