local windowX, windowY, windowMoving local frame, headx, heady, headr, frametime, maxx, maxy local pframe, pheadx,heady,pheadr; local prevcam local fileopened local hide local stop function widget:GetInfo() return { name = "Hedtrak v0.4b", desc = "Head tracking", author = "[teh]Beherith", date = "dec 30, 2007", license = "Free", layer = 0, enabled = false -- loaded by default? } end local spGetCameraState = Spring.GetCameraState local spGetCameraVectors = Spring.GetCameraVectors local spGetModKeyState = Spring.GetModKeyState local spGetMouseState = Spring.GetMouseState local spSendCommands = Spring.SendCommands local spSetCameraState = Spring.SetCameraState local spSetMouseCursor = Spring.SetMouseCursor local spWarpMouse = Spring.WarpMouse --Cammode 1=OTA cam --1: =1, ota mode --(where top left is 0,0) --2 bottompoint x --3 bottompoint elevation --4 bottompoint y --5 always 0 --6 sin of vector pointing down, -1 at full down, 0 at horz view --7 cos of vector pointing down, 0 at full down, -1 at horz view --8 dist of cam from bottompoint --9 tangent of vector pointing down, 0.05 at full down, 10 at horz view --10 always -1 --vector info: -- 90 shall be full down, so we shall set tangent of it actually, cotangent -- 1/TG((70+15*(y/maxy))*pi/180) function widget:Initialize() camerastack = {} cameraIndex = -1 lastCameraIndex = -1 returnSpeed = 0.3 windowX = Spring.GetConfigInt("CycleCameraWindowX", 200) windowY = Spring.GetConfigInt("CycleCameraWindowY", 200) windowX=200 windowY=500 windowMoving = false fd= io.open("Headtracker/headdata.txt") if fd then fileopened=true else Echo("Opening Headtracker/headdata.txt failed") fileopened=false end headx=0 heady=0 headr=0 frame=0 height=0 width=0 pheadx=0 pheady=0 pheadr=70 hide=false stop=false frametime=1000 end function widget:Shutdown() Spring.SetConfigInt("CycleCameraWindowX", windowX) Spring.SetConfigInt("CycleCameraWindowY", windowY) end local buttonBoxes = {} function addButton(button) table.insert(buttonBoxes, button) end function inBox(button, x, y, mouseButton) if x > windowX + button.x and x < windowX + button.x + button.width and y > windowY + button.y and y < windowY + button.y + button.height then return true end return false end function DrawButton(button) gl.Color(button.colour[1],button.colour[2],button.colour[3],button.colour[4]) gl.Shape(GL.QUADS, { {v = {windowX + button.x, windowY + button.y}}, {v = {windowX + button.x, windowY + button.y + button.height}}, {v = {windowX + button.x + button.width, windowY + button.y + button.height}}, {v = {windowX + button.x + button.width, windowY + button.y}} }) end function readlineio () --reads the last line of the file repeat string2=string1 string1=fd:read() until string1==nil if string2 ~=nil then return string2 end end local decayFactor = 1 local speedFactor = 50 local mx, my local active local bZero = false local deadzonex = 10 local deadzoney = 5 --drag bar addButton{x = 0, y = 60, width = 100, height = 20, MousePress = function (x, y, button) windowMoving = true end, colour = {0.0, 0.3, 0.8, 0.5}} --RELOAD button addButton{x = 0, y = 40, width = 33, height = 20, MousePress = function (x, y, button) fd= io.open("Headtracker/headdata.txt") bZero = false if fd then Echo("File opened") fileopened=true else Echo("open failed") fileopened=false end end, colour = {0.5, 0.3, 0.3, 0.5}} --show/hide button addButton{x = 33, y = 40, width = 33, height = 20, MousePress = function (x, y, button) if hide==true then hide=false else hide=true end if hide==true then windowMoving=true end end, colour = {0.3, 0.5, 0.3, 0.5}} --Stop /go button addButton{x = 66, y = 40, width = 34, height = 20, MousePress = function (x, y, button) bZero = false if hide==false then if stop==true then stop=false else stop=true end end end, colour = {0.3, 0.3, 0.5, 0.5}} --readln --addButton{x = 25, y = 0, width = 50, height = 20, --MousePress = function (x, y, button) --BackCamera() --readlineio() --end, --colour = {0.2, 0.2, 0.2, 0.5}} function widget:MousePress(x, y, button) for _, box in ipairs(buttonBoxes) do if inBox(box, x, y, button) then box.MousePress(x, y, button) return true end end if button ==1 then --Echo(table.concat(Spring.GetCameraState()," ")) --WRITES CAMSTATE TO CONSOLE end return false end function widget:MouseRelease(x, y, button) windowMoving = false return -1 end function widget:MouseMove(x, y, dx, dy, button) if windowMoving then windowX = windowX + dx windowY = windowY + dy end return false end function widget:Update(dt) if stop==false then if fileopened==true then campos=readlineio() if campos ~= nil then frame=tonumber(campos:sub(1,10)) headx=tonumber(campos:sub(11,15)) heady=tonumber(campos:sub(16,20)) headr=tonumber(campos:sub(21,25)) frametime=tonumber(campos:sub(26,30)) maxx=tonumber(campos:sub(31,35)) maxy=tonumber(campos:sub(36,40)) prevcam=Spring.GetCameraState() if bZero == false then mx = headx my = heady bZero = true end end end local x, y local cs = spGetCameraState() local speed = dt * speedFactor if (cs.name == "ta") then local flip = -cs[10] if headx - mx < 0 then vx = math.min(headx - mx + deadzonex, 0) else vx = math.max(headx - mx - deadzonex, 0) end if heady - my < 0 then vy = math.min(heady - my + deadzoney, 0) else vy = math.max(heady - my - deadzoney, 0) end -- simple, forward and right are locked cs[2] = cs[2] + (speed * flip * -(vx)) if (false) then cs[3] = cs[3] + (speed * flip * -(vy)) else cs[4] = cs[4] + (speed * flip * -(vy)) end end spSetCameraState(cs, 0) end end function widget:DrawScreen() if hide ==false then for _, button in ipairs(buttonBoxes) do DrawButton(button) end gl.Color(0.0, 0.0, 0.0, 1.0) if fileopened==true then gl.Text("Headtrak v.4: "..tostring(frame), windowX + 2, windowY + 65, 10, "t") else gl.Text("Headtrak:NO FILE ", windowX + 5, windowY + 85, 10, "t") end gl.Text("reload",windowX + 1, windowY + 42, 10, "t") gl.Text("hide",windowX + 40, windowY + 42, 10, "t") if stop==false then gl.Text("stop",windowX + 68, windowY + 42, 10, "t") else gl.Text("go",windowX + 68, windowY + 42, 10, "t") end gl.Text("x:"..tostring(vx),windowX + 3, windowY + 22, 10, "t") gl.Text("y:"..tostring(vy),windowX + 35, windowY + 22, 10, "t") gl.Text("fps:"..tostring(math.floor(1000/frametime+0.5)),windowX + 70, windowY + 22, 10, "t") else gl.Color(1.0,1.0,1.0,1.0) gl.Text("show",windowX + 40, windowY + 42, 10, "t") gl.Color(0,0,0,1) end end function Echo(message) if message==nil then Spring.SendCommands({"echo " .. "Attempted to send nil message"}) else Spring.SendCommands({"echo " .. message}) end end