function Create(self) --The timer that will measure out the missile's events. self.LTimer = Timer(); --The missile's target, currently set to nothing. self.target = nil; --Find out who shot the weapon by finding the closest actor within 50 pixels. local curdist = 50; for actor in MovableMan.Actors do local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2); if dist < curdist then curdist = dist; self.parent = actor; end end --If the missile has no firer, make it go after anyone. Otherwise, set its team to that of the firer. if MovableMan:IsActor(self.parent) then self.Team = self.parent.Team; else self.Team = -1; end end function ScanVectorForMO(StartPosition, VectorToDestination, MOIDToIgnore) local ResultMOID = 255; local ResultPosition = StartPosition + VectorToDestination; local CheckMO = SceneMan:CastMORay(StartPosition, VectorToDestination, MOIDToIgnore, -1, false, 1); if CheckMO ~= 255 then local MO = MovableMan:GetMOFromID(CheckMO); if MO.GetsHitByMOs then ResultMOID = CheckMO; ResultPosition = StartPosition + (VectorToDestination/ VectorToDestination.Magnitude * SceneMan:ShortestDistance(StartPosition,MO.Pos,true).Magnitude); end end return ResultPosition, ResultMOID; end function Update(self) if self.LTimer:IsPastSimMS(50) or MovableMan:IsActor(self.target) == false then --Get a target. Go for the closest actor within 250 pixels. if MovableMan:IsActor(self.target) == false then local curdist = 250; for actor in MovableMan.Actors do local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2); if dist < curdist and actor.Team ~= self.Team then curdist = dist; self.target = actor; end end end ResultPosition, ResultMOID = ScanVectorForMO(self.Pos, self.Vel.Normalized*50, self.ID); if ResultMOID ~= 255 then self:GibThis(); end --If the target still exists... if MovableMan:IsActor(self.target) then --Make sure the missile's thruster is firing. if self:IsEmitting() == false then self:EnableEmission(true); end --duh102 make it thrust towards the target using a mild accel --we use the missile's natural OrientToVel to make it look nicer than always pointing at the target --constants local turnspeed = 1; local maxspeed = 37; local targetdir = math.atan2(-(self.target.Pos.Y-self.Pos.Y),(self.target.Pos.X-self.Pos.X)); local desiredspeedx = math.cos(targetdir) * maxspeed local desiredspeedy = math.sin(targetdir) * (-maxspeed) if self.Vel.X < desiredspeedx then self.Vel.X = self.Vel.X + turnspeed elseif self.Vel.X > desiredspeedx then self.Vel.X = self.Vel.X - turnspeed end if self.Vel.Y < desiredspeedy then self.Vel.Y = self.Vel.Y + turnspeed elseif self.Vel.Y > desiredspeedy then self.Vel.Y = self.Vel.Y - turnspeed end end if self.LTimer:IsPastSimMS(7500) then --If the missile has run out of time, self destruct! self:GibThis(); end end end