--DESCRIPTION: vehicle script for use with smallg's city game --DESCRIPTION: [SPEED=9] --DESCRIPTION: [TURNSPEED#=3] local U = require "scriptbank\\utillib" g_vehicle = {} function vehicle_properties(e, speed, turnspeed) g_vehicle[e] = {} local gv = g_vehicle[e] gv.speed = speed gv.turnspeed = turnspeed gv.pathprev = -1 gv.pathe = e CollisionOff(e) GravityOff(e) end function vehicle_init(e) vehicle_properties(e, 9,3) end function vehicle_main(e) local function RotateToPoint(e,x,z) local destx = x - g_Entity[e]['x'] local destz = z - g_Entity[e]['z'] local angle = math.atan2(destx,destz) angle = angle * (180.0 / math.pi) if angle < 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end SetRotation(e,0,angle,0) return angle end local function RotateToPointSlowly(e,x,z,v) if v == nil then v = 1 end local destx = x - g_Entity[e]['x'] local destz = z - g_Entity[e]['z'] local angleneeded = math.atan2(destx,destz) angleneeded = angleneeded * (180.0 / math.pi) if angleneeded < 0 then angleneeded = 360 + angleneeded elseif angleneeded > 360 then angleneeded = angleneeded - 360 end local current_angy = g_Entity[e]['angley'] while current_angy < 0 or current_angy > 360 do if current_angy <= 0 then current_angy = 360 + current_angy elseif current_angy > 360 then current_angy = current_angy - 360 end end local L = angleneeded - v local R = angleneeded + v if L <= 0 then L = 360 + L elseif L > 360 then L = L - 360 end if R <= 0 then R = 360 + R elseif R > 360 then R = R - 360 end --if not already facing the target direction (+/- the rotation speed) if current_angy < L or current_angy > R then --do it in 2 halfs so we can account for 0/360 wrap if current_angy > 180 then if angleneeded > current_angy - 180 and angleneeded < current_angy then current_angy = current_angy - v else current_angy = current_angy + v end else if angleneeded < current_angy + 180 and angleneeded > current_angy then current_angy = current_angy + v else current_angy = current_angy - v end end SetRotation(e,0,current_angy,0) return current_angy end end local function GetFlatDistanceFromEntity(e, t) local x1,z1 = g_Entity[e].x,g_Entity[e].z local x2,z2 = g_Entity[t].x,g_Entity[t].z local dx = x2-x1 local dz = z2-z1 local ddist = dx*dx+dz*dz return ddist end local gv = g_vehicle[e] if gv.pathe == e then for a = 0, 9 do local connectede = GetEntityRelationshipID(e, a) if connectede > 0 then gv.pathe = connectede gv.pathprev = e end end else if GetFlatDistanceFromEntity(e,gv.pathe) < 140 then local fp = 0 for a = 0, 9 do local connectede = GetEntityRelationshipID(gv.pathe, a) if connectede > 0 then if GetEntityMarkerMode(connectede) == 11 then if connectede ~= gv.pathprev then fp = gv.pathe gv.pathe = connectede break end end end end if fp == 0 then fp = gv.pathe gv.pathe = gv.pathprev RotateToPoint(e,g_Entity[gv.pathe].x,g_Entity[gv.pathe].z) end gv.pathprev = fp else local ex,ey,ez,eax,eay,eaz = GetEntityPosAng(e) local destx,destz = g_Entity[gv.pathe].x,g_Entity[gv.pathe].z RotateToPointSlowly(e,destx,destz,gv.turnspeed) local ox,oy,oz = U.Rotate3D(0,0,gv.speed,0,math.rad(eay),0) SetPosition(e,ex+ox,ey+oy,ez+oz) end end end